How to declare a variable that accepts a character input from a user

Hi everyone. As my nick indicates, newbie here. Please help if you can. Tks.
This is what I'm trying to do:
Write a program which accepts two integers from the user. Then it asks the user what he wants to do with those two numbers. If the user choice is
‘A’ – Add the two numbers
‘B’ – Subtract the second number from the first number
‘C’ – Multiply the first number by the second number
‘D’ – Divide the first number by the second number
‘Q’ – End the operation
Your program should continue to accept the two numbers and the user choice unless the user enters ‘Q’ as choice.

My code (below)doesn't seem to like the way I declared the variable that accepts the user input as a character. I think the IF statement fails because of that. Also, I had to comment out the do..while loop because it won't even build with that in there.
Any help is GREATLY appreciated.
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
43
44
// Lab3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
 using namespace std;
 int main()
 {
 int X, Y, Z;
 char Action;
//do {
 cout<<"Please enter a number:  " << endl;
 cin>>X;
 cout<<"Please enter a second number:  " << endl;
 cin>>Y;
cout<<"Using the table below, please enter a code for the action you would like performed on your numbers"<< endl<< "A – Add the two numbers"<< endl<< "B – Subtract the second number from the first number"<< endl<< "C – Multiply the first number by the second number"<< endl<< "D – Divide the first number by the second number"<< endl<< "Q – End the operation"<< endl;
cin>>Action;
if ( Action='A' ) {                  // If the user chose option A
     Z = X + Y;  			//not sure if we need a C++ statement 
cout<<"Their sum is:  "<< Z<<endl;
  }
  else if (Action='B') {            // else if the user chose option B 
          Z = X - Y;  		//subtract the second number from the first
cout<<"Their difference  is:  "<< Z<<endl;
  }
  else if (Action='C') {            // else if the user chose option C 
          Z = X * Y;  		// multiply the first number by the second number
cout<<"Their product  is:  "<< Z<<endl;
  }
else if (Action='D') {            // else if the user chose option D
          Z = X / Y;  		// Divide the first number by the second number
cout<<"Their quotient is:  "<< Z<<endl;
  }
else {
    cout<<"Thank you for playing"<<endl;     // Executed if no other statement is true
	}
return 0;
 }
//while ( Action !='Q' );		//close the while loop when the user chooses Action Q}
//}
//return 0
//} 
Instead of the assignment as for example here

if ( Action='A' )

you should use the comparision

if ( Action == 'A' )
Last edited on
Oh, wow! I tried that first but was getting errors. I think I had double quotes around the letters instead of single quotes. Now I tried == again, leaving the single qutoes, and it seems to work. Thank you!!

Any chance you can help me with the do..while loop?

Or maybe help me figure out the console window automatically closes quickly after displaying one result? It's supposed to stay open and keep looping.

Here's the corrected (so far) code:

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
43
44
// Lab3.cpp : Defines the entry point for the console application.
//
//
#include "stdafx.h"

#include <iostream>
 using namespace std;
 int main()
 {
 int X, Y, Z;
 char Action;
//do {
 cout<<"Please enter a number:  " << endl;
 cin>>X;
 cout<<"Please enter a second number:  " << endl;
 cin>>Y;
cout<<"Using the table below, please enter a code for the action you would like performed on your numbers"<< endl<< "A – Add the two numbers"<< endl<< "B – Subtract the second number from the first number"<< endl<< "C – Multiply the first number by the second number"<< endl<< "D – Divide the first number by the second number"<< endl<< "Q – End the operation"<< endl;
cin>> Action;
if ( Action=='A' ) {                  // If the user chose option A
     Z = X + Y;  			//not sure if we need a C++ statement 
cout<<"Their sum is:  "<< Z<<endl;
  }
  else if (Action=='B') {            // else if the user chose option B 
          Z = X - Y;  		//subtract the second number from the first
cout<<"Their difference  is:  "<< Z<<endl;
  }
  else if (Action=='C') {            // else if the user chose option C 
          Z = X * Y;  		// multiply the first number by the second number
cout<<"Their product  is:  "<< Z<<endl;
  }
else if (Action=='D') {            // else if the user chose option D
          Z = X / Y;  		// Divide the first number by the second number
cout<<"Their quotient is:  "<< Z<<endl;
  }
else {
    cout<<"Thank you for playing"<<endl;     // Executed if no other statement is true
	}
 }
//return 0;
// }
//while ( Action !='Q' );		//close the while loop when the user chooses Action Q}
//}
//return 0
//} 
@noo1
Or maybe help me figure out the console window automatically closes quickly after displaying one result? It's supposed to stay open and keep looping.


Read here
http://www.cplusplus.com/forum/beginner/1988/
my program errors out if I use cin.get or cin.ignore like the article states
I thinl that you forgot to include header <limits>
I added it. Thanks! No errors, but window still closes. :-(

I'm listing some msgs I receive. Any idea on them or on the do...while loop?

Symbols loaded.
Cannot find or open the PDB file
Cannot find or open the PDB file
Cannot find or open the PDB file
Symbols loaded.
Symbols loaded.
The program '[10648] Native' has exited with code 0 (0x0).

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
43
44
45
46
47
48
// Lab3.cpp : Defines the entry point for the console application.
//
//
#include "stdafx.h"
#include <limits>

#include <iostream>
 using namespace std;
 int main()
 {
 int X, Y, Z;
 char Action;
//do {
 cout<<"Please enter a number:  " << endl;
 cin>>X;
 cout<<"Please enter a second number:  " << endl;
 cin>>Y;
cout<<"Using the table below, please enter a code for the action you would like performed on your numbers"<< endl<< "A – Add the two numbers"<< endl<< "B – Subtract the second number from the first number"<< endl<< "C – Multiply the first number by the second number"<< endl<< "D – Divide the first number by the second number"<< endl<< "Q – End the operation"<< endl;
cin>> Action;
if ( Action=='A' ) {                  // If the user chose option A
     Z = X + Y;  			//not sure if we need a C++ statement 
cout<<"Their sum is:  "<< Z<<endl;
  }
  else if (Action=='B') {            // else if the user chose option B 
          Z = X - Y;  		//subtract the second number from the first
cout<<"Their difference  is:  "<< Z<<endl;
  }
  else if (Action=='C') {            // else if the user chose option C 
          Z = X * Y;  		// multiply the first number by the second number
cout<<"Their product  is:  "<< Z<<endl;
  }
else if (Action=='D') {            // else if the user chose option D
          Z = X / Y;  		// Divide the first number by the second number
cout<<"Their quotient is:  "<< Z<<endl;
  }
else {
    cout<<"Thank you for playing"<<endl;     // Executed if no other statement is true
	}
cin.ignore();
 }
 

 //return 0;
// }
//while ( Action !='Q' );		//close the while loop when the user chooses Action Q}
//}
//return 0
//} 
ok, I think I have most of it working. It needs some tweaks and I'm not sure if it's the correct way to program this but...here it is:

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
43
// Lab3.cpp : Defines the entry point for the console application.
//
//
#include "stdafx.h"

#include <iostream>
 using namespace std;
 int main()
 {
 int X, Y, Z;
 char Action;
do {
 cout<<"Please enter a number:  " << endl;
 cin>>X;
 cout<<"Please enter a second number:  " << endl;
 cin>>Y;
cout<<"Using the table below, please enter a code for the action you would like performed on your numbers"<< endl<< "A - Add the two numbers"<< endl<< "B - Subtract the second number from the first number"<< endl<< "C - Multiply the first number by the second number"<< endl<< "D - Divide the first number by the second number"<< endl<< "Q - End the operation"<< endl;
cin>> Action;
if ( Action=='A' ) {                  // If the user chose option A
     Z = X + Y;  			//not sure if we need a C++ statement 
cout<<"Their sum is:  "<< Z<<endl;
  }
  else if (Action=='B') {            // else if the user chose option B 
          Z = X - Y;  		//subtract the second number from the first
cout<<"Their difference  is:  "<< Z<<endl;
  }
  else if (Action=='C') {            // else if the user chose option C 
          Z = X * Y;  		// multiply the first number by the second number
cout<<"Their product  is:  "<< Z<<endl;
  }
else if (Action=='D') {            // else if the user chose option D
          Z = X / Y;  		// Divide the first number by the second number
cout<<"Their quotient is:  "<< Z<<endl;
  }
else {
    cout<<"Thank you for playing"<<endl;     // Executed if no other statement is true
	}
 }
 while ( Action !='Q' );			//close the while loop when the user chooses Action Q

 return 0;
 }
 
I just saw a more recent post froma user asking about the Switch case statement.

Can anyone tell me if that would be a better way to program this, rather than the if statement I used?

Thanks in advance for your help.
Switch would be better in this case, yes.
Awesome...this is what I came up with. (but sure wish someone would have mentioned it to me before...) Anyways, very glad another posted a question about Switch Case because I'd never even heard of it before reading the post.

My code using switch seems much cleaner.

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
//Lab3C.cpp
#include <iostream>
using namespace std;
int main()
{
	int long long X, Y;
	char Action;
	cout<<"Please enter a whole number:  ";
	cin>>X;
	cout<<"Please enter a second whole number:  ";
	cin>>Y;
	cout<<"Using the table below, please enter a code for the action"<<endl<<"you would like performed on your numbers"<< endl<< "A - Add the two numbers"<< endl<< "B - Subtract the second number from the first number"<< endl<< "C - Multiply the first number by the second number"<< endl<< "D - Divide the first number by the second number"<< endl<< "Q - End the operation"<< endl;
	cin>>Action;
	while (Action != 'Q' && Action != 'q')
	{
		switch(Action)
		{
		case 'a':
		case 'A': cout<<"Here is your sum:  "<<X<<" + "<<Y<<"= "<<X+Y<<endl;
			break;
		case 'b':
		case 'B':  cout<<"Here is your difference:  "<<X<<" - "<<Y<<"= "<<X-Y<<endl;
			break;
		case 'c':
		case 'C':  cout<<"Here is your product:  "<<X<<" * "<<Y<<" = "<<X*Y<<endl;
			break;
		case 'd':
		case 'D':  cout<<"Here is your quotient:  "<<X<<" / "<<Y<<" = "<<X/Y<<endl;
			break;
			default  :  cout<<"That is not a choice."<<endl;
	}
		cout<<"Select a new action (A-D,Q): "<<endl;
		cin >> Action;
	}
	cout<<"Thank you for playing."<<endl;
	return 0;
	}
Topic archived. No new replies allowed.