[HELP]Help with C++ error?

Posts 1–15 of 20 · Page 1 of 2
[HELP]Help with C++ error?
Hi I was recently coding a prime # finder, calculator, sum/product function, and quadratic equation solver for my class and something really weird happens. I compile and it works fine, but when I run and type 3 for calculator it goes to if(choice = 1){...} instead of if(choice = 3){...}. But then it also runs the if(choice=3{...}. Can any1 help me?
Here is code
Code:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    char end;
    int prime, choice;
    double i, sum;
    double x, Y;
    top:
    choice = 2;
    cout << "Hello welcome to My all in one handy helper!" << "\n";
    cout << "Please enter 1 for a sum function, 2 for a Quadractic eqation solver," << "\n" << "Or 3 for a Prime number checker." << "\n";
    cin >> choice;   
    if(choice = 1) {
    cout << "This is a sum function.";  
    cout << "Please enter a starting value" << "\n";   
    cin >> x;
    cout << "Please enter an ending value" << "\n";
    cin >> Y;  
    sum = 0;
    for(i = x; i <= Y; i++){
          sum = sum + i; //cout << i << ", " << sum << "\n"; <-- Tells the user the sum for each steps.
          }
    cout << "The sum from " << x << " to " << Y << " is " << sum << "\n";
    sum = 1;
    for(i = x; i <= Y; i++){       
          sum = sum*i;  
          }      
    cout << "The product from " << x << " through " << Y << " is "<< sum << "\n";
}  
    if(choice = 3)
{
    int x, i;
    cout << "Welcome to My Prime Number Calculator! \n";    
    cout << "Please enter an odd number and I'll tell you if it is prime! \n";    
    cin >> x;    
    prime = 1;
    for(i = 3; i < x; i = i+2 ){
           if(x % i == 0){
           cout << i << " is a factor \n";
           prime = 0;
           }
           }
    if(prime == 0) {
             cout << x << " is not a prime number";
             }
             else
             {
             cout << x << " is a prime number";
             }
}   
    cout << "Start again? (y/n)";
    cin >> end;
    if(end == 'y')
    {
            goto top;
            }
    return 0;    
}
Wrong:
[php]
if(choice = 3) {...}
[/php]

Right:
[php]
if(choice == 3){...}
[/php]
You need 2 "=" signs, Or it will screw up.

And this counts for the 2 other "if"'s as well
Quote Originally Posted by Melodia View Post
You need 2 "=" signs, Or it will screw up.

And this counts for the 2 other "if"'s as well
Thanks for the advice but when I tried that it still didn't work. Maybe I'm just imagining.

EDIT: I was imagining I thought I did that. But yea thanks for the help. Lord my teacher is so dumb. He told us we had to make this stupid think in the first place.

Thanks for the help. BTW if instead of using a
Code:
cin >> whatever
command and use
Code:
if(GetASyncKeyState_NUM0)
do I need to do:
Code:
if((GetASyncKeyState_NUM0)&1)
Thanks
It works fine for me..

Right after 'top:' you put choice = 2; for some reason.. remove it.


Code that worked for me:
[php]
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
char end;
int prime, choice;
double i, sum;
double x, Y;
top:
cout << "Hello welcome to My all in one handy helper!" << "\n";
cout << "Please enter 1 for a sum function, 2 for a Quadractic eqation solver," << "\n" << "Or 3 for a Prime number checker." << "\n";
cin >> choice;
if(choice == 1) {
cout << "This is a sum function.";
cout << "Please enter a starting value" << "\n";
cin >> x;
cout << "Please enter an ending value" << "\n";
cin >> Y;
sum = 0;
for(i = x; i <= Y; i++){
sum = sum + i; //cout << i << ", " << sum << "\n"; <-- Tells the user the sum for each steps.
}
cout << "The sum from " << x << " to " << Y << " is " << sum << "\n";
sum = 1;
for(i = x; i <= Y; i++){
sum = sum*i;
}
cout << "The product from " << x << " through " << Y << " is "<< sum << "\n";
}
if(choice == 3)
{
int x, i;
cout << "Welcome to My Prime Number Calculator! \n";
cout << "Please enter an odd number and I'll tell you if it is prime! \n";
cin >> x;
prime = 1;
for(i = 3; i < x; i = i+2 ){
if(x % i == 0){
cout << i << " is a factor \n";
prime = 0;
}
}
if(prime == 0) {
cout << x << " is not a prime number";
}
else
{
cout << x << " is a prime number";
}
}
cout << "Start again? (y/n)";
cin >> end;
if(end == 'y')
{
goto top;
}
return 0;
}

[/php]
Quote Originally Posted by Void View Post
It works fine for me..

Right after 'top:' you put choice = 2; for some reason.. remove it.


Code that worked for me:
[php]
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
char end;
int prime, choice;
double i, sum;
double x, Y;
top:
cout << "Hello welcome to My all in one handy helper!" << "\n";
cout << "Please enter 1 for a sum function, 2 for a Quadractic eqation solver," << "\n" << "Or 3 for a Prime number checker." << "\n";
cin >> choice;
if(choice == 1) {
cout << "This is a sum function.";
cout << "Please enter a starting value" << "\n";
cin >> x;
cout << "Please enter an ending value" << "\n";
cin >> Y;
sum = 0;
for(i = x; i <= Y; i++){
sum = sum + i; //cout << i << ", " << sum << "\n"; <-- Tells the user the sum for each steps.
}
cout << "The sum from " << x << " to " << Y << " is " << sum << "\n";
sum = 1;
for(i = x; i <= Y; i++){
sum = sum*i;
}
cout << "The product from " << x << " through " << Y << " is "<< sum << "\n";
}
if(choice == 3)
{
int x, i;
cout << "Welcome to My Prime Number Calculator! \n";
cout << "Please enter an odd number and I'll tell you if it is prime! \n";
cin >> x;
prime = 1;
for(i = 3; i < x; i = i+2 ){
if(x % i == 0){
cout << i << " is a factor \n";
prime = 0;
}
}
if(prime == 0) {
cout << x << " is not a prime number";
}
else
{
cout << x << " is a prime number";
}
}
cout << "Start again? (y/n)";
cin >> end;
if(end == 'y')
{
goto top;
}
return 0;
}

[/php]
Yea I got it to work. Can you give me help the GetASyncKeyState_NUM0 because when I use it it doesnt respond to NUM0
When programming, make sure to look over your code and think a little. Ask yourself, how is it going to execute itself?

[php]
//code
if(GetAsyncKeyState(VK_NUMPAD0))
{
//stuff
}
[/php]

In this case, it does a single check to see if NUMPAD0 was pressed, how long do you have to press the button? Not long enough for sure.

Solution:
Wrap it in a loop.
Quote Originally Posted by Void View Post
When programming, make sure to look over your code and think a little. Ask yourself, how is it going to execute itself?

[php]
//code
if(GetAsyncKeyState(VK_NUMPAD0))
{
//stuff
}
[/php]

In this case, it does a single check to see if NUMPAD0 was pressed, how long do you have to press the button? Not long enough for sure.

Solution:
Wrap it in a loop.
What type of loop you think I should use?
a for() loop?
Quote Originally Posted by mwb1234 View Post
What type of loop you think I should use?
a for() loop?
Infinite loop, until key is hit. Then you can break out of the loop, if you want.
Quote Originally Posted by Void View Post
Infinite loop, until key is hit. Then you can break out of the loop, if you want.
You have more patience than me

~Thanks
Quote Originally Posted by Melodia View Post
You have more patience than me

~Thanks
lol yes thank you for the help.
I finished it.
Cant post now gotta go turn it in...
Most valid requests we get here require entire revision rather than guidance on one malfunctioning aspect, so things are usually pretty dull handling tedious rudimentary beginner concepts and revising things for them.
~
Nothing wrong with that if they learn something of course, but I got tired of doing this.
Quote Originally Posted by Arhk View Post
Most valid requests we get here require entire revision rather than guidance on one malfunctioning aspect, so things are usually pretty dull handling tedious rudimentary beginner concepts and revising things for them.
~
Nothing wrong with that if they learn something of course, but I got tired of doing this.
Did you help at all? 0_0
Quote Originally Posted by mwb1234 View Post
Did you help at all? 0_0
I didn't.
~
Why do you ask?
Hmm, I like helping anyway.
Posts 1–15 of 20 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?