Question

Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number...

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.

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
This is a Java program Program Description You work for a local cell phone company and...
This is a Java program Program Description You work for a local cell phone company and have been asked to write a program to calculate the price of a cell phone data plan being purchased by a customer. The program should do the following tasks: Display a menu of the data plans that are available to be purchased. Read in the user’s selection from the menu.  Input Validation: If the user enters an invalid option, the program should display an error...
Create a very simple TCP Application in JAVA. You will create a client and a server....
Create a very simple TCP Application in JAVA. You will create a client and a server. The client must execute after you start the server. Here is what they will do. The client will read from the console a string that the users enters. This string will be the name of the user, let's say Bob. The client then sends the name, Bob, in an Object Stream, to the server. The server will receive this Object Stream and send to...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all user information from a given input file. The input file contains information of a user in following order: username, first name, last name, password, account number and account balance. Information is separated with ‘|’. o username is a unique information, so no two users will have same username. Sample input file: Username eaglebank has password 123456, account number of BB12 and balance of $1000....