Reset turn counter in guess number game

Hi, I'm creating a guess the number game for my CS1 class. I've got most of it working how i want, however when it starts over it only gives one guess, but i can't figure out how to reset the turn counter. Also if anyone has any suggestions on where to look for how to store; games played, wins, and losses would be much appreciated. thanks in advance! // correction I figured out how to store and print games wins and losses. still could help resetting my turn counter so that i get 6 guesses each game.

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* 
 This program is designed to play guess the number with a single user, using a random number generator.  
Providing feed back to the user as they input guesses between 1-20, directing the user to guess higher or lower.
Each round the user will be given 6 chances to guess the number, if the user inputs the correct number with in 6 
tries he/she wins.  The program will store and output the # of games played, # of wins, and # of losses when the user
quits the game.

1. Create a function genNumber() that generates a random number, using a different seed each time it generates a number.
2. Create a main function that:
	a. Asks players name and greets the player
	b. Calls function genNumber()
	c. asks user to guess a number and allows user to input number.
	d. determines whether user guessed correctly or not.
	e. if user guessed correctly user wins
	f. if user guessed incorrectly program tells user higher or lower and asks user to guess again.
	g. continues through 6th guess.
	h. if user does not guess within 6 guesses the user loses.
	i. create loop that asks user if user would like to play again. //FIXME //fixed
	j. create function getStats() to store stats of games played, wins and losses //fix me research
	k. if user quits call getStats() and outputs them to the user. //FIXME still dont know how to return 
	wins losses games played
3. create test function
	a. create assertions to ensure guess number program works. //FIXME

*/

#include <iostream>
#include <ctime> //for time, to create a new seed each time a random number is generated to increase illusion of randomness
#include <cstdlib> // for srand and rand
#include <string>

using namespace std;

void genNumber() {
	
srand (time(NULL)); //initializes sequenced random number generator thats seed changes based on time.

}

int main()
{
	//int again; // trying char/
	string name;
	int guess, num, turn;
	genNumber();
	turn = 0;
	char again;
	num = (rand() % 20) + 1; //generates a random number between 1-20
	//cout << num << endl;  checking to see if calling function and rng works.
	cout << "Hi, what is your name?" << endl;
	cin >> name;
	cout << "Hi " << name << ", Let's play a game!!" << endl;
	while (true) { //fix me loop not working.  //fixed
	

		while (true) { 
			cout << "Enter a number between 1 and 20 (" << 5 - turn << " tries left): ";
			cin >> guess;
			cin.ignore();

			// Check is tries are taken up.
			if (turn >= 5) {
				break;
			}

			// Check number.
			if (guess > num) {
				cout << "you guessed to high!" << endl;
			}
			else if (guess < num) {
				cout << "you guessed to low" << endl;
			}
			else {
				break;
			}

			// If not number, increment tries.
			turn++;
		}

		// Check for tries.
		if (turn >= 5) {
			cout << "Sorry, I win this time!" << endl;
		}
		else {
			// Or, user won.
			cout << "You're a great guesser, you win! " << endl;
			
		}

		while (true) { // play again loop
			cout << "Want to keep playing? (Y/N)? ";
			cin >> again;
			cin.ignore();

	
			if (again == 'n' || again == 'N' || again == 'y' || again == 'Y') {
				system("cls"); // trying to figure out how to reset my turns.
				break;
			}
			else {
				//cout << "Please enter \'Y\' or \'N\'...\n";
			}
		}

		// Check user's input and run again or exit;
		if (again == 'n' || again == 'N') {
			cout << "Thank you for playing!";
			break;
		}
		else {
		
		}
	}

	
	cout << "Any Key to exit" << endl;

		//} while loop not worrking

	

		cin.ignore(1000, '\n');
		cin.get();
		return num;


	}
Last edited on
To set the value of turn to zero, you can use this code:
turn = 0;
closed account (SECMoG1T)
something close to this;

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
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <ctime> //for time, to create a new seed each time a random number is generated to increase illusion of randomness
#include <cstdlib> // for srand and rand
#include <string>

using namespace std;

int genNumber()
{
    srand (time(NULL)); //initializes sequenced random number generator thats seed changes based on time.
    return (rand() % 20) + 1;
}

int totalgames=0, wins=0, losses=0;

void getStats()
{
    std::cout<<"\nTotal number of games played: "<<totalgames<<"\n";
    std::cout<<"Total number of games won   : "<<wins<<"\n";
    std::cout<<"Total number of games lost  : "<<losses<<"\n";
}

bool validGuessNum(int guessnum)
{
    if(guessnum<0 || guessnum>20)
        return false;
    return true;
}

int main()
{
    string name;
    int userguess, randnum, trials=0;

    bool canplay = true;

    cout << "Hi, what is your name?" << endl;
    std::getline(std::cin,name);

    cout << "Hi " << name << ", Let's play a game!!" << endl;

    while(canplay)
    {
        while(trials < 6)
        {
            ++totalgames;
            randnum = genNumber();
            cout << "Enter a number between 1 and 20 (" << 5 - trials << " tries left): ";
            std::cin>>userguess;

            while(!std::cin || !validGuessNum(userguess))
            {
                if(!std::cin)
                {
                    std::cin.clear();
                    std::cin.ignore(1000,'\n');
                }
                std::cout<<"sorry your guess must lie btwn 1-20 : ";std::cin>>userguess;
            }

            if(randnum == userguess)
            {
                ++wins;
                std::cout<<"you won this round, the # is <"<<randnum<<"\n";
            }

            else
            {
                ++losses;
                if( userguess < randnum)
                    std::cout<<"you've guessed too low: ";

                else
                    std::cout<<"you've quessed too high: ";

                std::cout<<" the correct # is <"<<randnum<<"> \n";;
            }

          ++trials;
        }

        trials =0;///reset this vars
        char choice;

        cout << "\nWant to keep playing? press (Y/y), any other key to exit: ";
        std::cin>>choice;


        if(choice != 'Y' && choice != 'y')
            canplay = false;
    }

    getStats();
}
Topic archived. No new replies allowed.