In: Computer Science
Here is my assignment:
Write a program that generates a random number between 1 and 50 and asks the user to guess it. As a hint, it tells the user how many divisors it has, (excluding 1 and the number itself). Each time the user makes a wrong guess, it displays "Wrong" and says if the guess was too low or too high, until the user gets it right, in which case it says "Right" and says how many trials it took to guess it. Then, the program asks the user if he or she wants to continue. If the answer is 'y' or 'Y', it generates another number between 1 and 50 and repeats the game. If not, it prints the average of all trials with one decimal place and ends.
I've got the basic code, but i'm stuck on if I answer 'Y' for it to repeat the game and how to code the hint (the divisor)
Example:
I've picked a number between 1 and 50 that has 0 divisor(s).
My work so far:
#include <iostream>
#include <cstdlib> //needed to use srand and rand
#include <ctime> //needed to use time
using namespace std;
int main()
{
int random_number, guess, number_of_trys;
char go_again;
bool win = false;
unsigned seed;
seed = time(0);
srand(seed);
random_number = 1 + rand() % 50;
number_of_trys = 0;
cout << "I'have picked a number between 1 and
50. Guess the Number!" << endl;
cin >> guess;
cout << random_number;
do // this will loop while play-again
{
while (!win)
{
if (guess <
random_number) //if the guess is lower than the random number
{
cout << "Too Low! Guess Again." <<
endl;
cin >> guess;
number_of_trys++;
}
else if (guess
> random_number) //if the guess is higher than the random
number
{
cout << "Too High, Guess Again!" <<
endl;
cin >> guess;
number_of_trys++;
}
else
{
number_of_trys++;
cout << "That's Right!!!! It took you "
<< number_of_trys << " trys." << endl;
win = true;
}
} //end while
cout << "Play again[Y/N]:
";
cin >> go_again;
} while (go_again == 'Y'); //end do while
return 0;
}
I replace the do-while loops with simple while loops and the
code should be clear enough to understand. Ask in the comment
section if you don't understand any part.
Code:
#include <iostream>
#include <cstdlib> //needed to use srand and rand
#include <ctime> //needed to use time
using namespace std;
int main()
{
int random_number, guess, number_of_trys;
char go_again;
unsigned seed;
seed = time(0);
srand(seed);
float total_tries=0,total_rounds=0;
while(1)
{
random_number = 1 + rand() % 50;
number_of_trys = 0;
total_rounds++;
cout << "I'have picked a number between 1 and 50. Guess the Number!" << endl;
cin >> guess;
while(1)
{
if (guess < random_number) //if the guess is lower than the random number
{
cout << "Too Low! Guess Again." << endl;
cin >> guess;
number_of_trys++;
}
else if (guess > random_number) //if the guess is higher than the random number
{
cout << "Too High, Guess Again!" << endl;
cin >> guess;
number_of_trys++;
}
else
{
number_of_trys++;
cout << "That's Right!!!! It took you " << number_of_trys << " trys." << endl;
total_tries=total_tries+number_of_trys;
break;
}
}
cout << "Play again[Y/N]: ";
cin >> go_again;
if(go_again!='Y')
break;
}
cout<<"\nThanks for playing. Your average of all trials is: ";
printf("%.1f",(float)(total_tries/total_rounds));
return 0;
}
Output: