I'm making a calculator code for a class assignment, but it's not working. For some reason my cin statement inside the if statement isn't working when I test the code, but it does build successfully. Any ideas?
#include <iostream>
usingnamespace std;
int number1;
int number2;
int symbol;
int sum = number1 + number2;
int division = number1 + number2;
int subtraction = number1 + number2;
int multiplication = number1 + number2;
int modulus = number1 + number2;
int main()
{
cout << "Welcome to my calculator\n"
<< "Please enter a symbol to indicate which type of problem we will be solving\n"
<< "The choices are +, -, /, *, or % to find the modulus of two numbers\n";
cin >> symbol;
if ("+")
{
cout << "Please enter two numbers\n";
cin >> number1 >> number2;
cout << number1 << "plus" << number2 << "is" << sum;
}
elseif ("-")
{
cout << "Please enter two numbers\n";
cin >> number1 >> number2;
cout << number1 << "minus" << number2 << "is" << subtraction;
}
elseif ("/")
{
cout << "Please enter two numbers\n";
cin >> number1 >> number2;
cout << number1 << "divided by" << number2 << "is" << division;
}
elseif ("*")
{
cout << "Please enter two numbers\n";
cin >> number1 >> number2;
cout << number1 << "times" << number2 << "is" << multiplication;
}
elseif ("%")
{
cout << "Please enter two numbers\n";
cin >> number1 >> number2;
cout << number1 << "modulus" << number2 << "is" << modulus;
}
else
{
cout << "Invalid operator entered\n"
<< "Please enter a symbol to indicate which type of problem we will be solving\n";
}
system("pause");
return 0;
}
there is a difference between an int, char,string.
you defined symbol in line 7 as int and you intend to compare it will string in the if statements.
Compare int with int, char with char and so on.
A char is represented by single quotes. char character = 'A';.
A string by double quotes. string testString = "xxx";.
To make a comparison, say if(something compares-to another). The result is bool.
Ofcourse, the must be comparable; if(1) is always true. Same as if('A') and so on.
I'm still confused on how to make the if statement say that if they type +,-,/,*,% then the following statement will happen. Can I not use + in an if statement?