Thread: switch case

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    switch case

    ok im trying to make a calculator but i dont know how to use say


    type a when u want to add
    and type b when u want to subtract i want to surprise my parents but i need to know this

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use a char variable. Read the user's input into the char variable. Then use switch (variable) and case 'a', case 's', etc. Make sure to break after each case. Also be careful about the fact that 'a' and 'A' are different characters, so you will have to handle both if you want to handle both.

  3. #3
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    to handle the probs of 'a' and 'A', just put it like this:

    case 'a':
    case 'A':

  4. #4
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    to handle the probs of 'a' and 'A', just put it like this:
    Code:
    case 'a':
    case 'A':
                      statements goes here;
                      break;
    
    case 'b':
    case 'B':
                      statements goes here;
                      break;
    etc...

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    um i got confused at a does anyone have a short example of one maybe something easy like you press enter it says hello then it asks if you want to change it to something else like hey but if it takes more than 6 minutes just forget about it i dont know much about coding so wut i think takes seconds could take hours

  6. #6
    1479
    Join Date
    Aug 2003
    Posts
    253
    Code:
    char x;
    
    cout <<"Hello world!" <<endl;
    cin >>x;
    switch (x){
           case 'a':
                        //  Do addition
                        break;
           case 'A': 
                        // Do addition
                        break;
           case 'd':
                       // Do division
                      break;
           case 'D':
                      // Do division
                      break;
            default:
                     cout <<"Invalid value" <<endl;
    }
    hope this helps
    Last edited by RealityFusion; 09-22-2005 at 02:02 AM.
    Knowledge is power and I want it all

    -0RealityFusion0-

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use char instead of int so that the letter will be read in properly.

  8. #8
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    as long as it gets the job done then its all good.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you are referring to char vs. int, then int would not get the job done in this case. The code wouldn't work.

    In general, something that gets the job done for now isn't all good if there is a more correct solution that will cause fewer problems in the future.

  10. #10
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    lol omgz really???? that is so c00l!1!!!!!!

  11. #11
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    can char store alphanumeric or just characters?

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    im getting majorly confused im gonna make this a poll to see whether int or char is better im hearing different results

    edit never mind i cant make a poll

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A char variable really just holds a small integral number. It is generally used to refer to characters from a particular character set, like ASCII. If you look at an ASCII chart, you will see the characters that can be held by a char by most platforms using the ASCII character set (which yours almost certainly is). In this character means something different than the just alphabetic letters like A-Z. Other characters include each digit ('0', '1', '2', ... '9' are all characters), lots of special characters (e.g. a newline '\n', tab '\t' and space ' '), and other non-printable characters.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> im getting majorly confused im gonna make this a poll to see whether int or char is better im hearing different results

    No need to make a poll. If you don't believe me, just try the code yourself. It won't work with int.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    no i believe you but i also believe everyone else but how would i use char


    and i dont mean to be a dirty doopy doop but i use dev
    Last edited by lilhawk2892; 09-21-2005 at 07:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM