bug using getch

bug at getch it does not let me choose again, i dont want to use a while loop because it has more code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <conio.h>
using namespace std;

int main (){
    int opt;
    menu:
    cout << "Calculator.\n";
    cout << "1.ADDITION\n";
    cout << "2.SUBTRACTION\n";
    cout << "3.MULTIPLICATION\n";
    cout << "4.DIVISION\n";
    cout << "Select an option:";
    cin >> opt;

    switch(opt){
        case 1:
        double x,y,ans;
        cout << "----ADDITION----\n";
        cout << "Enter two integers:";
        cin >> x >> y;
        
        ans = x + y;
        cout << x << " + " << y << " = " << ans;
        break;

        case 2:
        cout << "----SUBTRACTION----";
        cout << "Enter two integers:";
        cin >> x >> y;

        ans = x - y;
        cout << x << " - " << y << ans;
        break;

        case 3:
        cout << "----MULTIPLICATIOMN";

        default:
        cout << "INVALID INPUT.";
    }
    getch();
    goto menu;
    

}
You're sadly mistaken if you think a while (true) loop generates more code than your goto. What do think a while (true) loop generates? Guess what, it generates a label and a goto under the covers. In other words, it's identical to what you've coded.

Now for the lecture: gotos are a very bad habit to get into. They lead to unmaintainable programs. A while loop clearly conveys to the reader that you want the code to continue (loop). A goto does NOT convey that. A goto implies you want to jump somewhere in the program. Not necessarily back to the beginning.

As a beginning programmer, it's good that you're concerned about the size of your program, but to be worried about one statement generating more code than another is a false economy. You should be much more concerned about making sure that your program executes correctly and clearly conveys it's logic to the reader.




thanks for the insights, well appriciated
and why do you want getch() and have #include <conio.h> conio.h isn't standard and isn't portable.
I think people learning programming for the first time seem to like having a simple function that gives immediate feedback when you press a button, as opposed to needing to press a particular key like "Enter" with a more complicated (albeit standard) call.

But not using goto is definitely a good lesson to learn early.

______________

With regard to the original post: I cannot reproduce your issue. It lets me choose again each time, after I press a key to execute passed the getch().
The caveat here is that if you accidentally entered a non-number into lines 14, 21, or 30, then your input stream will be set to a failure state, and will no longer accept input.
Last edited on
a simple function that gives immediate feedback when you press a button, as opposed to needing to press a particular key like "Enter" with a more complicated (albeit standard) call.


Yep. Why can't this feature be part of the standard? Same as providing a standard for kbhit()
Topic archived. No new replies allowed.