Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please finish and submit the number guessing game that you started in class. Kee

ID: 640887 • Letter: P

Question

Please finish and submit the number guessing game that you started in class. Keep in mind that the program takes the role of the one guessing the number, while the user provides the answers to the questions that the program is going to ask. The user will answer with one-character input, i.e. either 'y' or 'n'. After the necessary number of questions the program needs to come up with the number.

Please make one submission, i.e. for your partner and yourself, but make sure that both names appear in a comment section of the code file. Please DON'T submit the same code file twice.

Hint: the one character input is tricky. As we read the input from the command line, which is automatically buffered, you need to hit the return key in order to make the system to actually stop reading and look at the input. Thus, automatically you get a extra, unwanted ASCII 10 (i.e. new line) character, that you need to get rid of. In addition to using fflush or putting a extra space in the scanf you could also use the getchar() function and call the function twice. The second getchar() call will just discard the excess character:

response = getchar(); getchar();

In the above example, getchar() reads one character and puts it into the variable response (type char), then tries to read another character, catching the new line char that you entered before as well, thus discarding it.

Explanation / Answer

Program code:

// GuessANumber.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{

     srand(time(0));

     char status;

     int tries=0;

     int min = 0;

     int max = 100;

     int guess = rand() % 100 + 1;

     int toGuess;

     cout << "**************************************************************";

     cout << " Welcome to Guess My Number ";

     cout << "Computer vs Player Edition ";

     cout << "Please enter your number (between 1 & 100): ";

     cin >> toGuess;   

     cout <<"Computer's Guess: " <<guess << " ";

     cout<<"Is it greater than "<<guess<<endl;      

     do

     {   

          tries++;

          cin>>status;

         

          if(status=='y')

          {            

              min = guess;

              cout<<"min "<<min<<endl;

          }

          else if(status=='n')

          {            

              max = guess;

              cout<<"max "<<max<<endl;

          }   

          else

          {

             

          }        

          guess = rand()% (max-min) +min;

          cout <<"Computer's Guess: " <<guess << " ";

          cout<<"Is it greater than "<<guess<<endl;

         

     }while(toGuess!=guess && tries < 10);

     if(toGuess==guess)

     {

          cout<<"Congrats! you have guessed the number correctly.";

          cout<<endl;

          cout<<"The number is: "<<guess<<endl;

     }

     else

     {

          cout<<"The actual guessed number is: "<<toGuess<<endl;;

     }

    

     system("pause");

     return 0;

}

Sample output:

**************************************************************

Welcome to Guess My Number

Computer vs Player Edition

Please enter your number (between 1 & 100): 50

Computer's Guess: 22

Is it greater than 22

y

min 22

Computer's Guess: 64

Is it greater than 64

n

max 64

Computer's Guess: 37

Is it greater than 37

y

min 37

Computer's Guess: 42

Is it greater than 42

y

min 42

Computer's Guess: 46

Is it greater than 46

y

min 46

Computer's Guess: 49

Is it greater than 49

y

min 49

Computer's Guess: 50

Is it greater than 50

Congrats! you have guessed the number correctly.

The number is: 50

Press any key to continue . . .