Thread: While loop not skipping when condition isn't met

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    While loop not skipping when condition isn't met

    Code:
    #include <stdio.h>
    
    void calc_duty(int, float, float, float *, float *);
    void print_duty(int, int, int, float, float, float, float);
    
    
    int main (void)
    {
        int origin, category, quantity;
        float price, ship_price, duty;
        float tax;
        char looping;
    
    
    
    
        printf("Do you have more customs form to process(type: Y for yes, N for no)?\n"); 
        scanf("%c",&looping);
        while ((looping != 'Y') && (looping != 'N')) 
        { 
              printf("You have to  enter Y for Yes or N for No. Please try again. \n");      
            /* Discard the end of line character or other characters */ 
               scanf("%c",&looping);
        }
    
    
    
    
        while ((looping = 'Y'))
        { 
            printf("What is the origin of the goods (type: 1 for US, 2 for China, and 3 for Brazil)?\n");
            scanf("%d", &origin);
            printf("What category of goods (type: 1 for food, 2 for clothing, and 3 for wood)?\n");
            scanf("%d", &category);
            printf("What quantity of goods?\n");
            scanf("%d", &quantity);
            printf("What is the unit price?\n");
            scanf("%f", &price);
    
    
            if (origin=1)
            {
                if (category=1)
                {
                    tax=0.00;
                }
                if (category=2)
                {
                    tax=0.00;
                }
                if (category=3)
                {
                    tax=0.05;
                }
            }
            if (origin=2)
            {
                if (category=1)
                {
                    tax=0.02;
                }
                if (category=2)
                {
                    tax=0.03;
                }
                if (category=3)
                {
                    tax=0.04;
                }
            }
            if (origin=3)
            {
                if (category=1)
                {
                    tax=0.01;
                }
                if (category=2)
                {
                    tax=0.02;
                }
                if (category=3)
                {
                    tax=0.08;
                }
            }
    
    
            calc_duty(quantity, price, tax, &ship_price, &duty);
            print_duty(origin, category, quantity, price, tax, ship_price, duty);
    
    
            printf("Do you have more customs form to process(type: Y for yes, N for no)?\n"); 
            scanf("%c", &looping);
            while ((looping != 'Y') && (looping != 'N')) 
            { 
              printf("You have to  enter Y for Yes or N for No. Please try again. \n");      
            /* Discard the end of line character or other characters */ 
               scanf("%c",&looping); 
            }
            
        }
    
    
        printf("You have exited the loop.\n");
    
    
    }
    
    
    
    
    void calc_duty(int quantity, float price, float tax, float *ship_price, float *duty)
    {
        *ship_price=quantity*price;
        *duty=*ship_price*tax;
    }
    
    
    void print_duty(int origin, int category, int quantity, float price, float tax, float ship_price, float duty)
    {
        printf("Origin    Category    Quantity    Unit Price    Shipment    Tax Rate    Duty\n");
        printf("%d        %d          %d           %0.02f       %0.02f       %0.02f      %0.02f      ", origin, category, quantity, price, tax, ship_price, duty);
    }

    For some reason when I get to:

    Code:
    printf("Do you have more customs form to process(type: Y for yes, N for no)?\n");
    and put N it doesn't skip my while loop even though the condition is that looping=Y

    Edit: Just to clarify; the loop that checks if looping isn't equal to Y or N works. The larger loop is the one that doesn't.
    Last edited by MNM1245; 11-04-2012 at 05:03 PM.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    scanf() wasn't designed for user input.
    Use fgets() instead; and parse the input afterwards (with sscanf() or strtod() or ...).

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Quote Originally Posted by qny View Post
    scanf() wasn't designed for user input.
    Use fgets() instead; and parse the input afterwards (with sscanf() or strtod() or ...).
    In the class I'm taking we have always used scanf for user input. It has always worked. Is there something with characters that makes it not work?

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    put a space in front of any character scanf you have. Ex.) scanf(" %c".....); <---- notice the leading space before the %

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Quote Originally Posted by camel-man View Post
    put a space in front of any character scanf you have. Ex.) scanf(" %c".....); <---- notice the leading space before the %
    This unfortunately was not the solution. Would have been nice if it had been so easy.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by MNM1245 View Post
    In the class I'm taking we have always used scanf for user input. It has always worked. Is there something with characters that makes it not work?
    You might like to read the comp.lang.c FAQ, particularly section 12.

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Check your conditions ex while ((looping = 'Y')) should be == not =. You need to change them for your if statmentes as well.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Quote Originally Posted by camel-man View Post
    Check your conditions ex while ((looping = 'Y')) should be == not =. You need to change them for your if statmentes as well.
    camel-man, I love you! That was the issue. Can't believe it was something so stupid. Thanks a ton guys. Problem solved!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what condition in the while loop?
    By joeman in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2010, 11:49 AM
  2. Program skipping while loop
    By JFonseka in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 03:30 AM
  3. need while(for(loop)) to be condition
    By thestien in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2006, 08:32 AM
  4. Skipping a loop?
    By axon in forum C++ Programming
    Replies: 0
    Last Post: 02-16-2003, 07:52 PM
  5. help with loop condition
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-23-2002, 12:18 PM