Thread: How do you do this without a goto?

  1. #1
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68

    How do you do this without a goto?

    Code:
    repeat:
        gotoxy(50,5);
        strcpy(out, "Description number to change: ");
        puts(out);
        gotoxy(50+strlen(out),5);
        scanf("%s",number);
        num = atoi(number);
    if (num > 20) goto repeat;    
    gotoxy(50,6); puts(number);
    How do you do this without a goto?
    Last edited by Paderi; 05-07-2020 at 11:38 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,670
    do while num > 20
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Quote Originally Posted by Salem View Post
    do while num > 20

    In my example you stay in the loop till a proper entry is made. I was unable to do that in a do while loop.
    Last edited by Paderi; 05-07-2020 at 03:12 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,404
    Why not?
    Code:
    do {
        gotoxy(50,5);
        strcpy(out, "Description number to change: ");
        puts(out);
        gotoxy(50+strlen(out),5);
        scanf("%s",number);
        num = atoi(number);
    } while (num > 20);    
    gotoxy(50,6); puts(number);
    Where you might find a do while loop alone cumbersome is when you want to print an error message and do other error handling: in that case, you either need an input error flag to control the loop so you can do the error handling before the next iteration, or you move the error handling code to a function that you can call in the loop condition. In such cases, you might consider a controlled infinite loop as an alternative.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Quote Originally Posted by laserlight View Post
    snipped.
    I could not get the do while loop to work, whereas the spaghetti worked alight. I will need to have another go at it. Perhaps I might get back to you if I need to.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,670
    Perhaps you should post your attempt, rather than meaningless "it didn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Quote Originally Posted by Salem View Post
    Perhaps you should post your attempt, rather than meaningless "it didn't work".
    Yes I agree, a bit of an empty statement. I will get back to you with decent details, when I have got the program running and do the polishing up.

  8. #8
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    My code is running without hicks and tricks. It has an occasional goto. Goto, as you will know of course, is a facility provided by the creators of the language. So why bend over backwards to get rid of them because it isn't en vogue to use them. In all other disciplines - like engineering, medicine, psychology - if anything does what it is supposed to do, and does so 100% without fail, without dysfunctional side effects, if is constructed intelligently, if it can be easily accessed for maintenance and replacement of warn out or replacement parts, it is okay.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,670
    Post your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,404
    Quote Originally Posted by Paderi
    So why bend over backwards to get rid of them because it isn't en vogue to use them.
    Because this isn't a matter of fashion; it's a matter of readability that affects how maintainable your code will be. Structured code can certainly be written with goto: your code in post #1 is such an example, which is why I could trivially convert it to a do while loop. Loop constructs make the loop explicit, and being less powerful than goto, means that there is less mental overhead about where control might go: even with the use of break of continue, it is still only a choice between control going to the next iteration or right after the loop.

    Besides, the use of goto is still "en vogue" in C: it is still commonly used to centralise error handling within a function, or to break out of nested loops. In both cases control only goes forward, so in a sense goto is used to implement an implicit construct where explicit constructs (e.g., a break operation that can break out of nested loops) do not exist in the language.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Quote Originally Posted by Salem View Post
    Post your code.
    Salem, thank you for your invitation to post my code. I intend to just use the application as is, since it performs alright. This is not saying that the code cannot be improved, only that there will not be any apparent user benefit by doing so.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,670
    > only that there will not be any apparent user benefit by doing so.
    What about your benefit of improving as a programmer?

    Having your work reviewed by your peers is the only way you'll improve.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Quote Originally Posted by Salem View Post
    > only that there will not be any apparent user benefit by doing so. Salem: What about your benefit of improving as a programmer? Having your work reviewed by your peers is the only way you'll improve.
    How about trial and error? However, I much appreciate your implicit offer. A little analogy to amuse you during the Coronas: supposing you were walking along a canal and you see someone drowning. You jump in the water because you are a Helper. Swimming up to the poor fellow, he tells you he wants a bag of chips. What do you do (as a Helper), push his head under water so he will shut up, and drag him to the side? Or do you go home because the stupid bugger is wasting your time? Or do you go and buy a bag of chips for his last wish in life? Muddling-on is such a hell of a good teacher.
    Last edited by Paderi; 05-10-2020 at 04:14 AM.

  14. #14
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Quote Originally Posted by laserlight View Post
    Because this isn't a matter of fashion; i<snipped>
    I chanced to come across a text on the Net where it said that the compiler cannot optimize goto loop. Well that is a hard additional argument.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is goto?
    By muhammetekurt in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2020, 07:20 AM
  2. goto
    By chrismiceli in forum C Programming
    Replies: 20
    Last Post: 08-26-2003, 07:19 AM
  3. goto again...
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2003, 07:25 PM
  4. goto
    By volk in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2003, 10:04 PM
  5. Someone used a goto...
    By adrianxw in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 01-30-2003, 02:43 AM

Tags for this Thread