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