cout/cin problem (Beginner)

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!

Thanks in advance.

'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.

And please use [code][/code] tags
Ok thanks a lot Bazzy ill try that out when i get a chance
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.
Ok ive changed it up a bit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cstdlib>
#include <iostream>
#include <string>

using namespace 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 can't do
cin >> a,s,m,d


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.

For example...

cin >> x;

if(x==a)... etc.
cin >> a,s,m,d;

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', ...)
Ok i have addition working, but im not sure where to put sub/mult/div. Sorry im new at this, just not entirely sure where everything has to go yet.



[code]#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
int X,Y,Z;
char n,a,s,m,d;

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";








cin.ignore();
system("pause");


}
You are still missing the braces after the if
1
2
3
4
5
6
7
8
9
if(n=='a')
{
   do stuff;
}
else if(n=='s')
{
   do stuff;
}
else if(n=='m'), etc.
Last edited on
My final code looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdlib>
#include <iostream>
#include <string>

using namespace 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
Topic archived. No new replies allowed.