the user can guess at more than one magic number where the magic number is an integer between 1 and 20. When the program is run, the user should be prompted if they would like to guess at a magic number and continue to be prompted as such after they have guessed a magic number correctly. A while loop is necessary for this capability, The user should be given as many guesses as necessary for each number until they guess the number correctly.
A do-while loop must be implemented for this part of the program. They should also be responded with a clue after each guess on whether the guess is too high or low. The program should also make sure that if the user enters an invalid guess at the magic number (less than 1 or greater than 20), then the user should be prompted with an invalid guess message and not be penalized for a guess attempt. A while loop must be implemented for this part of the program. After the user guesses the number correctly, they should be graded on their effort. If they guess the number correctly within 3 guesses, rate them as "Excellent". If they guess the number correctly within 5 guesses, rate them as "Good". If they guess the number correctly within 7 guesses, rate them as "Average". If they guess the number correctly with more than 7 guesses, rate them as "Poor". Use either an if-else-if ladder or a switch block to rate the user.
Could somebody pls help? Thank you so much.
#include <iostream>
#include <cstdlib>
#include<ctime>
#include<cstdio>
usingnamespace std;
int main () {
srand(time(NULL));
int numSuccess = 0;
char answer='n';
int magic = rand() % 20 + 1;
do{
int guess,magic;
cout<<"Enter your guess."<<"\n\n";
cin>>guess;
if (guess == magic)
cout <<" ** Right!! **";
else
{
cout <<".... sorry - you guessed incorrectly";
if(guess> magic)
cout<<"\n Your guess is too high\n";
if(guess<magic)
cout<<"\n Your guess is too low\n";
}
cout<<"Do you want to try again?(y/n)"<<"\n\n";
cin>>answer;
}while(answer=='y');
cout<<"The magic number is"<<magic<<"\b\b";
}
#include <iostream>
#include <cstdlib> //required for random number algorithm
#include <ctime> // time library for random number seed
int main ()
{
int number, guess = 0, starter = 0;
unsigned seed;
std::cout << "\nThis program is a High-Low game. \n"
<< "The computer will generate a number between 0 and 50, and you have to guess the number. \n"
<< "The program will tell you if your guess is high or low.\n"
<<"\nEnter 1 to start playing, or 2 to quit the game. \n: ";
while (1)
{
seed = time (0);
srand (seed);
number = rand() % 50;
std::cin >> starter;
if (starter == 1)
{
std::cout << "Enter a number between 0 and 51. \n: ";
while (starter == 1)
{
std::cin >> guess;
if (std::cin.fail())
{
std::cout << "Invalid value. \nTry again, and enter a number. \n: ";
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
continue;
}
elseif (guess > 50 || guess < 0)
{
std::cout << "Invalid. Entry must be between 0 and 50. Try again. \n: ";
}
elseif (guess == number)
{
break;
}
elseif (guess < number)
{
std::cout << "Too low. Try another number. \n: ";
}
elseif (guess > number)
{
std::cout << "Too high. Try another number. \n: ";
}
}
}
else
{
break;
}
std::cout << "You guessed the number! \n"
<< "Play again? \n"
<< "Enter 1 to start playing, or 2 to quit the game. \n: ";
}
std::cout << "Thanks for playing! \n";
return 0;
}
The user can guess at more than one magic number where the magic number is an integer between 1 and 20.
1. When the program is run, the user should be prompted if they would like to guess at a magic number and continue to be
prompted as such after they have guessed a magic number correctly.
2. A while loop is necessary for this capability,
The user should be given as many guesses as necessary for each number until they guess the number correctly.
3. A do-while loop must be implemented for this part of the program.
4. They should also be responded with a clue after each guess on whether the guess is too high or low.
5. The program should also make sure that if the user enters an invalid guess at the magic number (less than 1 or greater than 20),
then the user should be prompted with an invalid guess message and not be penalized for a guess attempt.
A while loop must be implemented for this part of the program.
6. After the user guesses the number correctly, they should be graded on their effort.
If they guess the number correctly within 3 guesses, rate them as "Excellent". If they guess the number correctly within 5 guesses,
rate them as "Good". If they guess the number correctly within 7 guesses, rate them as "Average". If they guess the number correctly
with more than 7 guesses, rate them as "Poor".
7. Use either an if-else-if ladder or a switch block to rate the user.
I find that when you break it up and number it it helps when planning and writing the program.
So the first thing I noticed is that you have not done #1.
That is what you should start with. You do not need any kind of loop at first just get it working. Write the code, compile often and test when it compiles. When you knock out the easy parts first the rest tends to come easier.
Your code has some blank where they are not needed and other places where they are. I added some comments and made a couple of changes. Compare this to your code and see which is easier to read.
#include <iostream>
#include <cstdlib> // <--- For "rand()" and "srand()".
#include<ctime>
//#include<cstdio> // <--- Covered under "<iostream>". May also be included by "<iostream>".
usingnamespace std; // <--- Best not to use.
int main()
{
//srand(time(NULL));
srand(static_cast<unsignedint>(time(nullptr))); // <--- Updated form to use.
int numSuccess{};
char answer{ 'n' };
int magic = rand() % 20 + 1;
do
{
int guess{}, magic{};
cout << "Enter your guess." << "\n\n";
cin >> guess;
if (guess == magic)
cout << " ** Right!! **";
else
{
cout << ".... sorry - you guessed incorrectly";
if (guess > magic)
cout << "\n Your guess is too high\n";
if (guess < magic)
cout << "\n Your guess is too low\n";
}
cout << "Do you want to try again?(y/n)" << "\n\n";
cin >> answer;
} while (answer == 'y'); // <--- What happens if the user types "Y"?
//while (std::tolower(answer) == 'y'); // <--- Requires header file "<cctype>".
cout << "The magic number is " << magic << "\n"; // <--- Changed. The "\b" has no real use here. It just moves the cursor back 2 spaces.
}
I may not be as fast as max, but I will see what I can come up with.
Put most of "main" in comments for now and work on #1 first. There is no reason to delete code that you can still use later.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
int getInt(const std::string& prm)
{
int i {};
while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
std::cout << "Not an integer\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return i;
}
int start()
{
int starter {};
while ((starter = getInt("Enter 1 to play, or 2 to quit: ")) != 1 && starter != 2)
std::cout << "Entry must be 1 or 2. Try again\n";
return starter;
}
int main()
{
std::srand(static_cast<unsignedint>(std::time(nullptr)));
constexprint mino {1}, maxno {20};
const std::string prmpt {"Enter a number between " + std::to_string(mino) + " and " + std::to_string(maxno) + ": "};
std::cout << "\nThis program is a High-Low game. \n"
<< "The computer will generate a number between " << mino << " and " << maxno << " inclusive, and you have to guess the number. \n"
<< "The program will tell you if your guess is high or low.\n";
for (int starter {1}; (starter = start()) == 1; ) {
constauto number {rand() % 20 + 1};
int guess {}, noguess {};
do {
while ((guess = getInt(prmpt)) < mino || guess > maxno)
std::cout << "Invalid. Entry must be between " << mino << " and " << maxno << ". Try again.\n";
if (guess < number)
std::cout << "Too low. Try another number. \n";
elseif (guess > number)
std::cout << "Too high. Try another number. \n";
++noguess;
} while (guess != number);
std::cout << "You guessed the number in " << noguess << " guesses!\nPlay again?\n";
}
std::cout << "Thanks for playing!\n";
}
#include <iostream>
#include <string>
#include <cstdlib> // <--- For "rand()" and "srand()".
#include <ctime>
#include <cctype> // <--- For "std::tolower() and std::toupper()" + others.
int main()
{
srand(static_cast<unsignedint>(time(nullptr))); // <--- Updated form to use.
int numSuccess{}; // <--- I have no idea what this is for. Not only did you not use this the name does not easy to understand.
char answer{}; // <--- Just needs to be initialized. Does not need a value.
int magic = rand() % 20 + 1;
std::cout <<
"\n "
<< std::string(4, ' ') <<
" GUESS THE MAGIC NUMBER\n "
<< std::string(32, '-') << "\n""\n Would you lile to play the game?\n"" Enter (Y or N): ";
std::cin >> answer;
if (std::toupper(answer) == 'N')
{
//break; // <--- For later use when you add the while loop.
}
std::cout <<
"\n"" Add instructions here\n";
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
I would finish this up then you can add a while loop around lines 15 - 31 something. And when you add the while loop be sure to uncomment line 26.
This will cover steps 1 and 2 then you can start into step 3.