Thread: Help with problem

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Question Help with problem

    I'm having problem with the while loop and switch statements. I can run case 1 all the way through, but when I input case 2 the program ends, what am I doing wrong? Please help.


    Code:
     
    #include <stdio.h>
    
    void main(void)
    
    
    {	int paycode, hours;
    	float hourly_rate, weekly_pay, weekly_salary, weekly_sales;
    
    	
    	printf("Enter employee paycode (-1 to end): ");
    	scanf("%i", &paycode);	
    	
    	switch (paycode)
    	while (paycode = -1)
    	{
    		case 1:		
    			printf("	Enter the weekly salary: ");
    			scanf("%i", &weekly_salary);
    			weekly_salary = 400;
    			printf("The employee's weekly pay is $ %6.2f\n\n", weekly_salary);	
    			break;
    		case 2:
    			printf("	Enter the employee's hourly rate: ");
    			scanf("%f", &hourly_rate);
    			printf("	Enter the hours worked: ");
    			scanf("%i", &hours);
    			if(hours <= 40)
    			{
    				weekly_pay = hourly_rate * hours;
    				printf("The employee's weekly pay is $ %6.2f\n\n", weekly_pay);			
    			}
    			else if(hours > 40)
    			{
    				weekly_pay = (hourly_rate * 40) + ((1.5 * 12.50) * 5);
    				printf("The employee's weekly pay is $ %6.2f\n\n", weekly_pay);
    			}
    			break;
    		case 3:
    			printf("	Enter the gross weekly sales: ");
    			scanf("%i", &weekly_sales);
    			weekly_sales = 250 + (1000 * .057);
    			printf("The employee's weekly pay is $ %6.2f\n\n", weekly_sales);
    			break;
    	}
    		printf("Enter employee paycode (-1 to end): ");
    		scanf("%i", &paycode);	
    			
    		
    		
    
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I'm having problem with the while loop

    I'd say!


    >> while (paycode = -1)

    1) While loops don't get inserted in switch'es like that. You can put a while loop *after* a 'case' or 'default' label, but not otherwise.

    2) You are assigning paycode the value -1 here. Don't confuse '=' with '=='.

    Try something like:

    Code:
    int main(void) // main() *always* returns an 'int'.
    {
     int paycode = 0; 
     
     // code 
    
      while(paycode != -1)
     {
      printf("Enter employee paycode (-1 to end): ");
      scanf("%i", &paycode);	
      	
          switch (paycode)
         {
            // code    
         }
     }
    
     // code
    
     return 0;  // there's that 'int' :)
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    I kind of figured I had a problem. I'll try your suggestion. Thank you.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    I am still stuck, is that C++ code? I am programming in C, I'm not understanding what needs to b done, any other help please?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Be specific. What is the exact problem you are having at this point? Feel free to post code.

    >> I am programming in C

    Yes, that's C.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    when I run the program, I'm prompted for employee paycode and I type in 1 and then I enter the weekly salary of $400, it calculates the weekly pay just fine, it then prompts me to enter paycode I type in 2 and the program ends with press enter to continue. I'm using Visual Studio C++ to build C programs. Is there something wrong with my switch or if-else if statements also? I do appreciate your help.

    Code:
     
    #include <stdio.h>
    
    void main(void)
    
    
    {	int paycode = 0, hours;
    	float hourly_rate, weekly_pay, weekly_salary, weekly_sales;
    	
    	
    		printf("Enter employee paycode (-1 to end): ");
    		scanf("%i", &paycode);	
    	
    	switch (paycode)
    	while (paycode != -1)
    	{
    		case 1:		
    			printf("	Enter the weekly salary: ");
    			scanf("%f", &weekly_salary);
    			weekly_salary = 400;
    			printf("The employee's weekly pay is $ %6.2f\n\n", weekly_salary);	
    			break;
    		case 2:
    			printf("	Enter the employee's hourly rate: ");
    			scanf("%f", &hourly_rate);
    			printf("	Enter the hours worked: ");
    			scanf("%i", &hours);
    			if (hours <= 40)			
    			{	weekly_pay = hourly_rate * hours;
    				printf("The employee's weekly pay is $ %6.2f\n\n", weekly_pay);	
    			}	
    			else if (hours > 40)				
    			{	weekly_pay = (hourly_rate * 40) + ((1.5 * 12.50) * 5);
    				printf("The employee's weekly pay is $ %6.2f\n\n", weekly_pay);
    			}
    			break;
    		case 3:
    			printf("	Enter the gross weekly sales: ");
    			scanf("%i", &weekly_sales);
    			weekly_sales = 250 + (1000 * .057);
    			printf("The employee's weekly pay is $ %6.2f\n\n", weekly_sales);
    			break;
    	}
    		
    		printf("Enter employee paycode (-1 to end): ");
    		scanf("%i", &paycode);	
    			
    		
    }

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    #include <stdio.h>
    
    int main(void)
    {
     int paycode = 0, hours;
     float hourly_rate, weekly_pay, weekly_salary, weekly_sales;
    
    	 while (paycode != -1)
    	{
         printf("Enter employee paycode (-1 to end): ");
    	 scanf("%i", &paycode);
    
            switch (paycode)
    	   {
    	    case 1:
    			printf("	Enter the weekly salary: ");
    			scanf("%f", &weekly_salary);
    			weekly_salary = 400;
    			printf("The employee's weekly pay is $ %6.2f\n\n", weekly_salary);
    			break;
    		case 2:
    			printf("	Enter the employee's hourly rate: ");
    			scanf("%f", &hourly_rate);
    			printf("	Enter the hours worked: ");
    			scanf("%i", &hours);
    			if (hours <= 40)
    			{	weekly_pay = hourly_rate * hours;
    				printf("The employee's weekly pay is $ %6.2f\n\n", weekly_pay);
    			}
    			else if (hours > 40)
    			{	weekly_pay = (hourly_rate * 40) + ((1.5 * 12.50) * 5);
    				printf("The employee's weekly pay is $ %6.2f\n\n", weekly_pay);
    			}
    			break;
    		case 3:
    			printf("	Enter the gross weekly sales: ");
    			scanf("%i", &weekly_sales);
    			weekly_sales = 250 + (1000 * .057);
    			printf("The employee's weekly pay is $ %6.2f\n\n", weekly_sales);
    			break;
    
            default: paycode = -1; // assume the user wantred to bail out...
    
         } // end switch
    	 
       } // end while
    
     return 0; // all important return statement
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Why don't you try implementing the suggestions by Sebastiani then post the problems you are haveing with your revision.

    Your switch control structure shouldn't be interrupted by the while loop. As was stated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM