How to keep console from spazzing out when wrong input is entered

Im making a game and i cant seem to figure out how to get the input to keep letting me input if the user enters something wrong it just infinitly loops, i have tried while loops, do while loops everything and i cant get it to work.

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
int Player::mainGame()
{
    vector<string> accountNumberList;

    int choice;

    cout << "Main game" << endl;
    cout << "1) Store" << endl;
    cout << "2) Bank Account" << endl;
    cout << "3) Find bank" << endl;
    cout << "4) Save Game" << endl;
    cout << "5) Exit Game" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            shop();
            break;
        case 2:
            bankAccount();
            break;
        case 3:
            findBank();
            break;
        case 4:
            save();
            break;
        default:
            cout << "Not a valid input" << endl;

        }
}
Last edited on
awesome i got it, thanks.
actually i have a question, i tried this exact thing except i didnt have numeric_limits<streamsize>::max() in ignore(), i had (500, '\n') why didnt it work then?
The problem with this sort of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <limits>
using namespace std;

int main()
{
    cout << "Please, enter a number\n";

    int n = 0;

    while( !(cin >> n) )
    {
        cout << "You need to enter a number\n";
        cin.clear(); // reset the error flags
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    cout << "Thank you for entering the number " << n << '\n';

    return 0;
}

is that it will accept strings which begin with a number but then continue with other characters, e.g.

Please, enter a number
42nd Street
Thank you for entering the number 42

and

Please, enter a number
99 red balloons
Thank you for entering the number 99

If you want just a number, you need something more like this [1]

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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    cout << "Please, enter a number\n";

    int  n       = 0;
    bool notDone = true;

    while (notDone)
    {
        std::string line;
        std::getline(std::cin, line);

        std::istringstream is(line);

        char dummy;
        if (!(is >> n) || (is >> std::ws && is.get(dummy)))
            std::cout << "You need to enter just a number\n";
        else
            notDone = false ;
    }

    cout << "Thank you for entering the number " << n << '\n';

    return 0;
}


Please, enter a number
99 red balloons
You need to enter just a number
99
Thank you for entering the number 99


Andy

[1] A basic take off of cire's templated function here:

Trying to limit input to int type
http://www.cplusplus.com/forum/beginner/108849/#msg592118
Last edited on
"actually i have a question, i tried this exact thing except i didnt have numeric_limits<streamsize>::max() in ignore(), i had (500, '\n') why didnt it work then?"
In many situations, there would be no discernible difference between the use of numeric_limits<streamsize>::max() instead of 500.

It is simply that the former is a complete solution which should always work (by ignoring an unlimited number of characters) while the latter would fail if the user had typed in more than 500 characters before pressing enter.

hmm, so i guess numeric_limits<streamsize>::max() is the better solution then. is there any problem with using it all the time? also how do i see whats left in the input buffer? and lastly, how can i create a simple input buffer?
Also should i use cin.clear() and ignore at the end of every function that takes input to clear the input buffer?
Topic archived. No new replies allowed.