Help me understand my own program.

Hey everyone this is my first post on this forum as I have just recently got into learning programming. I have started to make some little projects with the small amount of knowledge I have. I don't quite get all of the intricacies of different commands and whatnot and so I sometime find ways to make a program work without fully understanding why.

I was making this number guessing game where you guess a random number and you have 8 chances to guess or else it says you lose. It works just like I want it to, but I had trouble with the for statement and the i's. I do not quite get why the i in the for statement had to be a 14 (after experimentation). Also, I threw that do while statement in there with almost no knowledge of what that would actually do and again I do not know why, but when i < 11 it got me to my 8 chances, too.

Please help me figure this out or revise it and do not get too technical, I really have not gotten the hang of this yet. Any help is greatly appreciated!

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

int main ()
{
    int guess, p, i = 0;

    srand(time(NULL));   //set seed

    p = rand() % 100;

    cout << "You have 8 chances to guess what number I am thinking of between 0 and 100: ";
    cin >> guess;
    cout << endl;
do
{
    for(i = 0;i < 14;i++)
    {
       if (guess == p)
    {
        cout << "You got it! ";
        cout << "The number I guessed was: " << p << endl;
        return 0;
    }
        if (guess < p && guess >= 0)
        {
            cout << "Sorry that is incorrect. Try higher: ";
            cin >> guess;
            cout << endl;
            i++;
        }
        if (guess > p && guess <= 100)
        {
            cout << "Sorry that is incorrect. Try lower: ";
            cin >> guess;
            cout << endl;
            i++;
        }
        if (guess < 0 || guess > 100)
        {
            cout << "Sorry that number is invalid. Please select between 0 and 100: ";
            cin >> guess;
            cout << endl;
        }

    }
}
   while (i < 11);


    cout << "You ran out of chances! You lose. Please try again." << endl;
    cout << "The number was: " << p << endl;


return 0;
}
Hmmm... Where to begin?

You don't need both a for loop and a while loop for this.
- you should either use the while loop with i<8 (not 11 as you have it) or the for loop with i<8 (not i<14)

If you use the for loop, you shouldn't be doing i++ anywhere other than the one in the loop definition itself.

If you use the while loop, you only need a single i++ just before the end of the while loop.

This site should help explain how to use for and while loops:
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
Thanks! Alright I see what you're saying and I changed it to only a for loop. That was a lot easier than I thought. The problem is I think I understand a topic and then as soon as it is a new example I have no clue how to implement it. That seems to be my problem with every code.
Topic archived. No new replies allowed.