Question

Write a C++ program using "for" loop to allow user to guess a certain number say...

Write a C++ program using "for" loop to allow user to guess a certain number say 55. The loop will end after 5 guesses or if he guesses the right number, which ever comes first. If user enters a number greater than 55, your program should print "You entered a bigger number". If user enters a number smaller than 55, your program should print "You entered a smaller number". If user enters 55 in less than 5 attempt, your program should print "You got it right. Congratulations." If user doesn't enter 55 in 5 attempts, your program should print "No more guesses. Better luck next time."

Hint: Declare a integer constant and initialize it to 55.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<iostream>

using namespace std;

int main() {
   int n, r = 55;
   cout<<"I am thinking of a number between 1 and 100"<<endl;
   for(int i = 0;i<5;i++){
      cout << "Your guess? ";
      cin >> n;
      if (n == r) {
         break;
      }
      else if (n < r) {
         cout << "You entered a smaller number" << endl;
      }
      else {
         cout << "You entered a bigger number" << endl;
      }
   }
   if(n == r){
      cout<<"You got it right. Congratulations."<<endl;
   }
   else{
      cout<<"No more guesses. Better luck next time."<<endl;
   }
   system("pause");
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program using "for" loop to allow user to guess a certain number say...
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
  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Write a program that prompts the user for an integer that the player (maybe the user,...

    Write a program that prompts the user for an integer that the player (maybe the user, maybe someone else) will try to guess. If the player's guess is higher than the target number, the program should display "too high" If the user's guess is lower than the target number, the program should display "too low" The program should use a loop that repeats until the user correctly guesses the number. Then the program should print how many guesses it took....

  • Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...

    Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it) Program should be able to guess correctly in 7 tries or lower. Initialize a variable that represents the lowest possible number to 0 (what type should this be?) Initialize a variable that represent the highest possible number to 100 (what type should this be?) Initialize a Boolean variable that represents if we’ve achieved the correct guess to false Initialize a variable in which to...

  • C++ Visual Studios Your program will attempt to guess a number that the user is thinking...

    C++ Visual Studios Your program will attempt to guess a number that the user is thinking of within the range [1, 20) (1 inclusive 20 exclusive). Your program will have 5 tries and each time the user should be able to tell your program if it is either correct, high, or low. If your program attempts 5 times but is not told that it is correct on the 5th try you should output "You cheated...". Otherwise your program should always...

  • write a python program to guess a number between 1 to 9. The user is prompted...

    write a python program to guess a number between 1 to 9. The user is prompted to enter a guess. when the user's guess in not correct, ask them for another guess. when the user guesses the correct number, print "correct" and exit the program #use these two lines to begin the program: from random import randint correctNum=randit(0, 9) #now write a while loop to play the game

  • <Java> 1-Write a program that converts from a string into an integer number. For example: For...

    <Java> 1-Write a program that converts from a string into an integer number. For example: For "123" the program should return 123 For "131", The program should return 131 For "-121" the program should return -121 For "1e3" the program should print an Invalid number For "a1" the program should print an invalid number Do not use Integer.parse function 2. Generate a random number in [0, 100]. Write an efficient guess procedure that takes no more than 7 guesses to...

  • Write a Java Program to allow a user to guess a person’s age until they get...

    Write a Java Program to allow a user to guess a person’s age until they get it correct. After each guess, the user must be told whether their guess was correct, too high or too low. Once the user enters the correct age, the program must display how many guesses it took the user to guess the correct age.

  • For this lab you will write a Java program using a loop that will play a...

    For this lab you will write a Java program using a loop that will play a simple Guess The Number game. Th gener e program will randomly loop where it prompt the user for a ate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a r has guessed the correct number, the program will end with a message indicating how many guesses it took to get the right answer and a...

  • Guess the number! You will create a program that will ask the user to guess a...

    Guess the number! You will create a program that will ask the user to guess a number between 1 and 10. The pseudocode is below. Be sure to import random at the beginning of your code and use a comment block explaining what your program does #Guess the number week 4 #Name: #Date: #Random number, loop while true #ask user for number. #if number is too high or too low, tell user, if they guessed it break out of loop...

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