Question

please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...

please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user plays more than one game. Don't forget - pseudocode, comments, proof of testing. Post file(s) in digication, then post link on MOODLE. SLOs: code modification file writing looping algorithm modification*/ #include #include //for rand and srand #include //for the time function using namespace std; int main() { srand(time(0)); // Seed the random number generator srand(NULL); //asking user if he/she likes to play a game cout << "Would you like to play a game? "; char ch; // choices cin >> ch; // if choice is 'Y' or 'y' if (ch == 'Y' || ch == 'y') { //generating a random number between 1 and 10 int randNum = (rand() % 10) + 1; int guess; //asking and reading a guess cout << "Guess a number between 1 and 10: "; cin >> guess; //if guess is same as number, if (guess == randNum) { cout << "Bingo" << endl; } //if guess is smaller than number, else if (guess < randNum) { cout << "too low" << endl; } //if guess is bigger than number, else { cout << "too high" << endl; } } return 0; }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code -

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{   
//varibable declare
int guess,guessCount=0;
char ch;
// open a file in write mode , result.txt file name
ofstream outfile;
outfile.open("result.txt");
//srand
srand(time(0));
//ask user if he want to play a game
cout << "Would you like to play a game? ";
cin>>ch;
//if user enter N or n for no then exit
while(ch!='n' && ch!='N'){
//generate random number
int randNum = (rand() % 10) + 1;
//write to result file Random number generated
outfile <<"Random number : "<< randNum << endl;
//inner while loop until guess not equal to random number
while(guess!=randNum){
//ask user to Guess between 1 and 10
cout << "Guess a number between 1 and 10: ";
cin >> guess;
//print user input to result file
outfile <<"User Guess : "<< guess << endl;
//increment guessCount varibable
guessCount++;
//if user guess equal to random guess
if (guess == randNum) {
//then print Bingo and
cout << "Bingo" << endl;
//write Total guess user took to output file
outfile <<"Total Guess User took : "<< guessCount << endl;
break;
}
//if guess is smaller than number,
else if (guess < randNum)
{
cout << "too low" << endl;
}
//if guess is bigger than number,
else {
cout << "too high" << endl;
  
}
}
//ask user if he want to play again
cout<<"Would you like to play again ,enter Y or y for yes else N or n for no "<<endl;
cin>>ch;
guessCount = 0;

}
outfile.close();
return 0;   
}


Screenshots -

result.txt -

Random number : 5
User Guess : 2
User Guess : 7
User Guess : 5
Total Guess User took : 3
Random number : 6
User Guess : 5
User Guess : 8
User Guess : 6
Total Guess User took : 3


pls do give a like , thank you

Add a comment
Know the answer?
Add Answer to:
please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • Specification Write a program that allows the user to play number guessing games. Playing a Guessing...

    Specification Write a program that allows the user to play number guessing games. Playing a Guessing Game Use rand() function from the Standard C Library to generate a random number between 1 and 100 (inclusive). Prompt the user to enter a guess. Loop until the user guesses the random number or enters a sentinel value (-1) to give up. Print an error message if the user enters a number that is not between 1 and 100. Print an error message...

  • #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0));...

    #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....

  • Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...

    Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis With each guess... If the guess does not have an equal number of characters, tell the user...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts...

    Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts the user for a guess. Use the pseudorandom number generator, the Random class. When the user guesses the correct number, say so, and display how many guesses the user required to guess correctly. Allow the user to play multiple times by asking the user if the user wants to continue playing.      Guess my number between 1 and 1000 => 500      Too high...

  • C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the...

    C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the following game. The computer selects a random number between 1 and 100 and asks the user to guess the number. If the user guesses incorrectly, the computer advises to try larger or smaller number. The guessing repeats until the user guesses the number. The dialog should look as follows (user input is shown in bold): Guess a number between 1 and 100: 50 try...

  • Has to be written in C++. Visual studio. Write a C++ program to realize the game...

    Has to be written in C++. Visual studio. Write a C++ program to realize the game of guessing the number. Generally, the game will generate a random number and the player has to find out the number. In each guess, the program will give you a feedback as your guess is correct, too large, or too small. Then the player play repetitively until find out the number. Specifically, the game plays as the following. a). When the game is started,...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT