CS 1005, C-term 03
Homework 2
Number Guessing

Due: January 28, 2003 at 11:59pm

Assignment

This project is to implement a program that plays a guessing game. The computer “thinks” of a number between 0 and 99 and the user repeatedly tries to guess it. After the user successfully guesses the number, the computer gives the user the option of playing again.

This program will be implemented with nested loops. The internal loop gets the user’s guess, determines whether it is correct or not, gives them a hint as to whether the answer is higher or lower than what they guessed, and lets them try again. The external loop “thinks” of a new number so that the user can play a new game.

The computer will “think” of a new number each time the game is played. The code to perform this operation will be provided for you since it involves C operations that we have not yet covered in class. See Section below entitled “Implementation”.

 

Before Starting

Read Chapter 2.

Details About the Program

Implementation

This program will be implemented as two loops. The inner loop gets the user’s input and plays the game. The outer loop lets the user play more than once. There are two pieces of code that are being provided to you. The first piece initializes the random number generator so that it generates a new number as the first number played in the game every time the program starts. This piece of code should come before either of the loops in your program:

// Seed the random-number generator with current time so that
// the first number will be different every time we start this program.
int gameNumber;
srand( (unsigned)time( NULL ) );

 The second piece of code calculates the next number for the game whenever the user wants to play again. This code should go inside the outer loop but outside the inner loop.

 // Generate next number for the game.
// Adjust random number so that it falls between 0 and 99.
gameNumber = (int) ((float) rand() / RAND_MAX * 100);

Note: With programs containing nested loops, it is a good idea to implement the program in stages. For the first stage implement the inner loop which allows the user to play the game once. After that code is working, add the outer loop which allows the user to play the game more than once.

Sample Execution

A sample output of the program would be:

Welcome to the Guessing Game!

 I’m thinking of a number between 0 and 99.
Enter your guess.

I’ll tell you whether to guess higher or lower.

Enter a guess: 49
Guess higher

Enter a guess: 75
Guess lower

Enter a guess: 63
Guess higher

Enter a guess: 69

Congratulations! You’re right!

Do you want to play again? (Enter Y for yes or N for no): Y

 I’m thinking of a number between 0 and 99.
Enter your guess.

I’ll tell you whether to guess higher or lower.

Enter a guess: 50
Guess lower

Enter a guess: 25
Guess lower

 Enter a guess: 15
Guess higher

Enter a guess: 20
Guess lower

Enter a guess: 18

Congratulations! You’re right!

Do you want to play again? (Enter Y for yes or N for no): N

Thanks for playing! Goodbye!




Include files

iostream
        allows
you to use cin, cout, endl

cstdlib
   
    allows you to use srand and rand

ctime
       
allows you to use time

Assumptions and restrictions

The program must validate the user’s input. Any invalid input should cause the program to terminate and the following error message to be displayed for the user.

Program terminating. Invalid input received.

The valid input is as follows:

·        When the user is guessing a number, they must enter a number between 0 and 99. 0 and 99 are valid numbers. The user does not have to follow the computer’s hints. For example, if the user entered a 50 and the computer said to guess higher and the user entered a 25 next, the computer would just say guess higher. This would not be considered invalid input.
·        When the user is prompted to play again both uppercase and lowercase letters are valid. They may enter Y or y for yes or N or n for no.

Program style and Comments

There's a lot more to programming than writing code that works. A good program is well-documented. Each program must contain in the comments your name and section number, and the date. The purpose of the program should be stated. Declared variables need to be explained with brief comments; use meaningful identifiers. Comments should be used to clarify obscure sections of code, but be careful - over-commenting can be worse than not including comments at all. Blank lines should separate major sections of the program. Indentation should make the program easy to read, and it should be used consistently. Refer to Documentation Guidelines for specific guidelines on how to document your programs for this course.

 

Deliverables

You must turn in your source code, a script file showing successful compilation and a sample execution of your program, and your documentation file called README. The command to use to turn in your files for this assignment is

/cs/bin/turnin submit cs1005 hw2 hw2.cxx hw2.script README

This assignment is due at 11:59pm on Tuesday Jan 28th, 2003.

 

Grading

The grade for this assignment will be based on 100 points. Point values will be awarded as follows:

Documentation (40 points)

Internal documentation

Introductory comment, meaningful identifiers, proper use of indentation and blank lines, comments for variables, algorithm comments

External documentation

Completion and submission of the CS department standard documentation file, (see Documentation Guidelines ). Remember to include a statement about outside assistance in the "References" section. Name your documentation file README.

Functionality (60 points)

The program should produce the correct results. Your script file should show successful compilation, as well as a sample execution. Name your script file hw2.script. Programs must compile in order to receive points for Functionality.