0
How do i correct this?[C++]
The problem in this program is that it is accepting inputs like "2a" or "2@" as well and it executes body of case 2 instead of the default statement.How do i correct this?I want every input other than 1 2 3 4 to execute the default statement which is the error int access_lvl; cin >> access_lvl; switch (access_lvl) { case (1): cout<<1; break; case (2): cout<<2; break; case (3): cout<<3; break; case (4): cout<<4; break; default: cout << "Please enter a number from the given choices." << endl; break;
5 Answers
+ 1
For example
Str=12, after strol(str,tmp) you get 12 and tmp will be empty
Str=12a after strol(str,tmp) you get 12 but tmp will contain symbol a
+ 1
String a;cin<<a;
int acess_lvl=atoi(a.cstr());
+ 1
sir can u provide some explanation to how this code works and where should i use it?
Thanks!
+ 1
Ou sorry i do not understand coreectly, but in real instead of atoi u need use strtol function.
You read some text via cin, then with help of function strtol conver string to number, this function take as second parameter some char*, function retuen number if it can convert it feom string, but if second param will be empty it means that string is fully convertet to num, if not than you have not number in cin
+ 1
thanks
have a good day