+ 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.
1 Odpowiedź
+ 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