# Problem with very simple code

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-21-2004
pizzapie
Problem with very simple code
ill let you be as confused as i am.
Code:

```#include <iostream> using namespace std; int main() { int guess; cout<<"I am thinking of a number between 1 and 10."<<endl; cin>>guess; while(guess!=5) {     cin.get(); if(guess<5) { cout<<"Too low try again"<<endl;cin.get(); } else if(guess>5) { cout<<"Too high, try again"<<endl;cin.get(); } } cout<<"Well done"<<endl; return 0; }```
plz find wat prob is
• 10-21-2004
cgod
wow that code is messy
try this
Code:

```#include <iostream> using namespace std; int main() {     int guess;     int number = 10;     retry:     cout<<"i am thinking of a number between 1-10 try and guess it"<<endl;     cout<<"guess:"<<endl;     cin >> guess;     if(guess > 10)     {         cout<<"to high"<<endl;         goto retry;     }     else if(guess < 10)     {         cout<<"to low"<<endl;         goto retry;     }     else if(guess == number)     {         cout<<"well done you got it!"<<endl;     }        cin.get(); }```
• 10-21-2004
FillYourBrain
ewww.... did you just suggest goto?
• 10-21-2004
sangi
The problem is that you are not getting the new value of guess within the while
#include <iostream>

using namespace std;

int main()

{

int guess;

cout<<"I am thinking of a number between 1 and 10."<<endl;

while(guess!=5)

{
cin>>guess;

if(guess<5){
cout<<"Too low try again"<<endl;
}
else if(guess>5){
cout<<"Too high, try again"<<endl;
}
}

cout<<"Well done"<<endl;

return 0;

}
• 10-21-2004
cgod
i think using goto is allright with smaller programs..
• 10-21-2004
sangi
goto is best avoided. Its better you go in for other options when available.
• 10-21-2004
FillYourBrain
Quote:

Originally Posted by cgod
i think using goto is allright with smaller programs..

http://www.xahlee.org/UnixResource_d...goto_harm.html
• 10-21-2004
toysoldier
Haha, this simple prog adapt to newbie like me,
below is my trying:

Code:

```    #include <iostream>        using namespace std;         int main()        {            int guess;            cout<<"I am thinking of a number between 1 and 10."<<endl;            cin>>guess;            while(guess!=5)            {                        if(guess<5)             {                 cout<<"Too low try again"<<endl;                 cin>>guess;              //instead of cin.get();             }                          else if(guess>5)                        {                 cout<<"Too high, try again"<<endl;                 cin>>guess;              }            }                cout<<"Well done"<<endl;                 system("pause");         return 0;        }```
• 10-21-2004
major_small
Quote:

Originally Posted by cgod
i think using goto is allright with smaller programs..

there's no need for goto in any program, ever...
• 10-21-2004
face_master
Quote:

Originally Posted by cgod
i think using goto is allright with smaller programs..

[grandpa simpson] Lets sacrifice him to our god! [/gs]
• 10-21-2004
major_small
Code:

`system("pause");`
try
Code:

`cin.get();`
the first one is less desireable because if I write a program called pause.exe and overwrite the program on your system with it, every one of your programs that uses that command will run my code without you even knowing it...
• 10-21-2004
cgod
in some programs you might have to use goto
• 10-21-2004
jlou
Quote:

Originally Posted by cgod
in some programs you might have to use goto

You never "have to" use goto. Whether there is ever a good reason to use goto or not is debatable, but you never "have to" use goto.
• 10-21-2004
cgod
well unless you use goto your going to have to rewrite code alot
• 10-21-2004
jlou
Quote:

Originally Posted by cgod
well unless you use goto your going to have to rewrite code alot

No, if you don't use goto, then you use functions and loops, which are more appropriate, easier to read, more maintainable, etc...
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last