Why is this while loop repeating the last iteration of arithmetic when exiting

This is a discussion on Why is this while loop repeating the last iteration of arithmetic when exiting within the C Programming forums, part of the General Programming Boards category; I am not exactly sure what is going on here. The code seems to function the way I expect it ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Why is this while loop repeating the last iteration of arithmetic when exiting

    I am not exactly sure what is going on here. The code seems to function the way I expect it to except that when I exit the while loop it repeats the math of the last run through of the loop. Sorry if this is a stupid question because I am not exactly sure what is happening inside the conditional parenthesis next to the "while".


    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
        char designator;
        int arrive, depart, cost_for_this_auto, total_seniors, total_cars, total_trucks, number_of_hours, total_hours_all_autos, total_profit_all_autos=0;
        int x;
        
    printf("Welcome to XYZ parking! Please hit return to enter the parking lot.\n"); 
        
        while((x=getchar())!=EOF)
    //while(scanf("%c %d %d",designator,arrive,depart)!=EOF)
        {
        
    printf("If you are a senior enter S, otherwise if you have a car enter C or if you have a truck enter T:\n");
            scanf("%c", &designator);
                
    printf("Please enter arrival time in military format 0600-2200\n");
            scanf("%d", &arrive);
       
    printf("Please enter departure time in military format 0600-2200\n");
            scanf("%d", &depart);
        
            number_of_hours = ((depart-arrive)/100);
      
            if (designator == 's')
            {
                total_seniors++;
                cost_for_this_auto=0;
            }
        
            else if (designator == 'c')
            {
                total_cars++;
                if (number_of_hours <= 2)
                    cost_for_this_auto = 0;
                else if (number_of_hours <= 5)
                    cost_for_this_auto = (number_of_hours-2)*.5;
                else
                    cost_for_this_auto = ((number_of_hours-5)*.25)+1.50;
            }
        
            else if (designator == 't')
            {
                total_trucks++;
                if (number_of_hours <= 1)
                    cost_for_this_auto = 0;
                else if (number_of_hours <= 3)
                    cost_for_this_auto = number_of_hours-1;
                else
                    cost_for_this_auto = ((number_of_hours-3)*.75)+2;
            }
        
            total_profit_all_autos=total_profit_all_autos+cost_for_this_auto;
            
    printf("you arrived at %d and departed at %d.\n Your total cost for parking today was $%d for %d hours of parking.\n Thank you come again!\n\n", arrive, depart, cost_for_this_auto, number_of_hours);
            
        total_hours_all_autos+=number_of_hours;
        
        }
        
        
        
    printf("total hours all cars were parked=%d \n total seniors=%d \n total cars=%d \n total trucks=%d \n total profit=$%d", total_hours_all_autos, total_seniors, total_cars, total_trucks, total_profit_all_autos);
        
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are expecting one character, but you are actually entering two. You forgot that when you type the 's', you also hit enter.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    EOF is a really lousy character to test for an exit...
    Also you never prompt for the exit...

    Try making your while() loop into a do-while() so the exit condition is at the bottom of the loop.
    Make a prompt for the user Traditionally ... Continue (y/n) ... don't just leave them hanging there.
    Test for a Y or a y, assume everything else is a no.

    Code:
    do
      {  
    
         // codesmut
    
       printf("Do you want to continue (y/n) :");
      }
    while (tolower(getchar()) == 'y');

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Part of the assignment was to have it end that way.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Quote Originally Posted by quzah View Post
    You are expecting one character, but you are actually entering two. You forgot that when you type the 's', you also hit enter.


    Quzah.

    So does that mean the second character is being evaluated? Or what?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Charlie Lesoine View Post
    Part of the assignment was to have it end that way.
    End on EOF? Wow that's totally strange... nobody uses CTRL-Z to exit programs anymore.

    ... BECAUSE... most operators wouldn't have the first clue how to use it.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Quote Originally Posted by CommonTater View Post
    End on EOF? Wow that's totally strange... nobody uses CTRL-Z to exit programs anymore.

    ... BECAUSE... most operators wouldn't have the first clue how to use it.

    Yes...my instructor is very strange....

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by CommonTater View Post
    End on EOF? Wow that's totally strange... nobody uses CTRL-Z to exit programs anymore.
    Most Unix programs work this way, it's not that strange. It lets them work the same whether you type the input yourself, grab it from a file or redirect the output of one program as the input of the next. It's a really powerful concept that you're missing out on if you're used to having to code up special cases for each of these different cases.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    I tried putting a fflush(stdin) at the end of the while loop and at a few other places. That did not work. In order for this program to function the way I wanted it to I had to add this outside underneath the while loop:

    Code:
        if (designator=='s')
            total_seniors--;
        if (designator=='c')
            total_cars--;
        if (designator=='t')
            total_trucks--;
        
        total_hours_all_autos-=number_of_hours;
        total_profit_all_autos-=cost_for_this_auto;

    Now my program is working correctly. I just wish I could have figured out how to do this in a smarter way...

  10. #10
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Do not use fflush(stdin), ever. It's wrong, as it invokes undefined behavior.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Well I'm not using it now. Any suggestions on how to get the while loop to work the way I want it to?

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Iteration of loop
    By funky in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 07:26 AM
  2. Replies: 2
    Last Post: 11-17-2007, 03:25 PM
  3. What the? Loop repeating to much?
    By Blackroot in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2006, 04:17 PM
  4. kernel iteration through for loop
    By sononix in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2004, 06:16 AM
  5. Loop iteration v's vectors
    By sononix in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2004, 10:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21