#include <iostream>  
 #include <string>  
   
 std::string input;  
   
 std::getline(std::cin, input);  
   
 if(!(input.find_first_not_of("0123456789") == std::string::npos)){  
   cout << "ERROR" << endl;  
   cin.clear();  
 }  
   
 int entered = stoi(input);  
25/10/2019
[C++] Validate string contains only digits and convert it to int
While playing with the Rock, Paper, Scissors game, I had to validate the user input to check only digits where entered at a particular moment. Turns out, C++ does not have a very straightforward way of doing so, but I found this solution works well:
Subscribe to:
Post Comments (Atom)
 
 
No comments:
Post a Comment
With great power comes great responsibility