i want to enter q to quit the and y to play the game again but im not sure what code to use. any suggestion??

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
cout << "\t\t\tWELCOME TO THE GUESSING GAME\n" ;
cout << "\t\t\t**********\n";
//VARIABLES
srand (time (0)); //generate the different random number display
int random_num = (rand() % 20) + 1; //generate the random number between 1 and 20 AND the +1 to leave out the 0(zero) number
int guess_number; // the user's guess.
bool correct = false; //if the user has correctly guessed the number
int attempt = 0; //the number of attempt the user has guessed

while (attempt < 3)
{
cout << "A number has randomly generated between 1-20. And you have only " << attempt << " attempts.\n Guess the number: ";
cin >> guess_number;
if (guess_number >random_num){
cout<<"Your guess is higher try again\n";
}
else if (guess_number < random_num)
{
cout<<"Your guess is lower, try again\n";
}
else if(guess_number == random_num)
{
correct = true;
break; //stop the while loop
}
attempt++; //increment the number of attempts
}
if (correct == false)
{
cout<<"You are out of guesses so you're out of the game, the number was " << random_num << endl;
}
else if(correct == true){
cout<<"You found the number!!! CONGRATULATIONS\n";
}

system("pause");
return 0;
}
Last edited on
Hello batzot,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


A little reformat on your code and it looks like 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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    cout << "\t\t\tWELCOME TO THE GUESSING GAME\n";
    cout << "\t\t\t**********\n";

    //VARIABLES
    //srand(time(0)); //generate the different random number display
    srand(static_cast<unsigned int>(time(nullptr)));  // <--- The more  up to date way to use "srand".

    int random_num = (rand() % 20) + 1; //generate the random number between 1 and 20 AND the +1 to leave out the 0(zero) number
    int guess_number; // the user's guess.
    bool correct = false; //if the user has correctly guessed the number
    int attempt = 0; //the number of attempt the user has guessed

    while (attempt < 3)
    {
        cout << "A number has randomly generated between 1-20. And you have only " << attempt << " attempts.\n Guess the number: ";
        cin >> guess_number;

        if (guess_number > random_num)
        {
            cout << "Your guess is higher try again\n";
        }
        else if (guess_number < random_num)
        {
            cout << "Your guess is lower, try again\n";
        }
        else if (guess_number == random_num)
        {
            correct = true;
            break; //stop the while loop
        }
        attempt++; //increment the number of attempts
    }

    if (correct == false)
    {
        cout << "You are out of guesses so you're out of the game, the number was " << random_num << endl;
    }
    else if (correct == true)
    {
        cout << "You found the number!!! CONGRATULATIONS\n";
    }

    system("pause");

    return 0;
}

Right now you program would loop 3 times before it ends. What you need is a loop around the while loop and at the end of the while loop ask the user if they want to continue or quit. I would use a do/while loop for this part and base the while condition on either "q" or "y". Should the loop end then you could do any cleanup output that you may need before the program ends.

Andy
Hello Andy
Actually i am really new to this so i dont really know how to edit the code here but thanks for the helps.

But the code to enter any variable to quit or play again and the code will determine a new random variable is what i was looking for please if you have any other suggestion
Perhaps something like:

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

int main()
{
	std::srand(static_cast<unsigned char>(time(nullptr)));

	constexpr size_t maxguess {3};
	constexpr int maxnum {20};

	std::cout << "\t\t\tWELCOME TO THE GUESSING GAME\n";
	std::cout << "\t\t\t**********\n";

	for (bool again {true}; again; ) {
		const int random_num {(rand() % maxnum) + 1};
		bool correct {}; //if the user has correctly guessed the number

		std::cout << "A number has randomly generated between 1 - " << maxnum << ". You have " << maxguess << " attempts to guess.\n";

		for (size_t attempt {}, guess_number {}; !correct && attempt < maxguess; ++attempt) {
			std::cout << "Attempt " << attempt + 1 << ". What is your guess: ";
			std::cin >> guess_number;

			if (guess_number > random_num)
				std::cout << "Your guess is higher";
			else if (guess_number < random_num)
				std::cout << "Your guess is lower";
			else if (guess_number == random_num)
				correct = true;

			if (correct != true && attempt < maxguess - 1)
				std::cout << ". Try again";

			std::cout << '\n';
		}

		if (!correct)
			std::cout << "You are out of guesses so you're out of the game, the number was " << random_num << '\n';
		else
			std::cout << "You found the number!!! CONGRATULATIONS\n";

		char play {};
		std::cout << "Play again (y/n): ";
		std::cin >> play;

		again = play == 'y' || play == 'Y';
	}
}

heey seeeplus thanks i got it all correct and this is all i want but can you explain what that std :: cout and those code that start with std..
sorry for asking but i am really new to c++ code please
This how the do ... while can be used and which can be extended to make sure user provides a valid answer.

'std::' as a prefix is in lieu of 'using namespace std;'

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

#include <ctype.h> // <-- toupper()

using namespace std;

int main()
{
    char answer {};
    do{
        
        cout << "\t\t\tWELCOME TO THE GUESSING GAME\n";
        cout << "\t\t\t**********\n";
        
        //VARIABLES
        //srand(time(0)); //generate the different random number display
        srand(static_cast<unsigned int>(time(nullptr)));  // <--- The more  up to date way to use "srand".
        
        int random_num = (rand() % 20) + 1; //generate the random number between 1 and 20 AND the +1 to leave out the 0(zero) number
        int guess_number; // the user's guess.
        bool correct = false; //if the user has correctly guessed the number
        int attempt = 0; //the number of attempt the user has guessed
        
        while (attempt < 3)
        {
            cout << "A number has randomly generated between 1-20. And you have only " << attempt << " attempts.\n Guess the number: ";
            cin >> guess_number;
            
            if (guess_number > random_num)
            {
                cout << "Your guess is higher try again\n";
            }
            else if (guess_number < random_num)
            {
                cout << "Your guess is lower, try again\n";
            }
            else if (guess_number == random_num)
            {
                correct = true;
                break; //stop the while loop
            }
            attempt++; //increment the number of attempts
        }
        
        if (correct == false)
        {
            cout << "You are out of guesses so you're out of the game, the number was " << random_num << endl;
        }
        else if (correct == true)
        {
            cout << "You found the number!!! CONGRATULATIONS\n";
        }
    }while(
           cout << "Go again (Y/y)? " &&
           cin >> answer &&
           toupper(answer) == 'Y'
           );
    
    system("pause");
    
    return 0;
}
thank you!!
last question:
Please describe at least three test cases that will allow you to assess whether the program is correct. Which inputs should you choose, and what outputs would you expect? Answer in less than 300 words. Does your
program actually satisfy those test cases?
So what test cases would you use to check that the program is 'working'?
actually i am asking what are these test cases would be?
What do you think?
i think the for loop would be used.
what do you tinkk??
That's not a test case.
3 test cases could be:

1. Does a correct guess get recognized?
2. Does answering y/Y start a new game?
3. Are only 3 attempts allowed?
so thats the test cases?
LINE 17
for (size_t attempt {}, guess_number {}; !correct && attempt < maxguess; ++attempt) {

An explanation how this part of code work ??
What part don't you understand? It defines 2 variables, has a condition statement and an iteration expression. !correct means is correct == false.
so thats the test cases?

To answer your question, one way of looking at the 3 is, if your program doesn't do those 3 things, among others, then can you say it has fulfilled the requirements and is working as it was meant to?
Topic archived. No new replies allowed.