problem with cin >> "char" and switch

I have problem with cin >> char and switch command.

char x;
cin.get(x);
cout <<endl;
switch(x)
{
case 49:
{
limit++;
getReceipt();
}
break;
case 50:
{
limit++;
getFamily();
}
break;
case 51:
limit = 5;
break;
default:
{
cout << "Wrong entry!, please TRY AGAIN" << endl<<endl;
fflush(stdin);
select();
}
}

I am using Ascii code, so if a user enter "1", it is 49 to the computer. But when I enter double or more number like "123", the switch command will only take "1" instead of "123". how can i solve this problem?
Last edited on
cin.get(x)
Extracts a single character from the stream and stores it in x.

more info : http://www.cplusplus.com/reference/iostream/istream/get/

Last edited on
cin.get(x) didn't work for me, it still has the same problem.
another way is, how to make the user only enter a single character or number?
The experts will tell you not to use ASCII code because its not portable across all platforms.
Try using the getline() function.
What do you want it to do when you enter "123"? Check each character one by one, or add the ascii values together, or what? Each digit is its own character, so you can't store the whole in a single char.
if you want to use the char values of the int.

use cin << x;

make this declaration

const int ONE = 49, TWO = 50, and so on... //not recommended if you have many cases...

then in your switch statement
1
2
3
4
5
switch(x){
    case ONE:
              ...
    case TWO:
}
I doing some error checking, how do i add the ascii values?
If i use "case ONE: and case TWO: " method, i will have these two problem,
1.case expression not constant.
2.switch statement contains 'default' but no 'case' labels.

For getline() function,
the error is
error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments.
I think need to use string for "x" but switch expression of type 'std::string' is illegal.
I doing some error checking, how do i add the ascii values?
Is that what you want to do? If the string is more than one character long, add them together?

I'm just trying to understand what you want this program to do. If you explain what it's for, it will probably be easier to find a solution.

1.case expression not constant.


did you include
 
const int ONE = 49, TWO = 50, THREE = 51;

in your variable declaration?

1
2
cout<<"enter x ";
cin << x; //this will solve your problem 
okay, erm, Actually I use "int x" at the start, but when I do error checking by entering a character, it will produce a infinite loop. So I change to "char x". The problem now is that I want to read "char x" as more then a single character while using switch case. So if I do error checking by entering two numbers, the Switch case should read it as two numbers instead of one number only and go to the default case in Switch. Thanks a lot for helping anyway.
erm, I am a beginner at c++ so correct me if i am wrong but can "cin" use "<<"? cause there is a lot of errors when I use it.
anyway, the way of using
"const int ONE = 49, TWO = 50, THREE = 51;" solve the case expression problem but the original problem is still there.
Ok, why not accept it as a string rather than a single char? Assuming valid input is always going to be a number, you can convert it to an integer.
1
2
3
4
5
6
7
8
9
10
#include <string>
#include <sstream>
string x;
int xint;
cin >> x;
//convert it to an integer
std::istringstream ss( x );
ss >> xint;
switch (xint) {
//etc 

EDIT: cin uses >>, not <<
With the example I've shown you, if you input "123" the value of xint should be 123, just to be clear.
Last edited on
it works,haha , really thanks a lot.
oops my bad cin uses >>.
No problem. Good luck on your project.
Topic archived. No new replies allowed.