Modify the magic number program so that 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 with the number of magic numbers that they would like to guess at. An additional loop is necessary for this capability, and the loop to be implemented must be a for loop. The user should be given 5 guesses for each number and 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. before the program is completed, the user should be notified of their success rate given by the following formula:
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0)));
int randomNumber = rand() % 20 + 1;
int tries = 0;
int guess;
do
{
cout << “Take a shot at guessing a number between 1 and 20: n”;
cin >> guess;
++tries;
if (guess > randomNumber)
{
cout << “too high!n”;
}
else if (guess < randomNumber)
{
cout << ” too low!n”;
}
else
{
cout << “You won! ” << tries << ” tries!n”;
}
if (tries>=5)
{
cout << “You have failed. n”;
break;
}
} while (guess != randomNumber);
system(“pause”);
return 0;
}


0 comments