Thread: Having trouble with while loop

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    13

    Having trouble with while loop

    I'm still a noon trying to teach myself C and I'm using the Prada C Primer book to help me along. This program is supposed to have you choose a pay rate then calculate the gross tax and net then display them. It is supposed to exit if a 5 is entered to quit but the loop isn't behaving as I expected. Can someone please explain how and where I went wrong?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    #define RATE1 .15
    #define RATE2 .20
    #define RATE3 .25
    
    
    #define GROSS1 300
    #define GROSS2 450
    
    
    #define TAX1 (GROSS1 * RATE1)
    #define TAX2 (TAX1 + (RATE2 * (gross - GROSS1)))
    #define TAX3 (TAX1 + TAX2 + (RATE3 * (gross - GROSS2)))
    
    
    #define WAGE1 8.75
    #define WAGE2 9.33
    #define WAGE3 10.00
    #define WAGE4 11.20
    
    
    int pay_Rate;
    float hourz, gross, net, tax, wage;
    
    
    int main(int argc, const char **argv)
    {
        printf("*****************************************************************\n");
        printf("Enter the number corresponding to the desired pay rate or action:\n");
        printf("1) $8.75                            2) $9.33\n");
        printf("3) $10.00                           4) $11.20\n");
        printf("5) quit\n");
        printf("*****************************************************************\n");
        
        while((scanf("%d", &pay_Rate) > 0) || (scanf("%d", &pay_Rate) < 5) || (scanf("%d", &pay_Rate) != 5))
        {
            scanf("%d", &pay_Rate);
            
            switch(pay_Rate)
            {
                case 1:
                    wage = WAGE1;
                    break;
            
                case 2:
                    wage = WAGE2;
                    break;
            
                case 3:
                    wage = WAGE3;
                    break;
            
                case 4:
                    wage = WAGE4;
                    break;
            
                case 5:
                    printf("quit\n");
                    continue;
            
                default:
                    printf("you entered an invalid number.\n");
                    printf("Enter a valid number.\n");
                    continue;
            }
        
            printf("\nNumber of hourz worked this week: ");
            scanf("%f", &hourz);
        
            if(hourz > 40)
                gross = ((hourz - 40) * (1.5 * wage)) + (40 * wage);
            else
                gross = hourz * wage;
        
            if(gross <= GROSS1)
                tax = RATE1 * gross;
        
            else if(gross <= GROSS2)
                tax = TAX2;
        
            else
                tax = TAX3;
        
            net = gross - tax;
        
            printf("gross is %.2f minus tax %.2f is a net of %.2f.\n", gross, tax, net);
            printf("\nEnter another pay rate: ");
        }
        printf("end of run");    
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while((scanf("%d", &pay_Rate) > 0) || (scanf("%d", &pay_Rate) < 5) || (scanf("%d", &pay_Rate) != 5))
    This may call scanf three times - which is not what you want.
    Also in this case, the only return results possible are 0, 1 and EOF.

    Maybe
    while((scanf("%d", &pay_Rate) > 0) && pay_Rate != 5 )
    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.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    13
    Thank you @Salem! That did the trick. That was a complete brainfart!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with FOR-Loop
    By Xinkei in forum C Programming
    Replies: 9
    Last Post: 01-25-2012, 11:33 PM
  2. For Loop Trouble
    By Jackbenimble in forum C Programming
    Replies: 15
    Last Post: 02-11-2011, 03:04 PM
  3. even more loop trouble
    By mabufo in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 06:37 PM
  4. While Loop trouble.
    By Aterxerxes in forum C++ Programming
    Replies: 6
    Last Post: 10-04-2005, 03:59 AM
  5. Loop Trouble
    By pujuman in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2005, 06:59 PM

Tags for this Thread