I'm trying to write a basic calculator for a class in my school. Basically the format would be "Would you like to add? " If yes is chosen it would let you input integers to add, if no is chosen it would ask you "Would you like to subtract". Etc. through multiplication and division.
My program looks like this:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int A,B,C;
char yes,no;
cout << "Would you like to add? \n";
cin >> yes;
if (yes)
cout << "Enter A \n";
cin >> A;
cout << "Enter B \n";
cin >> B;
cout << "Enter C \n";
cin >> C;
cin.ignore();
system("pause");
}
Note that I haven't added the code to subtraction/multiplication/division because this is as far as i get without a problem.
What is happening is that when i run the program, I choose yes for addition. It then runs all my Cout << "Enter A/B/C" all at once without allowing any cin/input to be entered, it enters all my "Enter A/B/C" at once.
I'm a beginner so some help would greatly be appreciated!
'yes' is a char, so each time you enter more than a single character, only the first would get read into it.
The other characters will be read by cin >> A; and if they aren't numerical, input would fail ( to prevent this see http://www.cplusplus.com/forum/articles/6046/ )
If you want to compare the user input to the string "yes" read a std::string instead of a char.
You should also have braces surrounding the if block or it will affect only the following line.
Also using if (yes) won't work you need something like if (yes='y' || yes='Y')
The if command should only work with Boolean type of input as in true/false. So, comparing your char variable yes to a 'y' or a 'Y' will flag a true and let the if execute the next command or curly braced set of commands.
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int X,Y,Z;
char a,s,m,d;
cout << "Enter A for addition, S for subtraction, M for multiplication, and D for Division \n \n";
cin >> a,s,m,d;
if (a) cout << "You are adding. Enter 3 integers for X Y Z \n";
else cout << "You are subtracting. Enter 3 integers for X Y Z \n";
cin >> X;
cin >> Y;
cin >> Z;
cin.ignore();
system("pause");
}
Now no matter what i press it will always do addition
You have do to do one at a time. You also need to set up a conditional cin beforehand so that you can compare each one to the a or the s or the m or the d so you can then direct the following code into the proper channel.
This line does not do what you think it does, nor do chars work like the you are testing them. Instead, you should simply get input into a single char and then test it against each possible input ('A', 'S', 'M', ...)
cout << "Enter A for addition, S for subtraction, M for multiplication, and D for Division \n \n";
cin >> n;
if (n == 'a')
cout << "Addition: Enter integers for X,Y,Z \n";
cin >> X;
cin >> Y;
cin >> Z;
cout << "Answer is " <<X+Y+Z;cout << "\n";
if (n =='s') cout << "Subtraction \n";
if (n =='m') cout << "Multiplication \n";
if (n == 'd') cout << "Division \n";
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int X,Y,Z;
char n,a,s,m,d;
cout << "Enter A for addition, S for subtraction, M for multiplication, D for Division \n \nInput 0 or 1 in place of Z for usage of two variables \n";
cin >> n;
if (n =='a') cout << "Addition: Enter integers for X,Y,Z \n";
if (n =='s') cout << "Subtraction: Enter integers for X,Y,Z \n";
if (n =='m') cout << "Multiplication: Enter integers for X.Y.Z \n";
if (n =='d') cout << "Division: Enter integers for X,Y,Z \n";
cin >> X;
cin >> Y;
cin >> Z;
if (n == 'a') cout << "Answer is " <<X+Y+Z; cout << "\n";
if (n == 's') cout << "Answer is " << X-Y-Z; cout << "\n";
if (n == 'm') cout << "Answer is " << X*Y*Z; cout << "\n";
if (n == 'd') cout << "Answer is " << X/Y/Z; cout << "\n";
cin.ignore();
system("pause");
}
And everything seems to be working like it should, thanks all