Thread: switch cases and numeric operators issue

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    switch cases and numeric operators issue

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        const int currentYear = 2010;
        string name;
        int yearBorn;
        int age;
        cout<<"Enter your name: ";
        cin>>name;
        cout<<"Oh, what a nice name, "<<name<<"."<<endl;
        cout<<"When were you born? ";
        cin>>yearBorn;
        age = currentYear - yearBorn;
        cout<<"Oh, so you're "<<age<<" years old?"<<endl;
        cin.get();cin.get();
    
        string msg;
        switch(age)
        {
            case (10):
                msg = "Oh, you're just a little kid,,, How cute!";
            break;
        }
    
        cout<<msg<<endl;
    
    }
    Program works fine, but i'd like the switch's case to work with numeric operators.
    In the example I made so the case would only store that message if the entered age was 10, but i'd like to work as "< 10", so that message would be printed if the user's age was lower than 10. How could i make this switch case work with numeric operators?

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    You could use the dangerous bleeding-case setup:
    Code:
    switch (value) {
      case 1:
      case 2:
      case 3:
      ..
      case 9:
        //code for < 10
        break;
    }
    I mostly see this looked down upon, but for what you're doing you might just want to stick with if/else, especially if that's the only case you're working with...
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    I think that, if that really is what you want, you should use if-else cases. It will simply be more understandable and efficient.

    Edit: Sorry, my answer is late

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Code:
    stick with if/else, especially if that's the only case you're working with
    no, thats not the only case i'm working with, ive only made one because i couldnt figure out how to do the switch case work with numeric operator

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    Right, and you really can't do it in a 'decent' or 'wise' way by simply falling through all possible cases. What if you have to check for something that is >100 and <300 ?

    Sometimes if-else cases are just better.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    1
    Code:
    jus try using a temproary variable for the age that s less than 10 an the switch tat variable. 
    like
     if(age<=10)
      j=0;
    switch(j)
    {
    case 0:
     msg = "Oh, you're just a little kid,,, How cute!";
            break;
        }
    
        cout<<msg<<endl;
    
    }

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    mithr56, the suggestion you offered can cause problems if the value of 0 (or any other value you choose) could have a separate meaning aside from assigning it like you do (such as 0 oftentimes being implicitly interpreted as false). switch statements are best used when working with a distinct set of possible values. if you are looking for ranges, your code and its future readers will benefit greatly if you stick with if/else.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed