Karnataka KSEAB SSLC JTS Programming in ANSI C study materials
Karnataka KSEAB SSLC JTS Programming in ANSI C study resources

The Menu That Never Exits

Karnataka KSEAB SSLC JTS Programming in ANSI C · Put your knowledge to work in context.

All case studies

The Menu That Never Exits

In a Belagavi classroom, a student builds a menu-driven calculator with a do-while loop. Options: 1 add, 2 subtract, 3 multiply, 4 quit. The switch has case 1 with break, case 2 without break, case 3 with break. Input reading uses scanf("%d", &choice) once before the loop, then scanf("%d", &choice) at the bottom of the loop. The student tests with 1 6 3 and gets 9, which looks right. Testing 2 10 4 prints both the difference and a product, and after option 4 the program asks for the choice one more time before stopping. A second bug appears when a classmate types 'x': the program prints the menu endlessly. The teacher asks for a line-by-line trace and a corrected loop.

1. Trace the run with choice 2, operands 10 and 4. Explain what is printed and why.

Read the worked answer

Execution enters case 2, computes and prints 10 - 4 = 6. Because there is no break at the end of case 2, control falls through into case 3 and also executes the multiply branch, printing 10 * 4 = 40. So the user sees both 6 and 40.

The missing break is the cause; the subtract case is not "also doing multiply" by design. Every case that should terminate on its own needs a break; (or a return/goto exit), otherwise execution continues into the next case label. Option 1 looks right only because case 1 happens to have a break.

2. Why does choosing option 4 not stop the program immediately? Give a corrected control-flow structure.

Read the worked answer

The break inside a switch leaves only the switch, not the surrounding do-while. After the quit message, execution still reaches the scanf at the bottom of the loop body, asks for a choice again, and only then evaluates the loop condition. That is why the extra prompt appears.

A cleaner structure reads the choice at the top of the loop and uses the loop condition to stop:

scanf("%d", &choice);
while (choice != 4) {
    switch (choice) {
    case 1: /* ... */ break;
    case 2: /* ... */ break;
    case 3: /* ... */ break;
    default: printf("Invalid\n");
    }
    scanf("%d", &choice);
}

Alternatively keep the do-while but set a done flag on option 4 and test it in the condition. Either way, the quit path must avoid the trailing input read.

3. Why does typing 'x' cause the menu to print endlessly, and how should the input be validated?

Read the worked answer

scanf("%d", &choice) fails on 'x', returns 0, and leaves 'x' in the input buffer. choice keeps its old value, the loop condition remains true, and the next iteration reads the same character again - an infinite loop that repeatedly prints the menu.

Fix it by checking the return value and consuming the bad input:

if (scanf("%d", &choice) != 1) {
    int c;
    while ((c = getchar()) != '\n' && c != EOF)
        ;
    printf("Please enter a number\n");
    continue;
}

The continue re-tests the loop condition after clearing the buffer. A menu-driven program should never assume that input conversion succeeded; the return value of scanf is the only reliable signal.