Thread: case switch problem

  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
    28,413
    How does it not work?

    The possible problem I see is that for case 'y', repeat() does not return a value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 09: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