Thread: instead of goto

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    instead of goto

    what can u use instead of goto and why is this a bad way to
    write your codes

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    It is not liked by many because people think it creates sloppy code.

    It can be prevented by making the code a function or using loops. It matters how you are using it.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    somthing like this


    cout<<"what's your answer";

    int answer;

    cin>>answer;

    //if the answer is more than 10 answer again
    if(answer>10)
    //.........what now

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what can u use instead of goto
    You can usually avoid goto with a simple change in program flow or an extra variable, it's rarely hard.

    >and why is this a bad way to write your codes
    It's not a bad way to write code, but goto is easily abused. It's that abuse that everyone fears.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest
    I was advised that goto's are only useful for efficiently escaping deeply nested loops. But good programming shouldnt require more than about 4 levels of nesting (that'll probaby start an argument!) and I have still never used a goto statement

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    s how should i write the code above ^^^

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    13
    I don't know if it's the best way but instead of goto i use a while statement that continues and restarts until you make the while statement false and then it moves on.

    int main(){
    int 1st, 2nd;
    int 1st = 1;
    int 2nd = 1;

    while(1st==2nd)
    {
    //your code
    int 2nd = 2; //Set this to one and it will continue to the code again
    }
    return 0;
    }
    ~Silencer~
    What is this "C.O.D.E." you speak of?

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Code:
    cout<<"what's your answer"; 
    
    int answer; 
    
    cin>>answer; 
    
    //if the answer is more than 10 answer again 
    if(answer>10) 
    //.........what now
    change it around so it all goes inside a while loop:
    Code:
    int answer=99999;
    
    while (answer>10) {
     cin >> answer;
    }
    if you wanted to warn after a bad answer:
    Code:
    int answer;
    cin >> answer;
    
    while (answer>10) {
     cout << endl << "sorry, try again" << endl;
     cin >> answer;
    }

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    yes but i see u used cin>> one more time but that's exactly what i dont want to do


    while (answer>10) {
    cout << endl << "sorry, try again" << endl;
    cin >> answer; /// see what i mean!
    }

  10. #10
    Ryce
    Guest
    duno why u dont want cin>> again but try this...

    int answer = 0;
    cout << "Answer: ";
    do
    {
    cin>>answer;

    if(answer>10)
    {
    cout << "ERROR! " << endl;
    }
    }while(answer<10);

    should work

  11. #11
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    Question

    i've never heard of goto, how do you use it? i may need to use it someday in like someone said a REALLY nested loop...or should i never learn it? i say knowledge is power
    Paro

  12. #12
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Goto is pretty self explanatory, it makes the program goto a certain spot in the code.

    Code:
    int dummy;
    cin>>dummy;
    if (dummy==7) goto LABEL1;
    cout<<"you did not enter 7";
    return 0;
    LABEL1:
    cout<<"you entered 7";
    return 0;

  13. #13
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    Talking

    it seems to be VERY helpful! why shouldnt i use it? it looks like it is the best idea since sliced bread!!!

    SOMEONE TELL ME WHY I CANT USE IT OR I WILL!!!
    Paro

  14. #14
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    No one is saying that you cannot use it, but, it is just not liked by programmers. It creates very hard to read code because you have to look all over the place.

  15. #15
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    ok, i think i will use it then, because it makes it easier for me
    Paro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM