[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
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;
}
