-
comparison problem
Hi, I am new to C++ so this may seem like a stupid problem, but it has me stumped.
What I'm supposed to do here, is compare the value in mysign to +. Add if it is +, subtract if it is -, and display error message if it is neither.
this is what I have so far:
Code:
#include <iostream>
using namespace std;
main()
{
int first, second, result=0;
char mysign;
cout<<"Please input an integer. \n";
cin>> first;
cout<<"Please input an integer once again. \n";
cin>> second;
cout<<"Please input + or - \n";
cin>> mysign;
if (+ == mysign) ; {
cout<< first+second;
cin>> result;
}
else if(- == mysign) ; {
cout<< first-second;
cin>> result;
}
else ;{
cout<< "Cannot perform an operation.\n";
}
cout<<"The variable stored in result is:" << result << endl;
cin.get();
return 0;
}
There are probably many things wrong here, please help me correct them. I'm lost.
-
mysign is a char. chars are reffered to with single quotes.
+ <---That's an operator
'+' <-----That's a character
When your program looks at mysign, make sure you're using the right one.
-
You need to put " " around the symbols. Right now you are saying "minus from itself mysign" you want it to be "-" == mysign so that it is comparing the 2.
-
Code:
if ('+' == mysign) ; {
Remove the ; from that line and from
Code:
if ('-' == mysign) ; {
-
Code:
#include <iostream>
using namespace std;
int main()
{
int first, second, result=0;
char mysign;
cout<<"Please input an integer. \n";
cin>> first;
cin.ignore();
cout<<"Please input an integer once again. \n";
cin>> second;
cin.ignore();
cout<<"Please input + or - \n";
cin>> mysign;
cin.ignore();
if (mysign=='+') {
result = first + second;
}
if(mysign=='-') {
result = first - second;
}
cout<<"The variable stored in result is: " << result << endl;
cin.get();
return 0;
}
Here, I cleaned up the code for you.
-