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;

6th Jan 2021, 6:36 AM
Dex
Dex - avatar
5 ответов
+ 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
6th Jan 2021, 7:22 AM
george
george - avatar
+ 1
String a;cin<<a; int acess_lvl=atoi(a.cstr());
6th Jan 2021, 7:09 AM
george
george - avatar
+ 1
sir can u provide some explanation to how this code works and where should i use it? Thanks!
6th Jan 2021, 7:12 AM
Dex
Dex - avatar
+ 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
6th Jan 2021, 7:20 AM
george
george - avatar
+ 1
thanks have a good day
6th Jan 2021, 7:22 AM
Dex
Dex - avatar