Thread: Continue and switch

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

    Unhappy Continue and switch

    Hi All

    I am new to c programming and trying to figure out why I cannot break out of a loop. I have used an if statement and want to use the continue and break. Now I know if I just use the break it will terminate and if I just use the continue yes it continues. My question is should I just be using one or the other(continue or break) but then how to I continue ot break? or is it better for me to rename the while. I have tried a switch statement and that doesn't work what ever statement I put first it does this should be telling me something but i cant think why. I'm just confused now, was just thinking about using a for loop? any pointers would be great many thanks for reading and you time.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int one = 1, two = 2, three = 3, i = 3, number;  //Declare and Initialise Global Variables 
    float grossPay, pay, netPay;
    const float TAX = 4.800; //Use constant as these values will never change
    const float BONUS = 0.25; // Bonus payment to add to grosspay when tax is deducted
    char answer; 
    
    int main (void) //Main Function
    {
    
      printf("\n\t\tANNUAL GROSS PAY MAIN MENU\n");	//prepare menu list for user input
      printf("\t\t1. Display Conversion Table Goss Pay\n");
      printf("\t\t2. Process Pay Value\n");
      printf("\t\t3. Exit\n\n");
      printf("\n\t\tEnter Option");
      scanf ("%d",&number);
      
      while (number > i)  // while loop to ensure user enters the correct number, if not throw   invalid option
      {
    	i--;
    	printf ("\n\t\tINVALID OPTION");
    	printf("\n\t\tEnter Option " );
           scanf ("%1s", number);
       }
    	
      
    			if (one == number) 
                            { 
    				printf("\n\t\t CONVERSION TABLE\n\n"); 
                             } // list screens, use if statement to switch between screens
     
    			else if (two == number) 
                             { 
    				printf("\n\t\t COMPANY PAYROLL RECORDS\n\n"); 
                             }	
      
    			else if (three == number) 
                            { 
    				printf("\n\t\tEND OF PROGRAM\n\n"); 
                             }
    
    
    //Calculate GrossPay
    
    while (number == one)
    {
    
    	printf("\n\t\t GROSS_PAY");
    	printf("\n\t\t Enter Gross Pay " );
    	scanf("%f", &grossPay);
    	
    	pay = (grossPay - TAX);		   //Calculate gross pay leaving remainder
    	netPay = (pay * BONUS) + pay; // use remainder * bonus and add pay
    	
    	printf("\n\t\t NET_PAY");
    	printf("\n\t\tYour Net Pay %.3f\n", netPay);
    
    		
    	printf ("\n Would you like to continue [Y/N] ");
    	scanf("%c", &answer);
    	getchar();
    			
    			if (answer == 'Y'|| 'y'){
    					continue;
    					}
    			else if (answer == 'N'||'n'){
    					break;
    					}
    						
    			
    }	//End of While	
    	    	
       return 0; 
    } // End of Program

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't see a good reason for i-- inside your first while loop.

    In this case, you appear to want your while loop (in the number==one case) to really be controlled by answer.

    As to switch, switch goes like:
    Code:
    switch(number) {
        case one:  //case one goes here
            break;
        case two: //case two goes here
            break;
        //and so on
    }

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    California
    Posts
    19
    Make sure you have the right value in answer. Did you correctly read it? Your use of continue and break are correct, so try looking a little bit higher up. Try looking at the if, and think of what may happen for it to always be false - or always true!

    In addition to what tabstop said about switch, I'd like to make a point that the break statement after each case is essential, or else the program will evaluate every statement for every case after the one it starts at. This will do this until the end or until it hits a break.

    Man, I wish tax was just a constant.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    			if (answer == 'Y'|| 'y'){
    					continue;
    					}
    			else if (answer == 'N'||'n'){
    					break;
    					}
    This won't do what you think it does.
    'n' and 'y' are always true, so the resulting ifs will always be true.
    You need to do answer == 'X' || answer == 'Y'.

    And your indentation is horribly messed up. What IDE / editor do you use?
    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.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Another alternative to what Elysia suggested is doing something like

    Code:
    if(toupper(answer) == 'Y')
    But I won't start suggesting too much until you learn to post code that is user readable. To be honest, I like to have my IDE print '\t' characters. However, I always convert to spaces when I post code from my projects. One of these days when I am really bored I will write a firefox plug-in that reparses copied and pasted source code.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Actually, I wasn't suggesting a solution, but merely pointing out why it doesn't work as expected and how to fix that little problem.
    It was related to how you would use OR with an if with multiple conditions.
    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.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I suppose... "Dude your code is f**ked" is not necessarily a suggestion to some, but if someone told me that, I would take it as constructive criticism. But I am optimistic like that.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Thank you so much, all of you I was'nt expecting to see so many replies, have been busy so taking on board what you have said im now going back to my program and look at the suggestions given. Apologise about my code I am using Xcode again this is a new IDE for me so learning this aswell as C I can't find at the moment where to format the text? many thanks again you've been very helpful

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is possible to format the text yourself. One tab for every {.
    But there are perfectly good other IDEs out there, too.
    Visual Studio is the best one I know to format code for 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.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Many thanks

    Thanks again Elysia, I will download this and use what you have suggested until then. I use Xcode as I am following a book, it recommended using this IDE plus I have a mac book. I have read the information you mentioned this was very interesting and will continue to use this throughout future exercises.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, Visual Studio is for Windows only.
    I don't know of IDEs for Mac, but perhaps this may help: http://cpwiki.sourceforge.net/IDE
    Remember indentation "rules" will also work, regardless of IDE.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. switch() inside while()
    By Bleech in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 02:34 AM
  4. switch - continue
    By DavidP in forum C Programming
    Replies: 2
    Last Post: 06-24-2004, 10:09 AM
  5. Switch in a While Loop
    By WindShield in forum C Programming
    Replies: 6
    Last Post: 10-19-2002, 12:04 AM