Problem with very simple code

This is a discussion on Problem with very simple code within the C++ Programming forums, part of the General Programming Boards category; ill let you be as confused as i am. Code: #include <iostream> using namespace std; int main() { int guess; ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    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

  2. #2
    Banned
    Join Date
    Oct 2004
    Posts
    250
    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();
    }

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ewww.... did you just suggest goto?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    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;

    }

  5. #5
    Banned
    Join Date
    Oct 2004
    Posts
    250
    whats so bad about goto?
    i think using goto is allright with smaller programs..

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    goto is best avoided. Its better you go in for other options when available.

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by cgod
    whats so bad about goto?
    i think using goto is allright with smaller programs..
    ask dijkstra
    http://www.xahlee.org/UnixResource_d...goto_harm.html
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    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;    
        }

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by cgod
    whats so bad about goto?
    i think using goto is allright with smaller programs..
    there's no need for goto in any program, ever...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Quote Originally Posted by cgod
    whats so bad about goto?
    i think using goto is allright with smaller programs..
    [grandpa simpson] Lets sacrifice him to our god! [/gs]

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    instead of
    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...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Banned
    Join Date
    Oct 2004
    Posts
    250
    in some programs you might have to use goto

  13. #13
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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.

  14. #14
    Banned
    Join Date
    Oct 2004
    Posts
    250
    well unless you use goto your going to have to rewrite code alot

  15. #15
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  2. problem with some simple code =/
    By Josh Norton in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 05:27 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21