#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
usingnamespace std;
int main()
{
system("clear");
int counter = 1;
int num1, num2, num3, uInput, length;
float right = 0.0, wrong = 0.0, percent;
loop:
cout<<"How many problems would you like to do: ";
cin>> length;
system("clear");
if (!cin.fail()) {
while (counter <= length) {
srand(time(0));
num1 = 1 + rand()%10;
num2 = 1 + rand()%10;
cout << setw(4) << num1 << endl;
cout << setw(2) <<"+ "<< num2 << endl;
cout << setw(4) <<"----"<< endl;
cout << " ";
cin >> uInput;
num3 = num1 + num2;
system("clear");
if (num3 == uInput) {
cout<<"Good job you are correct!\n";
right++;
}
else {
cout<<"Sorry, try again!\n";
wrong++;
}
++counter;
}
system("clear");
percent = (right/length) * 100.0;
cout<<"You got "<< right << " right and " << wrong<< " wrong" << endl;
cout<< setprecision(3);
cout<<"Score: %" << percent << "\n" << endl;
}
else{
cin.ignore(1000);
cin.clear();
cout<<"You have entered a letter, please enter a number.\n";
goto loop;
}
return 0;
}
You have entered a letter, please enter a number.
How many problems would you like to do:
The problem is in the checking for a letter. It goes to the else statement, the loops back to the top. but it turns into an endless loop that wont let me give any input. What the heck am I doing wrong?
I guess I wouldn't use a goto loop for this. General rule in programming is don't use goto if don't have to, which in this case you don't need to. The other thing I will note is don't use "system" like that. There are other ways to clear the screen. I could search through the forum for that info.
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
usingnamespace std;
int main()
{
int counter = 1;
int num1, num2, num3, uInput, length;
float right = 0.0, wrong = 0.0, percent;
bool myErrorOne = false;
bool myErrorTwo = false;
do
{
myErrorOne = false;
cout<<"How many problems would you like to do: ";
cin>> length;
if (!cin.fail())
{
while (counter <= length)
{
srand(time(0));
num1 = 1 + rand()%10;
num2 = 1 + rand()%10;
myErrorTwo = false;
do
{
myErrorTwo = false;
cin.ignore(); // you don't need any params on it.
cin.clear();
cout << setw(4) << num1 << endl;
cout << setw(2) <<"+ "<< num2 << endl;
cout << setw(4) <<"----"<< endl;
cout << " ";
cin >> uInput; // I was betting this one was causing the problems not the first one
if(cin.good())
{
num3 = num1 + num2;
if (num3 == uInput)
{
cout<<"Good job you are correct!\n";
right++;
}
else
{
cout<<"Sorry, try again!\n";
wrong++;
}
}// else good...
else
{
cout << "Your guess wasn't a number" << endl;
myErrorTwo = true;
}
} while(myErrorTwo)
++counter;
} // while
percent = (right/length) * 100.0;
cout<<"You got "<< right << " right and " << wrong<< " wrong" << endl;
cout<< setprecision(3);
cout<<"Score: %" << percent << "\n" << endl;
} // if fail
else
{
cin.ignore();
cin.clear();
cout<<"You have entered a letter, please enter a number.\n";
myErrorOne = true;
}
} while(myErrorOne); // do loop
return 0;
}
If you see my placements for things in the code, you might have a better way of handling what you are trying to do. After I went through code I don't think it was the first cin which was causing the problme but the second one, which you were not checking for validity, or clearing if it had an error. This would cause the endless loop.
How about replacing if (!cin.fail())
in line 23 with if (length>9)
Would that work?
Entering a letter is just a shortcut for an ascii code which could be represented as an integer. Therefore cin.fail() may never be triggered causing the infinite loop. Using any integer that is not one of the first 10 digits would mean that we are using an ascii character which is the condition that we want to avoid.