Wondering why program will not loop.

Hey, I'm new here and this is my first post. I have started on my journey down the path of learning the language of computers. I know a lot of tutorials say to mess around and try to create your own program with the knowledge that you learned so far. So that is exactly what I attempted. I was wondering why this program will not loop. It posts the question, accepts the answer, but when I go to try again/start new problem, I am not allowed. It automatically jumps to the end "Okay." is displayed and the program terminates.

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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
    char input[1];
    int x = rand() & 25;
    int y = rand() & 25;
    int a = x+y;
    int answer;
    cout<<"This program is a simple test. You must add 2 random numbers.\n\n";
    do {
        cout<<"The question is: " << x << "+" << y << "= ";
        cin>>answer;
        cout<<"\n\n";
        if (answer == a){
                cout<<"Correct!\n\n";
                cout<<"Would you like to try a new problem? (y/n): ";
                gets(input);
        }
            else if (answer != a) {
                cout<<"Incorrect!\n\n";
                cout<<"Would you like to try again? (y/n): ";
                gets(input);
            }
    } while (strcmp (input, "y") == 0);
    cout<<"Okay.";
    cin.get();
}
Change "y" to 'y'.
Last edited on
Line 10: You declare input as a single character array.
Lines 23,28: You call gets. gets stores characters until a new line is encountered and and then stores a terminating null writing past the single character you've allocated corrupting storage in your program. gets has no knowledge of how large input is. gets is deprecated and shouldn't be used.

Try the following instead:
1
2
3
4
5
  string input; 
...
  cin >> input;
...
} while (input == "y");

Here are some fixes:

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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    char input;
    int x;
    int y;
    int a;
    cout << "This program is a simple test. You must add 2 random numbers.\n";
    while(input != 'n')
    {
        srand((unsigned)time(0));
        x = rand() & 25;
        y = rand() & 25;
        a = x + y;
        int answer;
        cout << "The question is: " << x << "+" << y << "= ";
        cin >> answer;
        cout << "\n";
        if (answer == a)
        {
                cout << "Correct!\n";
                cout << "Would you like to try a new problem? (y/n): ";
                cin >> input;
        }
        else if (answer != a)
        {
            cout << "Incorrect!\n";
            cout << "The correct answer was: " << a << endl;
            cout << "Would you like to try again? (y/n): ";
            cin >> input;
        }
    }
    cout << "Okay.";
    return 0;
}
@iHutch - That won't work using strcmp. strcmp expects two const char * pointers. 'y' is an int.
Really? My bad.

Been a while since I actually used C strings.

For a good reason. ;-)
Thank you all for replying. I seem to have fixed it by changing input[1] to input, gets() to cin>>input, and while (strcmp (input, "y") == 0) to while (input == 'y')

@GreenLeaf, thank you for the input and fixes, I will study your code and figure out what everything does so that hopefully in the future I can avoid this mistake :)

Thanks everyone!
Thanks
The previous replies are all good.
I'm just suggesting an approach you could have taken to find the problem.

Your program doesn't loop therefore input never equals "y".

Your program doesn't stop and wait for a value for input to be entered so it must already have a value for input. What is it and where did it come from?

input may be a garbage value so initialize it to "y".

Program still misbehaves so gets(input) is changing input but to what?


The answer is because of the way extraction operator (>>) works (which BTW should be included in a beginners tutorial).

After cin>>answer; there is an end of line character left in the buffer (stream?).

gets(input); reads the end of line and puts '\0' into input.*

input is never "y" so your program never loops.


*I'm not sure if gets() uses same buffer (stream?). Feel free to enlighten me.

The code below works for me.

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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
    char input[2]; // change to 2 for correctness but this alone is not a fix
    int x = rand() & 25;
    int y = rand() & 25;
    int a = x+y;
    int answer;
    cout<<"This program is a simple test. You must add 2 random numbers.\n\n";
    do {
        cout<<"The question is: " << x << "+" << y << "= ";
        cin>>answer;
        cout<<"\n\n";
        gets(input);  // added line "eats" '\n' and the buffer is empty
        if (answer == a){
                cout<<"Correct!\n\n";
                cout<<"Would you like to try a new problem? (y/n): ";
                gets(input);
        }
            else if (answer != a) {
                cout<<"Incorrect!\n\n";
                cout<<"Would you like to try again? (y/n): ";
                gets(input);
            }
    } while (strcmp (input, "y") == 0);
    cout<<"Okay.";
    cin.get();
}






Last edited on
Topic archived. No new replies allowed.