#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";
}
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:
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace 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<unsignedint>(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";
}
elseif (guess_number < random_num)
{
cout << "Your guess is lower, try again\n";
}
elseif (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;
}
elseif (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.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
std::srand(static_cast<unsignedchar>(time(nullptr)));
constexpr size_t maxguess {3};
constexprint maxnum {20};
std::cout << "\t\t\tWELCOME TO THE GUESSING GAME\n";
std::cout << "\t\t\t**********\n";
for (bool again {true}; again; ) {
constint 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";
elseif (guess_number < random_num)
std::cout << "Your guess is lower";
elseif (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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <ctype.h> // <-- toupper()usingnamespace 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<unsignedint>(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";
}
elseif (guess_number < random_num)
{
cout << "Your guess is lower, try again\n";
}
elseif (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;
}
elseif (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?
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?