Banning non-numbers for user input and letting the user retry an input

How can I "ban" letters from being used while only letting numbers to get through? Inputting letters causes my program to crash. And if they were to input a letter or foreign character, how can I let them retry?
Last edited on
To avoid screwing up your one and only source of input (std::cin), you should read whole lines at a time and then deal with them after that:
1
2
3
4
5
6
7
8
int x;
std::string line;
while((std::cout << "Enter a whole number: ") //prompt for input
   && std::getline(std::cin, line) //read whole line (almost never fails)
   && !(std::istringstream{line} >> x)) //validate input
{
    std::cout << "Invalid input, please try again." << std::endl;
}
Don't forget to #include <sstream>
Last edited on
Thanks! I've been trying to figure that out for a while now. I do have another question. How would I be able to manipulate the number that the user enters in? The tip you gave me worked, I just don't know how to transfer it to this because it ends after I enter a number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    double N1 = 0;
    double N2 = 0;
    double quotient = 0;

    cout<< "Enter first number:   "; // user input
    cin >> N1;
    cout<< "Enter second number:  ";
    cin >> N2;

    quotient = N1 / N2; // formula


   if (N2 == 0) // if divided by zero
    {
        cout << "Quotient does not exist" << endl;
    }
    else
    {
       cout << "Quotient is " << quotient << endl;
    }

return 0;
}

Last edited on
You could take the code from above and put it into a function, then just call the function whenever you want input.
Sorry, I found that confusing. Could you further explain what you mean? I'm fairly new at this. I think only allowing digits 0-9, ".", and "-". would work but I think I'm getting too far ahead of myself for someone who just started.
Don't worry about which characters make up a number - C++ handles that for you.
1
2
3
4
5
6
7
8
9
10
11
double ask_for_number(std::string const &prompt)
{
    double x;
    std::string line;
    while((std::cout << prompt << ": ") //prompt for input
       && std::getline(std::cin, line) //read whole line (almost never fails)
       && !(std::istringstream{line} >> x)) //validate input
    {
        std::cout << "Invalid input, please try again." << std::endl;
    }
}
1
2
int n1 = ask_for_number("Enter a number: ");
int n2 = ask_for_number("Enter another number: ");
Thanks again, LB! I couldn't get it to work properly with the code that you provided me so I made some minor changes to it. Is my code proper still, or did I just get lucky?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    double quotient = 0;

    double ask_for_number_1;
    string line;

    while((cout  << "Enter your first number: ") //prompt for input
       && getline(cin, line) //read whole line (almost never fails)
       && !(istringstream{line} >> ask_for_number_1)) //validate input
    {
        cout << "Invalid input, please try again." << endl;
    }
    double ask_for_number_2;
    string line2;
    while((cout  << "Enter your second number: ") 
       && getline(cin, line)
       && !(istringstream{line} >> ask_for_number_2))
    {
        cout << "Invalid input, please try again." << endl;
    }


    double N1 = ask_for_number_1;
    double N2 = ask_for_number_2;




    quotient = N1 / N2; // formula


   if (N2 == 0) // if divided by zero
    {
        cout << "Quotient does not exist" << endl;
    }
    else
    {
       cout << "Quotient is " << quotient << endl;
    }

return 0;
}
Last edited on
Oops, the reason my code didn't work was because I forgot the return x; at the end:
http://ideone.com/8SizAy

You don't need your line2 variable on line 19 - the point of the line variable is just to hold the most recent line of input, so re-using it is intended.

Also, why not just call the variables N1 and N2 from the start? Then you wouldn't need lines 28 and 29.
Topic archived. No new replies allowed.