Am i better off doing this...

Ok so in my code i use

cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

pretty much everywhere there is input, i dont really have a choice so i was wondering if i should put

cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

in a function and just call it wherever i need it in my code?

For example here is where i use it all the time and more places in my code.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
void Player::gameStart()
{
    bool enterNameLoopEnd = false;
    int satisfied;

    cout << "Welcome to the game we are pleased to see a new" << endl;
    cout << "recruit join our ranks. We have a special mission" << endl;
    cout << "for you, we need you to hack into bank accounts all" << endl;
    cout << "across the world and take them for everything they have!" << endl;
    cout << "Now please enter your name\n" << endl;
    cout << "Name: ";

    while(enterNameLoopEnd != true)
    {
        getline(cin, name);

        if(name.empty())
        {
            cout << "You must enter a name!" << endl;
            cout << "Name: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

        cout << "Are you satisfied with the name " << name << "?" << endl;
        cout << "1) Yes" << endl;
        cout << "2) No" << endl;
        cin >> satisfied;

        //The code below is error handling so if the user enters an undesired input
        //then the program wont flip out.
        if(satisfied == 1)
        {
            break;
        }
        else if(satisfied == 2)
        {
            cout << "Ok please enter the name you wish to use\n" << endl;
            cout << "Name: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        else
        {
            cout << "Invalid response please enter the name you wish to use\n" << endl;
            cout << "Name: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }

    cout << "\nOk " << name << " were going to start you" << endl;
    cout << "off with $1200.\n" << endl;

    cout << "Dont forget to save from the main menu or else your progress" << endl;
    cout << "will not be saved!!\n" << endl;
    cin.get();
    cin.clear();

    mainGame();
}
Last edited on
I really don't see anything stopping you from doing that function.
Topic archived. No new replies allowed.