Thread: switch won't break

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    12

    switch won't break

    i am writing a program for class and a switch i have in it won't break, but only one case won't break when i enter the sentinel value it goes back to the menu but the other case exits when the sentinel is entered and the default case works properly

    Code:
    switch(choice){
    		case 1:
    			printf("Enter the length of the base(9999 to quit):\n");
    			scanf("%lf", &base);
    			while (base != 9999){
    				printf("Enter the length of the height:\n");
    				scanf("%lf", &height);
    				while(height < 0 || base < 0 ){
    					printf("Invalid input!\n");
    					printf("Enter the length of the base:\n");
    					scanf("%lf", &base);
    					printf("Enter the length of the height:\n");
    					scanf("%lf", &height);
    				area = .5 * base * height;
    				printf("The area of the triangle is %.2lf\n", area);
    
    				printf("Enter the length of the base(9999 to quit):\n");
    				scanf("%lf", &base);
    				}
    			}
    			break;
    		case 2:
    			printf("Enter the length of side 1(9999 to quit):\n");
    			scanf("%lf", &side1);
    			while (side1 != 9999){
    				printf("Enter the length of side 2:\n");
    				scanf("%lf", &side2);
    				printf("Enter the length of side 3:\n");
    				scanf("%lf", &side3);
    				while(side1 < 0 || side2 < 0 || side3 < 0){
    					printf("Invalid input!\n");
    					printf("Enter the length of side 1:\n");
    					scanf("%lf", &side1);
    					printf("Enter the length of side 2:\n");
    					scanf("%lf", &side2);
    					printf("Enter the length of side 3:\n");
    					scanf("%lf", &side3);
    				}
    				perimeter = side1 + side2 + side3;
    				printf("The perimeter of the triangle is %.2lf\n", perimeter);
    
    				printf("Enter the length of side 1(9999 to quit):\n");
    				scanf("%lf", &side1);
    			}
    			break;
    		default: printf("Invalid input!");
    			break;}
    			break;
    the last break at the end outside of the braces is because the switch is inside a case of another switch
    plz help me if you can im new at this

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    nvm i figured it out but i don't know how to delete my thread i am also new to posting on forums

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    What was the answer? I'm curious. I can't see it readily. Something to do with buffering non-numeric input into scanf() maybe?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    no,
    i didn't post the whole code and i have a do while loop in there and in the condition i put the wrong value for the sentinel value so i changed it and it worked its kinda confusing cuz i have one sentinel value with a few different variables that it could be assigned to and all of them were right except the one that is used in the one case that kept looping so it would go through the do while loop then it would break and since the case values were the same as the first time they were input it would just skip to that one case everytime so it would seem to not break but it would then loop cuz the sentinel was wrong lol kinda confusing so im not 100&#37; thats what happened bt it seems right

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    so you wouldn't have been able to help cuz the actual problem wasn't shown

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    Philippines
    Posts
    3

    can u try this one....

    Code:
    switch(choice)
    {
    		case 1:
                                           {
    			printf("Enter the length of the base(9999 to quit):\n");
    			scanf("%lf", &base);
    			while (base != 9999)
                                                                    {
    				printf("Enter the length of the height:\n");
    				scanf("%lf", &height);
    				while(height < 0 || base < 0 )
                                                                                      {
    					printf("Invalid input!\n");
    					printf("Enter the length of the base:\n");
    					scanf("%lf", &base);
    					printf("Enter the length of the height:\n");
    					scanf("%lf", &height);
    				area = .5 * base * height;
    				printf("The area of the triangle is %.2lf\n", area);
    
    				printf("Enter the length of the base(9999 to quit):\n");
    				scanf("%lf", &base);
    				}
    			}
                                           }
    			break;
    		case 2:
                                              {
    			printf("Enter the length of side 1(9999 to quit):\n");
    			scanf("%lf", &side1);
    			while (side1 != 9999)
                                                                {
    				printf("Enter the length of side 2:\n");
    				scanf("%lf", &side2);
    				printf("Enter the length of side 3:\n");
    				scanf("%lf", &side3);
    				while(side1 < 0 || side2 < 0 || side3 < 0)
                                                                                  {
    					printf("Invalid input!\n");
    					printf("Enter the length of side 1:\n");
    					scanf("%lf", &side1);
    					printf("Enter the length of side 2:\n");
    					scanf("%lf", &side2);
    					printf("Enter the length of side 3:\n");
    					scanf("%lf", &side3);
    				           }
    				perimeter = side1 + side2 + side3;
    				printf("The perimeter of the triangle is %.2lf\n", perimeter);
    
    				printf("Enter the length of side 1(9999 to quit):\n");
    				scanf("%lf", &side1);
    			}
                                     }
    			break;
                                                
    		default: 
                                                   {
                                                       printf("Invalid input!");
    			}
    }
    
    can u try this one....

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indent the code properly. You can't expect us to read that unreadable code mess!
    And you failed to mention what was wrong.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    my indention is from visual c++ express
    it automatically indents like that
    plus i select all then do ctrl+k, ctrl+f and that indents it
    so the indention is not my fault sry

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    i could probably use more spacing though my code is kinda cramped

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by GiantFurby View Post
    my indention is from visual c++ express
    it automatically indents like that
    plus i select all then do ctrl+k, ctrl+f and that indents it
    so the indention is not my fault sry
    Seriously?!
    That is the first time I see Visual Studio indent so horribly...
    OK, I forgive you
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > my indention is from visual c++ express
    > it automatically indents like that
    Find the option which tells it to use tabs for indentation, and set it to "spaces only".

    Your super fancy IDE can make sensible choices over spaces and tabs, but it's invariably a sorry mess when posted online / printed on paper / sent in an email.

    In fact, whenever anyone other than you has to deal with it, problems set in.
    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.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I usually set my tabs to the tab character, but set the number of spaces per tab down to two. As far as posting stuff online, I have a separate program that takes clipboard data and converts tabs to multi-spaces.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Salem View Post
    > my indention is from visual c++ express
    > it automatically indents like that
    Find the option which tells it to use tabs for indentation, and set it to "spaces only".
    Sorry, no, that doesn't work in Visual Studio.
    It will replace the tabs with spaces, so you have to press backspace 4 times to get rid of one tab.
    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.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It will replace the tabs with spaces, so you have to press backspace 4 times to get rid of one tab.
    Or use SHIFT + TAB instead of a backspace.
    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

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    As a fan of the arrow keys (which my gut tells me Elysia is one too), I just dislike using spaces over tabs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  3. right use of switch(), case and break statements
    By Niara in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2006, 07:29 AM
  4. MDICLIENT Problem
    By loko in forum Windows Programming
    Replies: 7
    Last Post: 01-16-2005, 10:42 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM