Switch/Case questions

This is a discussion on Switch/Case questions within the C++ Programming forums, part of the General Programming Boards category; Lets say i have a simple program like this: Code: #include <iostream> using namespace std; void displaytext(); void displaynumbers(); int ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Switch/Case questions

    Lets say i have a simple program like this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void displaytext();
    void displaynumbers();
    
    int input;
    
    int main()
    {
          cout<<"What do you want to do?\n\n";
          cout<<"1. Display Text.\n\n";
          cout<<"2. Display Numbers\n\n";
          cout<<"3. Exit.\n\n";
          cout<<"Selection:";
          cin>> input;
          switch ( input ) {
                 case 1:
                     cout<<"Text!!!!!!\n";
                     cin.get();
                 case 2:
                     cout<<"Numbers!!!!!!\n";
                     cin.get();
                 case 3:
                     cout<<"Good bye!\n";
                     cin.get();
                 default:
                     cout<<"Bad input! lets try that again.\n";
                     cin.get();
                 }
    cin.get();
    }
    If you compile and run this code, you will notice that after either case has been executed, the program will exit.
    Without looping around the whole thing, is there a way to return to the beginning of the
    program after Case 1,2,3 or default have been executed. And start all over from the beginning without restarting the program?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    We don't need to compile that program to tell you that it wont loop, as your question isn't really concerning switch statements; You simply need to learn about loops.

    In C++ they come in 3 flavours: "do while", "while", and "for".
    I would recommend a "do while" in this case.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    You can use any of the loops mentioned by iMalc. You can try using goto statement. It is not recommended though as it makes a program complex. Use break; instead of cin.get(); as it is not a sure way. It can give problems. To use such a small function as displaytext() etc, you can use macros.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,270
    Quote Originally Posted by saqib_
    Use break; instead of cin.get(); as it is not a sure way. It can give problems.
    Good point, but the reason to use break here is not ecause cin.get() "can give problems", but because you probably do not want the fall through effect of a switch. If you do intend it, then break would not be appropriate.

    Quote Originally Posted by saqib_
    To use such a small function as displaytext() etc, you can use macros.
    Do not resort to a macro unless other options are worse. You can, for example, declare the function as inline if you deem fit, but it probably does not matter here.
    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

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Thanks very very much for the help. Looks like goto statements did the trick! ( and a do while loop in case users never select exit, it wont go on forever) I probably did not need the do while loop, but im in those beginning experimental stages so, what the heck. I had never learned about goto statements before, i find them to be very useful, and even easier to learn (Basic usage anyway). Even with the actual code im working on (MUCH more complex than my example) i still had no problems. You guys are the best.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,270
    NOOO!!!!

    Please avoid the use of goto. When you are more experienced you might be able to better judge when to use them, but at the moment if you rely on goto, your code will become unreadable!
    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

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    51
    Quote Originally Posted by laserlight View Post
    NOOO!!!!

    Please avoid the use of goto. When you are more experienced you might be able to better judge when to use them, but at the moment if you rely on goto, your code will become unreadable!
    Lol. Seems like saqib_ did more bad than good here by introducing the idea of a goto.

    kbzombie, if you're not ready to use loops then you're definitely not ready to properly use goto. One of the very very few places where a goto is considered acceptable is to break out of a set of deeply nested loops. Perhaps you should read up on goto a bit.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Lol. Seems like saqib_ did more bad than good here by introducing the idea of a goto.
    Yes. telling about goto statement to someone who is new to programming is a disaster, since he/she does not have a clue whatsoever about the dirty effects of using goto statement and rely heavily on using it. Although it is not recommended but still intelligent use of goto statement can work wonders (not for noobs of course).
    Last edited by hash; 04-07-2010 at 12:04 PM.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    I would not say any harm has been done. Like i said, im just experimenting. With the more complex stuff. But i guess experimentation is the cause of mistakes made while experimenting. I studied loops last week, i understand them well. The problem is knowing when to use a for, while, or do while loop when the difference is looping cout<<"Hello world!"; , and looping a switch statement with many cases. For loops are simple for me. Iv used them many times, it just does not give me the result im looking for in the program im working on. Do while would work, and i understand how and why, but i need to have the correct function at the end of each case. Because what iv tried so far does not seem to "Start the program over". I see goto was a bad way to go, break does not work the way i thought, yet to try return, besides return 0;, its to complex for macros. I think ill just put my .cpp file in my archives to work on when i have more experience.

    Okay! Lets sum up.

    You (may have) learned more about the subject: "Dealing with noobs."

    I learned: Listen to people who know what there talking about! (Im being truthful here), and just because you MAY do something, does not mean you CAN. Intill you get better at it at least.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    If you feel like you don't get loops correctly, then it's usually a logic problem.
    Try writing down the logic, then translating it to code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by kbzombie View Post
    The problem is knowing when to use a for, while, or do while loop when the difference is looping cout<<"Hello world!"; , and looping a switch statement with many cases.
    There's not a difference here. For loops work the same way, regardless of what's inside them.
    Quote Originally Posted by kbzombie View Post
    Do while would work, and i understand how and why, but i need to have the correct function at the end of each case.
    You should probably elaborate on this sentence, because as you have it written it makes no sense. There's no reason at all you would need to have "the correct function" (whatever that means, if anything) at the end of each case for a loop to work. (EDIT: Unless you mean "getting new input before restarting the loop", I guess. But the point there is that it should be after the switch, since you wouldn't want to write it in each case, as you say.)
    Quote Originally Posted by kbzombie View Post
    yet to try return, besides return 0;, its to complex for macros.
    Macros are just text substitution, not an actual function call, so using return inside a macro is Just Plain Wrong. (Unless you really intend to return from the function itself, and not from the macro.) Although I'm not sure why we're even talking about macros all of a sudden.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by kbzombie View Post
    I would not say any harm has been done.
    Harm was done when the idea popped into your head, well before you even posted the idea to spread the disease known as goto, to others.

    I can honestly say that it never entered my mind as I wrote my previous post. It simply isn't something worth considering.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc problem
    By Tool in forum C Programming
    Replies: 23
    Last Post: 03-12-2010, 10:54 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 08:22 AM

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