Noob question

Hey guys, I'm trying to make the user only store numbers into the variable userInput. This is what I got so far, whenever the user enters something like 1a the program takes the 1, but I don't want that. I want it to prompt the user to enter a correct input, any ideas?

1
2
3
4
5
6
7
8
9
10
        bool fail;
	int userInput;
	do
	{
		cin >> userInput;
		fail = cin.fail();
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');

	} while (fail == true);
I wouldn't label this as a 'noob' question. If it was truly a 'noob' question then your thread would be flooded with answers and suggestions by now. This isn't something easy to achieve, I myself struggled for weeks back when I was learning C++ to bring about something like this.

Your attempt to accept only integer values from the user is good, but as you said, it wouldn't work quite well if the user entered a digit followed by random gibberish. So here's my suggestion, why don't you declare userInput as a string instead of integer. Then, what you could do, since a string is essentially an array of characters, you could loop through each individual element in the string and check whether it's a character or not. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
string text;
LABEL:cin >> text;
for (int i = 0; i < text.size(); ++i)
	{
		if (!(isdigit(text[i])))
		{
			cout << "Incorrect input. Please enter a proper integer value.\n";
			cin.clear();
			cin.ignore(256, '\n');
			goto LABEL; //NOTICE: This will go up 12 lines
		}
	}


The user will be prompted to enter another value should the loop detect a non-digit character. Then, you could do something like
 
int aNumber = stoi (text); //Converts text into integer. 


- OR -

 
float aNumber = stof (text); //Converts text into float. 


Hope you found this helpful.

EDIT: If you're one of those "we mustn't use goto as they lead to confusion" guys, then use break; and figure out the rest.
Last edited on
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
#include <iostream>
#include <string>

int get_int_from_stdin( int min_value, int max_value )
{
    std::cout << "enter an integer in [" << min_value << ',' << max_value << "]: " ;

    // accept the input into a string
    std::string input ;
    std::getline( std::cin, input ) ; // or std::cin >> input ;

    // try to convert the input string into an integer
    try
    {
        std::size_t number_of_chars_processed = 0 ;

        // will throw if there is no conversion,
        // or if the converted value would fall outside the range of values an int can hold.
        // http://en.cppreference.com/w/cpp/string/basic_string/stol
        const int n = std::stoi( input, std::addressof(number_of_chars_processed) ) ;

        // if the entire string was used for the conversion,
        // and if n is within [min_value,max_value], return n
        if( number_of_chars_processed == input.size() && n >= min_value && n <= max_value ) return n ;
    }

    catch( const std::exception& ) {} // error in conversion, try again

    std::cout << "invalid input. try again\n" ;
    return get_int_from_stdin( min_value, max_value ) ; // try again
}
@Uk Marine, thank you very much! This is a very good way to go about doing it indeed!
@JLBorges, thank you for the code!

Both of you guys are brilliant. =)
Topic archived. No new replies allowed.