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
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