Hey. Okay so I wrote this code for a magic # program (using if-else statements)
ID: 3652404 • Letter: H
Question
Hey. Okay so I wrote this code for a magic # program (using if-else statements) for a user to guess a # btwn 1 and 20.Now the prof. wants us to alter the program using a for-loop. Allow the user to have up to 5 chances to guess what that # is.(I'm assuming this is why we have to use for-loop)
In addition, we also have to tell the user on what guess they guessed the right # (if at all).
If person guessed correctly in say guess #3 you have to break out of the loop.
I've been trying to go over the text and trying to figure out but I'm so lost with this for-loop thing.
Here is the unaltered program for only 1 guess:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
// This program prompts a user to guess a random number between 1 and 20, and tells them if their guess is correct.
// If it is incorrect then it will let the user know whether or not to guess higher or lower.
int magic;
int guess;
int TRY;
magic=rand()%20+1;
cout<<" Enter your Guess of a number beteewn 1 and 20: ";//prompt msg for user
cin>>guess;//users input
if(guess==magic) //if guess is correct it will let the user know its right and display what the # is.
{
cout<<" *** Wow, You got it RIGHT! *** The magic number is: "<<magic<<" "<<endl;
}
else //if guess if incorrect it will let the user know its incorrect.
{
cout<<" ...Sorry, you're WRONG "<<endl;
if(guess<magic)cout<<"Your guess is too low "<<endl;//If this will let the user know if they guessed too low, if incorrect.
else cout<<"Your guess is too high "<<endl;//this will let the user know if they guessed too high, if incorrect
}
system("pause");//I had to put this or else my compiler closes the terminal
return 0;
}