case switch problem

This is a discussion on case switch problem within the C++ Programming forums, part of the General Programming Boards category; i wrote a program and it worked fine, but i added a case swich just so the user didnt have ...

  1. #1
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91

    case switch problem

    i wrote a program and it worked fine, but i added a case swich just so the user didnt have to reopen the program everytime he/she wanted to recaluclate, but it doesn't work. Can someone tell me whats wrong?

    Code:
    int repeat()
    {
        int yesno;
        
        cout << "\nWould you like to calculate again? y/n : ";
        cin >> yesno;
        
        switch( yesno )
        {
                  case 'y' :
                     start();
                     break;
                  
                  case 'n' :
                     return 0;
                     break;
                  
                  default:
                     cout << "Not an option!";
                     return repeat();
                     break;
        }
    }
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    That's because yesno is an integer not a character. Declare that as so:
    Code:
    char yesno;
    Then it should work.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,597
    How does it not work?

    The possible problem I see is that for case 'y', repeat() does not return a value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    it would go to the default and keep showing the samething over and over really fast, i didnt relize i decalered it an int.
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    The reason it's not working is becase the switch cases send LRARAM and WPARAM messages. Besides, how could you expect an int var to hold characters?

    Code:
    int repeat()
    {
    char yesno;
     
    cout << "\nWould you like to calculate again? y/n : ";
    cin >> yesno;
     
    if(yesno == 'y')
    start();
    else if(yesno == 'n')
    return 0;
    else{
    cout << "Not an option!";
    return repeat(); }
    }
    Might work.


    [EDIT] Ahh! Beat me to it! [/EDIT]

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    The over-use of recursion where a few loops would do seems to be a problem to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by laserlight
    The possible problem I see is that for case 'y', repeat() does not return a value.
    That's true, he doesn't, but how can that be a problem?

  8. #8
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Cool-August you can use switch statements like he is doing. It is so you don't have to have all those if statements. Like below:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        char yesno;
        cout<<"Enter y or n:";
        cin>>yesno;
        switch(yesno)
        {
                     case 'y':
                          cout<<"You entered a y\n";
                          break;
                     case 'n':
                          cout<<"You entered an n\n";
                          break;
                     default:
                             cout<<"You didn't enter a y or n\n";
                             break;
        }
        system("PAUSE");
        return 0;
    }
    That is just like 2 if statements and an else if statement, except shorter and better looking.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,597
    The over-use of recursion where a few loops would do seems to be a problem to me.
    Well, that depends on what start() does, I suppose, though I agree the recursion in calling repeat() isnt necessary at all.

    but how can that be a problem?
    That depends on what the OP is using the return value for, but yeah, it probably is the int yesno that's causing the problem described.

    It is so you don't have to have all those if statements.
    It'll probably make more sense when used to test both uppercase and lowercase characters, but then this is only a test snippet.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yes, it would but he didn't include them so I didn't either. <-Regarding the upper and lower case y and n.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  11. #11
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by jmd15
    Cool-August you can use switch statements like he is doing. It is so you don't have to have all those if statements. Like below:
    I wasn't aware of that. I always thought switch statements are is a windows thing, because they are used in API-GUI so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21