Homework 4 Problem 4

Homework 4 Problem 4

You cannot submit for this problem because the homework's deadline is due.

Description

Write a guessing number game program. The program will generate a random number (in JOJ, the random number will be given by program argument) from 1 to 100. The user will be asked to key in its guessed value for this random generated number. The program will tell the user if the guesss is Too low, Too high, You won in s guesses, or You lost in s guesses, where \(s\) is the number of times this user has guessed in this round. The program keeps running until the user guessed the corresponding value or until the fixed number of times has reached (10 times).

At the end of each round, the pragram with ask the user if he/she wants to play another round. The game continues with a new number if the user choose to continue playing the game; and terminates if the user choose otherwise.
Requirments: Decompose your program to contain at least two sub functions.

Input

Input is given from both standard input and program argument.
Your random number should is given by the program argument in order. And the guessing number is given by standard input in order.
After one round, the input will either be N or Y, denoting stopping game or continuing game.
We promise the given input will follow the game rule.

Output

Print the prompts as descirbed in the preceding part of the problem. Just keep consistent with the one descirbed in pdf.

Sample

Sample Test #1

Sample Program Argument

35 35

It means you should pass in the argument by adding the argument to your program,

Test on Unix: ./ex4_4 35 35
Test on Windows: ex4_4.exe 35 35

Sample Standard Input

50
25
35
Y
1
2
3
4
5
6
7
8
9
0
N

Sample Output

A new number has been generated.
Guess the secret number between 1 and 100. Enter guess: Too high; Try again.
Enter guess: Too low; Try again.
Enter guess: You won in 3 guesses!
Play again (Y/N): A new number has been generated.
Guess the secret number between 1 and 100. Enter guess: Too low; Try again.
Enter guess: Too low; Try again.
Enter guess: Too low; Try again.
Enter guess: Too low; Try again.
Enter guess: Too low; Try again.
Enter guess: Too low; Try again.
Enter guess: Too low; Try again.
Enter guess: Too low; Try again.
Enter guess: Too low; Try again.
Enter guess: Too low; You lost in 10 guesses!
Play again (Y/N): 

The output here differs from the one descirbed in pdf, beacuse the extra newline is given by user input.

In your case, you can save your standard input file as in.txt, and run the command ./ex4_4 35 35 <in.txt to check your exact output (or submit to JOJ directly).

Interactive output (won't accept by JOJ):

A new number has been generated.
Guess the secret number between 1 and 100. Enter guess: 50
Too high; Try again.
Enter guess: 25
Too low; Try again.
Enter guess: 35
You won in 3 guesses!
Play again (Y/N): Y
A new number has been generated.
Guess the secret number between 1 and 100. Enter guess: 1
Too low; Try again.
Enter guess: 2
Too low; Try again.
Enter guess: 3
Too low; Try again.
Enter guess: 4
Too low; Try again.
Enter guess: 5
Too low; Try again.
Enter guess: 6
Too low; Try again.
Enter guess: 7
Too low; Try again.
Enter guess: 8
Too low; Try again.
Enter guess: 9
Too low; Try again.
Enter guess: 0
Too low; You lost in 10 guesses!
Play again (Y/N): N

Homework 4

Not Claimed
Status
Finished
Problems
5
Open Since
2020-07-01 00:00
DDL
2020-07-07 23:59
Extension
48.0 hour(s)