+ 3

How to create a code which identifies whether the user input is composed of the only characters specified in the program?

Like for example, the code dictates that the input value (a string) can only have characters +,-,*,/,% and 1,2....,9 in this sequence only, if the user input is not of this form, then the code will not run. input - ace..did outputs an error. input - %4 runs the code.

30th Apr 2018, 8:20 AM
scientist
scientist - avatar
1 Antwort
+ 3
You can use a code like this std::string input; cin >> input; std::string in = input; if(in.find('+') != in.npos) in.erase(std::remove(in.begin(), in.end(), '+'), in.end()); //do this for all the characters you want in string if(in.empty) runcode else don't run code you didn't say what language you want I wrote in c++ but you can convert this code into any other language
30th Apr 2018, 10:09 AM
Ias0601
Ias0601 - avatar