0

if - elseif - else condition is not working as expected in the below code. The code behaves differently when passing different inputs. It looks like break is not working or being skipped in the case.

Code:

#include <iostream>

void function(){
    int option;
    char choice;
    std::cout << "Select an option\n";
    std::cin >> option;
    switch (option)
    {
    case 1:
        std::cout << "Y or N ??\n";
        std::cin >> choice;
        if (choice == 'Y')
        {
            break;
        }
        else if (choice == 'N')
        {
            function();
        }
        else
        {
            std::cout << "Error : Invalid Input1\n";
            break;
        }
    default:
        std::cout << "Error : Invalid Input2\n";
        break;
    }
}

int main()
{
    function();
    return 0;
}

output of the code when executed in a linux environment.

user@user:~/CPP_Project/test$ g++ test.cpp
user@user:~/CPP_Project/test$ ./a.out 
Select an option
1
Y or N ??
Y
user@user:~/CPP_Project/test$ 
user@user:~/CPP_Project/test$ ./a.out 
Select an option
1
Y or N ??
N
Select an option
Y
Error : Invalid Input2
Error : Invalid Input2
user@user:~/CPP_Project/test$ 
James Z
  • 12,209
  • 10
  • 24
  • 44
Pisers
  • 103
  • 5
  • 3
    The function is calling itself recursively on entering `'N'`. When the recursive call reads a `'Y'`, it will return to its caller, which (since execution then continues) will tell you you have invalid input. Instead of using recursion, use a loop. [Clearly you are expecting recursively called functions, when they return, to return back to `main()` - that is not what happens - a recursive call is not a glorified goto]. – Peter Mar 18 '23 at 23:36
  • _"is not working as expected "_ What were you expecting? _"The code behaves differently"_ differently than what? – Drew Dormann Mar 18 '23 at 23:37
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Mar 18 '23 at 23:42
  • 1
    its not clear what you want to do in this code. The call to function inside function is almost certainly wrong. You probably need a while loop but its hard to say since its not clear what you are trying to do. – pm100 Mar 18 '23 at 23:46
  • *"The code behaves differently when passing different inputs."* -- this is typical behavior when using branching structures such as `switch`-`case` and `if`-`else`. Why is this wrong in your example code? – JaMiT Mar 19 '23 at 03:49
  • *"It looks like break is not working or being skipped in the case."* -- which `break`? I see three. – JaMiT Mar 19 '23 at 03:52
  • *"The code behaves differently when passing different inputs. It looks like break is not working or being skipped in the case."* -- which case? You just multiple scenarios ("different inputs"). How are we supposed to know which inputs are required to re-create the problem? For that matter, why do we have to enter the inputs? You could make the problematic case easier to reproduce by bypassing the I/O, as in replacing `std::cin >> option;` with `option = 1; // Simulate user input.` – JaMiT Mar 19 '23 at 03:55

1 Answers1

3

The location of the break statement appears to be the source of the problem with the code. In the current implementation, the break statement only exits the if condition and not the switch condition. So, when an invalid input is entered, the switch statement continues to execute its default scenario.

Move the break statement outside the if-else block and use it to exit the switch statement to resolve this issue.