Switch, case more than 1 time

Hi, I have a program thats input a number and than output a number.
If number is three, than programm exit, but only first time. I need a program to exit every time i enter a number 3.

First time i enter other number than 3.
Second time if i enter 3 , my programm continues working.

Example

#include <iostream>
using namespace std;
int main() {

int a;

cout << " Enter a number " <<endl;

cin>>a;

switch(a) {

case 3:

cout << " You have entered 3, programm exit " << a << endl;

return 0;

default: cout << " Any other number than " << a << endl;

cout << " Enter a number " <<endl;

cin>>a;

}
}
}


Last edited on
Do you need to use a switch, because an if works just as well here, and for looping you need a loop.
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main() {
    int a;
    do {
        cout << "Enter a number: ";
        cin >> a;
        if( a == 3 ) {
            cout << "You entered 3. Bye!\n";
            break;
        } else
            cout << "You did not enter 3, you entered " << a << ". Try again!\n";
    } while( true );
    return 0;
}
Thank you.
Topic archived. No new replies allowed.