Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- using std::cout;
- using std::cin;
- using std::endl;
- using std::string;
- string isItANumber(string input="")
- {
- int count = 0;
- bool runAgain = true;
- while(runAgain)
- {
- if(count == 0)
- {
- for (int i = 0; i < input.length(); ++i) {
- if(!(isdigit(input[i])))
- {
- // Let runAgain stay true
- cout << "One of the characters is not a digit" << endl;
- break;
- }
- else
- {
- runAgain = false;
- }
- }
- count++;
- }
- else
- {
- cout << "Error! Please enter a number: " << endl;
- // Put the input in input
- getline(cin, input);
- // Check again
- for (int i = 0; i < input.length(); ++i) {
- if(!(isdigit(input[i])))
- {
- // Let runAgain stay true
- break;
- }
- else
- {
- runAgain = false;
- }
- }
- }
- }
- return input;
- }
- int main() {
- string input;
- cout << "Please enter a number: " << endl;
- getline(cin, input);
- isItANumber(input);
- return 0;
- }
Add Comment
Please, Sign In to add comment