Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number 1-10 in her mind and commands the user to guess what it is. If the user’s guess is equal to (remember your double equal sign!) the conjured number, Mme. Esmerelda extends a job offer as a clairvoyant assistant. Add a user-driven loop that allows the user to play repeatedly if desired (but your program should conjure a new random number with every loop iteration).
These steps from my teacher:
//0. create variables for random number and user guess (you have
user guess but not the random number variable). Also seed the
random number (which you are doing correctly already at the top of
your program).
//1. generate a random number and put it in an integer variable.
You've seeded the random number but have not actually generated
one. Also, the game is not fun if the number to guess is always 5,
as you've coded it. It should be random every time.
//2. display greeting and ask user for guess
//3. You may keep your input validation loop here, which is
correct, but that is not actually required in this
assignment.
//4. now make the decision with a simple if/else:
//if user guess is equal to the random number
//display "that is correct" message
//else
//display "sorry, that is incorrect" message
^We only need one simple if/else in this program.
If the program is required in C++ please refer the below code snippet.
#include <iostream>
#include <time.h>
int main() {
//defines range for random number selection.
int min = 1, max = 10, rcount = 10;
int rndnum, usernum;
bool willguess = true;
char chkwish;
char nwish[1];
nwish[0] = 'n';
while (willguess == true)
{
srand(time(0)); //seed to change the random number
rndnum = (rand() % (max - min) + 1) + min;
std::cout << "\nGreetings!!.. Can u guess the number?\n";
std::cin >> usernum;
//compare the user guess and random number
if (usernum == rndnum)
{
std::cout << "Perfect!!.. That's correct!\n";
}
else
{
std::cout << "Sorry, that is incorrect.\n";
std::cout << "Correct guess is:" << rndnum << "\n";
}
std::cout << "Do you want to guess another number(y/n)?";
std::cin >> chkwish;
if (chkwish == nwish[0])
{
willguess = false;
}
}
return 0;
}
Output Snapshot:
If you need program in C code, please refer the below code snippet.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef enum { F, T } boolean;
int main() {
//defines range for random number selection.
int min = 1, max = 10, rcount = 10;
int rndnum, usernum;
boolean willguess = T;
char chkwish;
char chr;
char nwish[1];
nwish[0] = 'n';
while (willguess == T)
{
srand(time(0)); //seed to change the random number
rndnum = (rand() % (max - min) + 1) + min;
printf("\nGreetings!!.. Can u guess the number?\n");
scanf("%d",&usernum);
//compare the user guess and random number
if (usernum == rndnum)
{
printf("Perfect!!.. That's correct!\n");
}
else
{
printf("Sorry, that is incorrect.\n");
printf("Correct guess is:%d\n", rndnum );
};
printf("Do you want to guess another number(y/n)?");
scanf("%c",&chkwish);
scanf("%c",&chkwish); //repeating to avoid newline char as scanf was skipping
if (chkwish == nwish[0])
{
willguess = F;
}
}
return 0;
}
Output Snapshot:
Get Answers For Free
Most questions answered within 1 hours.