Thread: Quick Help, Kinda Lost here

1. Quick Help, Kinda Lost here

Okay well I know this is a no brainer to most of you but I am brand new to the language and programming so could I get some help . Basically I want to make a quick game where you have to guess a number within 5 tries.

Its pretty crazy what happens when you execute my program i've created an endless loop of some kind.

Code:
```#include <iostream.h>
int main()
{

//declerations
int guess=0; //ammount of guesses
int UserNum=0; //the number the user guested
int GenNum=10; //the random number, 10 until i figure out how ro rand

cout<<"Please guess a number between 1-100:  ";
cin>>UserNum;
while(guess!=5) //as long as guess isnt 5 keep going
if (UserNum==GenNum)
{
cout<<"You win!";
break;
}
else
cout<<"Please try again!";
cin>>UserNum;
guess++;
cout<<"You Lose";
return 0;
}```

2. You're missing a lot of braces. Forexample in your else statement and in your while statement.

3. Code:
```#include <iostream.h>
int main()
{

//declerations
int guess=0; //ammount of guesses
int UserNum=0; //the number the user guested
int GenNum=10; //the random number, 10 until i figure out how ro rand

cout<<"Please guess a number between 1-100:  ";
cin>>UserNum;
while(guess!=5) //as long as guess isnt 5 keep going
{
if (UserNum==GenNum)
{
cout<<"You win!";
break;
}
else
{
cout<<"Please try again!"<<endl;
cin>>UserNum;
guess++;
}
//i have messed these 2 braces up !!
}
cout<<"You Lose";
return 0;
}```
okay there is no more endless loop but it doesnt quit after 5 guesses it just keeps going

4. Works for me. You also might try better formatting that makes the code readable. Maybe something like:
Code:
```#include <iostream.h>
int main()
{
//declerations
int guess  = 0; //ammount of guesses
int UserNum= 0; //the number the user guested
int GenNum =10; //the random number, 10 until i figure out how ro rand

cout << "Please guess a number between 1-100:  ";
cin >> UserNum;
while(guess != 5) //as long as guess isnt 5 keep going
{
if (UserNum == GenNum)
{
cout << "You win!";
break;
}
else
{
cout<<"Please try again!"<<endl;
cin>>UserNum;
guess++;
}
//i have messed these 2 braces up !!
}
cout << "You Lose";
return 0;
}```

Popular pages Recent additions