-1

Below you can see a do while cycle for inputting strength stats, the program works fine but if in the section where you input the number (cin >> RemainingNumber) if you input a character of any kind it starts a infinite loop.

int RemainingStats;
int CurrentStats = 20;
do 
{
    //Cycle For Strength
    cout << "Please input a number between 0 and " << CurrentStats << " for strength: " << endl;
    cin >> RemainingStats;
    if (RemainingStats > CurrentStats || RemainingStats < 0)
    {
        cout << "Invalid input please input a number betwen 0 and " << CurrentStats << endl;
    }
    else if (RemainingStats < CurrentStats || RemainingStats > 0)
    {
        strength += RemainingStats;
        cout << "Strength: " << strength << endl;
    }
} while (RemainingStats > CurrentStats || RemainingStats < 0);

It starts showing you this part of the code constantly

{cout << "Please input a number between 0 and " << CurrentStats << " for strength: " << endl;
    cin >> RemainingStats;
    if (RemainingStats > CurrentStats || RemainingStats < 0)
    {
        cout << "Invalid input please input a number betwen 0 and " << CurrentStats << endl;
    }

except the fact that it does not let you cin RemainingStats and just continues to the if statement, if anyone can help and explain i will really apretiate it.

image of the result when you input the character

I added cin.clear() and cin.ignore() but after that the next cycle is the same its just for a different attribute and if you input a character it goes out of the do while loop which makes no sense since the while statement is true

    do
{
    //Cycle For Agility
    cout << "Please input a number between 0 and " << CurrentStats << " for agility: " << endl;
    cin >> RemainingStats;
    if (RemainingStats > CurrentStats || RemainingStats < 0)
    {
        cout << "Invalid input please input a number betwen 0 and " << CurrentStats << endl;
        cin.clear();
        cin.ignore();
    }
    else
    {
        agility += RemainingStats;
        cout << "Agility: " << agility << endl;
        CurrentStats -= RemainingStats;
    }
} while (RemainingStats > CurrentStats || RemainingStats < 0);

if you input a character you will get this

Image of the console

which is the code right after that a do while switch statement it just switches the RemainingStats with the Response variable

do
{
    cin >> Response;
    system("cls");
    switch (Response)
    {

and its a infinite switch default result over and over, this i fix with a cin.clear before the cin >> Response but it still exits the loop and just ads the previous result which was inputed for strength, but strength works fine even with a char input because i added this change

    do 
{
    //Cycle For Strength
    cout << "Please input a number between 0 and " << CurrentStats << " for strength: " << endl;
    cin >> RemainingStats;
    if (RemainingStats > CurrentStats || RemainingStats < 0)
    {
        cout << "Invalid input please input a number betwen 0 and " << CurrentStats << endl;
        cin.clear();
        cin.ignore();
    }
    else if (RemainingStats < CurrentStats || RemainingStats > 0)
    {
        strength += RemainingStats;
        cout << "Strength: " << strength << endl;
        CurrentStats -= RemainingStats;
    }
} while (RemainingStats > CurrentStats || RemainingStats < 0);

Please help me i dont understand at all why the code is acting like this

SAVIOR1111
  • 11
  • 1
  • The "bad" input is wedged in the pipes. – Eljay Feb 22 '18 at 22:12
  • Because cin is expecting an integer, if it encounters a non-integer character in its stream, it sets the fail flag. You cannot use cin again until this flag is cleared. You should call cin.clear() when a invalid characters is entered. You also should clear the cin input buffer, as well (cin.ignore(n)). – sizzzzlerz Feb 22 '18 at 22:22

1 Answers1

1

See std::basic_istream::operator>>

If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified and failbit is set.

Since you entered any other character the read fails. To compensate for this, you must either abort the loop on invalid input, or read into a string and do the conversion yourself.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Please check i edited the post now when i add cin.clear and ignore it works for the first loop but on the second loop it just goes out of it when it shouldn't since the while statement is true. – SAVIOR1111 Feb 26 '18 at 17:05