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?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);
Last edited by Paderi; 05-07-2020 at 11:38 AM.
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.
Why not?
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.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);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.
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.
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.Originally Posted by Paderi
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
> 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.
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.