Thread: Looping in Switches

  1. #1
    Programming Newbie Kaminaga's Avatar
    Join Date
    Jun 2005
    Posts
    14

    Looping in Switches

    Okay, I'm just doing a bit of a revision in programming so bear with me. Here's what I'm trying. As you can see, if I choose 1, the output will be a continuous "You picked 1"s . So what I'm trying to do is looping through the switches e.g If I choose a number, it will output the number I chose and still give me the option to choose a number again.
    Code:
    #include <iostream>
    
    using namespace std;
    
    void main()
    {
    
    	int num;
    	cout << "1: Task1" << endl;
    	cout << "2: Task2" << endl;
    	cout << "0: exit" << endl;
    	cout << "Please choose a number" << endl;
    	cin >> num;
    	while(num!=0)
    	{
    		switch(num)
    			{
    				case 1:
    					cout <<"You picked 1" << endl;
    					break; 
    	
    				case 2:
    					cout <<"You picked 2" << endl;
    					break;
    
    				default:
    					cout <<"Please choose number 1 or 2" << endl;
    			}
    	}
    
    	cout << "You're outside the loop" << endl;
    
    
    }
    "Temporary constructs of the feeble human intellect, trying desperately to justify it's meaning or purpose.." -Agent Smith
    kaminaga.deviantart.com

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Perhaps a construct more like this is in order:
    Code:
    do
    {
       cin >> num;
       switch(num) { ... } etc.
    } while(num != 0);
    Note, you have to provide a case for num == 0 in the switch here, otherwise it falls into the default path.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can put your menu and user input inside an infinite while loop, and if the choice is 0, then break out of the while loop.

  4. #4
    Programming Newbie Kaminaga's Avatar
    Join Date
    Jun 2005
    Posts
    14
    Hey thanks Zach. It works fine now. I guess I understand why it works (I put a break in that line and debugged so I could see more). But first (just for fun), I'm picturing myself as a student and you guys are the mentors and I'm trying to exlpain it to show that I completely understand (but correct me if I'm wrong):

    1. If I had done this:
    Code:
    while(num!=0)
    	{
    		cin >> num;
    		switch(num)
    			{
    				(.....)
    			}
    	}
    The code won't work because the 'num' is defined by the user, but in the code, the time of when the user define this is inside the loop which, would coincide with the 'while num != 0" thus I will get an error.

    2. If I do this:
    Code:
    cin >> num;
    do
    	{
    		
    		switch(num)
    			{
    				(.....)
    			}
    	}while(num!=0);
    The output will be continuous because the "user define num" is outside the loop and from what I can think, is that it will only be defined once. This can also said that the data is not contained or handled properly.

    3. Finally, if I do this:
    Code:
    do
    	{
    		cin >> num;
    		switch(num)
    			{
    			    ........
    			}
    	}while(num!=0);
    Everything looks okay. When I picked a task, the loop will start over at cin >> num until I pressed 0. Putting the 'while' statement at the end makes the loop run properly Right? And yes, I do have to put the case 0 or it will treat it as default.

    Another thing. I tried putting a character e.g 'z', and the program went to a crazy continuous loop. I know since the switch only accepts 'ints' but why does it go crazy like that? and is there a way to allow the user to input an interger as well as a character?

    Thanks again for the help. Just trying to run slowly through programming so expect more student questions
    -Kaminaga
    Last edited by Kaminaga; 07-26-2005 at 02:50 PM.
    "Temporary constructs of the feeble human intellect, trying desperately to justify it's meaning or purpose.." -Agent Smith
    kaminaga.deviantart.com

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You're on the right track certainly. I'm not quite sure your right on though, but that could just be the wording, so here's a quick run-down.

    There are three primary loop constructs: for, while, and do... while. Ignoring for, since it is not the proper one for this type of problem, we have the following two choices:
    Code:
    while(cond) {
       statements;
    }
    .. and ..
    do {
       statements;
    } while(cond);
    The difference between these two is simply where the condition is evaluated. So, as the syntax would suggest, 'cond' is evaluated before the statements in the body of the loop are executed, and if it evaluates to false, then the loop is terminated without executing the statements (so, even if the statements modify the condition, they are not given the chance to do so). This means, the body of a while loop may not execute at all. With do-while, however, the statements are executed, and then the condition is evaluated, so the loop body is always executed at least once, and, as in this case, they have the chance to initialize the condition.

    So, you could use a while loop, you would just need to ask your question outside the loop (to initialize the condition, so a valid condition is evaluated by the loop), and then again inside.
    Code:
    cin >> num;
    while(num != 0) {
       switch(num) { ... }
       cin >> num; //Note that this is now at the end...
    }
    Of course, there is no need for this duplication in this case, as there is a more elegant construct with do-while.

    As for the I/O problem, here is a good bit explanation: http://www.parashift.com/c++-faq-lit....html#faq-15.3

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Programming Newbie Kaminaga's Avatar
    Join Date
    Jun 2005
    Posts
    14
    Quote Originally Posted by 7stud
    You can put your menu and user input inside an infinite while loop, and if the choice is 0, then break out of the while loop.
    I just noticed this reply. Thanks a lot. Played around with it and works fine.


    Also, cheers to Zach for the explanation and the good link. So many cool things about C++ over there that I need to read.

    -Kaminaga
    "Temporary constructs of the feeble human intellect, trying desperately to justify it's meaning or purpose.." -Agent Smith
    kaminaga.deviantart.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  3. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  4. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM
  5. Microswitch joysticks and arcade switches
    By Scourfish in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-30-2001, 07:50 PM