Looping

I need some help with this code. Im just starting to learn loops and I can't seem to get this right.


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
62
63
64
65
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>


using namespace 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.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>


using namespace 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.
Last edited on
I tried this code but i still end up in an endless loop at the first question after entering a letter. everything works but the validating user input.
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.
Thats a good thought, but say i wanted to do 10 or 15 problems. This code is really confusing me why its not working.
Topic archived. No new replies allowed.