-2

I always put default at the end of all cases and it works, however, today I ran this chunk of code and my default is not working. I put it in other possible locations to check if it would work but it didn't. It is running all fine except for when I take a user input that does not match the cases I made. Can anyone please tell me whats wrong with the code or how can I make default statement work?

int main(){      
    int selection;

    do
    {
        cout << "Please make a selection: \n";
        cout << "1) Addition\n";
        cout << "2) Subtraction\n";
        cout << "3) Multiplication\n";
        cout << "4) Division\n";
        cin >> selection;
    } while (selection != 1 && selection != 2 && selection != 3 &&
        selection != 4);

    switch (selection)
    {
    case 1:
        cout << "you want addition\n";
        break;
    case 2:
        cout << "you want subtraction\n";
        break;
    case 3:
        cout << "you want multiplication\n";
        break;
    case 4:
        cout << "you want division\n";
        break;
    default:
        cout << "you entered wrong operation\n";
        break;

    }

    cout << "You selected option #" << selection << "\n";
    system("pause");
    return 0;
}
apalomer
  • 1,895
  • 14
  • 36
Aira
  • 1
  • 2
    Because of the `do...while` condition, 1 <= selection <= 4 so the `default` will never be needed. – 001 Dec 27 '17 at 19:00
  • 1
    think of what would happen in your `do-while` condition for say, input 5 – Aditi Rawat Dec 27 '17 at 19:00
  • @user0042 Sorry but i don't understand how this is a duplicate, **here** OP's issue concerns faulty `do-while` loop condition not a faulty `switch` block – Aditi Rawat Dec 27 '17 at 19:06
  • @AditiRawat Then the question title should probably changed. – user0042 Dec 27 '17 at 19:07
  • @user0042 Yes the title **is** misguiding. – Aditi Rawat Dec 27 '17 at 19:08
  • @AditiRawat for 5, it just prints out again all the code ... do i need to change the while condition to include it? – Aira Dec 27 '17 at 19:18
  • @Aira exactly, for any input other than 1,2,3,4 your do-while condition becomes true and again goes back into the do-while block. You need to change your condition accordingly. – Aditi Rawat Dec 27 '17 at 19:21
  • @Aira Improve your question title as mentioned please. If the duplicate's answers don't solve your problem, [edit] your question and clearly state why not. – user0042 Dec 27 '17 at 19:23
  • @AditiRawat okay so i tried with simple while loop and it got even worse. Can you tell me how should i alter this while condition here? – Aira Dec 27 '17 at 19:26
  • @Aira the solution to your problem; learn C++ - start by reading [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jesper Juhl Dec 27 '17 at 19:29
  • 2
    @Aira You do not need a `default` as your `while` condition guarantees the input will be in the range [1-4]. – 001 Dec 27 '17 at 19:29
  • I think you should just follow @JohnnyMopp's comment! – Aditi Rawat Dec 27 '17 at 19:30
  • 1
    "What should be the position of default" - it doesn't matter. – Jesper Juhl Dec 27 '17 at 19:32
  • @JohnnyMopp thank you for making the point clear. much appreciated – Aira Dec 27 '17 at 19:32
  • @Aira It's quite obvious you want to move that `default` statements into your `do .. while()` loop. I'd recommend to catch the condition once in a `bool variable`, and use that one to decide if the user should be noticed about the invalid input and if the loop can be stopped. – user0042 Dec 27 '17 at 19:32
  • @user0042 got it. thanks – Aira Dec 27 '17 at 19:40

1 Answers1

0

Your do while loop makes it impossible for the program to proceed with the value of "selection" different than 1,2,3 or 4. In that case your switch statement can only reach your 4 defined cases and cannot reach the default case. You could change your code by excluding the do while loop and just make the input possible only once.