my default in my switch case statement doesn't seem to work, so if i type in a non integer, it will exit, and follow case 0. there are no signs of the default working.. how to solve, i've tried alot! :(

bool isFinished = false;
while(!isFinished) {
  .......
  ....
  ....
            int input = 0; 
    cin >> input;



    cout << "--------------------------------" << endl;

    switch(input) {
    case 1 : doPolygonPointEntry(); break;
    case 2 : doslopeIncrementEntry(); break; 
    case 3 : doParticleSizeIncrementEntry(); break;
    case 4 : doComputeDistancesEntry();break;
    case 5 : doComputeWettedPerimeter();break;
    case 6 : doComputeAreaEntry();break;
    case 7 : doComputeHRadius(); break;
    case 8 : doComputeFlowDischarge(); break;
    case 9 : doSeeAllValues (); break;
    default :  cout << "Unknown option" << endl; break; 
    case 0 : cout << "Thanks. Bye Bye. " << endl; isFinished = true; 
    }

Recommended Answers

All 3 Replies

I don't think you should try to catch this behavior (non integer entered) with a switch. You should look into the "isdigit" function:
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

Another thing to try is cout << input; to see what input actually is.

I don't think you should try to catch this behavior (non integer entered) with a switch. You should look into the "isdigit" function:
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

Another thing to try is cout << input; to see what input actually is.

so something like this:?

    int input = 0; 
    cin >> input;

    int isdigit(int c);
     if (int isdigit(int input)) {
        cout << "Unknown option" << endl; 
    return 0;          
     }

i checked the value of the input, even when i type in a letter, input is 0
:(

Please use code tags when you post code.

I don't think you should read the input as an int. I'd read it as a string then you can do whatever you want with it:

std::string input; 
cin >> input;

if(input.length() > 1)
{
std::cout << "You entered more than 1 digit";
exit(-1);
}

if(!isdigit(input[0]))
{
std::cout << "You must enter a numeric digit (0-9)";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.