Thread: First "While Loop", sentinel won't work.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    70

    First "While Loop", sentinel won't work.

    Ok, so the program is supposed to take in an initial mile odometer reading. Subtract that number from a new odometer reading at time of refueling and divide that difference by the amount of gas needed to fill the car up. Resulting in MPG for that segment of trip.

    Here is what I got so far:
    Code:
    #include <stdio.h>
        #define CUTOFF -1
    
    int main()
    {
    float gas;
    int sMileage, fMileage, tMileage, mpg;
    
    printf("\nThis program will calculate your Miles Per Gallon(MPG) between two distances.");
    printf("\nTo stop entering data, type a negative number for input");
    printf("\nor simotaneusly press ctrl+Z(Windows) or ctrl+D(Unix) to stop.");
    
    while (sMileage > CUTOFF || fMileage > CUTOFF || gas > CUTOFF){
    
    printf("\nEnter starting trip mileage");
    scanf("%d", &sMileage);
    printf("\nEnter end of trip mileage");
    scanf("%d", &fMileage);
    printf("\nEnter amount of gas needed to refeul vehicle");
    scanf("%.2f", &gas);
    
    tMileage = (fMileage - sMileage);
    }
    
    mpg = gas / tMileage;
    printf("\nyour MPG for trip distance entered was %.2f, mpg");
    
    return 0;
    }
    So right now this is what I am getting after running with no compiler issues...

    [cameron@localhost Program5]$ ./Exercise10

    This program will calculate your Miles Per Gallon(MPG) between two distances.
    To stop entering data, type a negative number for input
    or simotaneusly press ctrl+Z(Windows) or ctrl+D(Unix) to stop.
    Enter starting trip mileage5

    Enter end of trip mileage10

    Enter amount of gas needed to refeul vehicle2

    Enter starting trip mileage
    Enter end of trip mileage5

    Enter amount of gas needed to refeul vehicle10

    Enter starting trip mileage
    Enter end of trip mileage2

    Enter amount of gas needed to refeul vehicle-1

    Enter starting trip mileage
    Enter end of trip mileage-1

    Enter amount of gas needed to refeul vehicle-1

    Enter starting trip mileage
    After the first run of the loop it completely skips the "scanf" to enter the starting tirp mileage and goes straight to end of trip mileage, also the sentinel doesn't work and it just keeps cycling.

    I was kind of shooting to give mpg results and then go back to entering in the initial trip mileage where the user could deside to terminate.

    Also ctrl+d doesn't work for me on Fedora.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to increase your compiler warning level. Here are the warnings I got when I compiled your code.

    /main.c||In function ‘main’:|
    /main.c|20|warning: unknown conversion type character ‘.’ in format [-Wformat]|
    /main.c|20|warning: too many arguments for format [-Wformat-extra-args]|
    /main.c|26|warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat]|
    /main.c|7|warning: variable ‘mpg’ set but not used [-Wunused-but-set-variable]|
    ||=== Build finished: 0 errors, 4 warnings ===|
    Also find and use an indentation style.
    Jim

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    additionally - your line 13 uses uninitialized variables - so you enter your loop for the first time by pure luck.

    you also should check you scanf return values to see if anything was read at all.

    you make your mpg calculations outside the loop - when all vars should be negative - so it makes no sense
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Change scanf("%.2f", &gas); to scanf("%f", &gas);

    Also that while statement is being run before you initialize any of the variables used in that while statement. You should initialize all of them to zero.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ok, I made some changes based on what I could get out of your suggestions.

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
        #define CUTOFF -1
    
    int main()
    {
    float gas;
    int sMileage, fMileage, tMileage, mpg;
    
    printf("\nThis program will calculate your Miles Per Gallon(MPG) between two distances.");
    printf("\nTo stop entering data, type a negative number for input");
    printf("\nor simotaneusly press ctrl+Z(Windows) or ctrl+D(Unix) to stop.");
    
    sMileage = 0;
    fMileage = 0;
    gas = 0;
    while (sMileage > CUTOFF){
    
        printf("\nEnter starting trip mileage");
            scanf("%d", &sMileage);
        printf("\nEnter end of trip mileage");
            scanf("%d", &fMileage);
        printf("\nEnter amount of gas needed to refeul vehicle");
            scanf("%f", &gas);
    
        tMileage = (fMileage - sMileage);
    
        mpg = gas / tMileage;
        printf("\nYour MPG for trip distance entered was %f", mpg);
    }
    
    
    return 0;
    }
    This is what I am getting now. It goes back to the first after computing average, but the average doesn't come out correctly for some reason. The sentinel still doesn't work properly though. It should only have to work with the first number I think so I removed the "||" 's for now.

    [cameron@localhost Program5]$ ./Exercise10

    This program will calculate your Miles Per Gallon(MPG) between two distances.
    To stop entering data, type a negative number for input
    or simotaneusly press ctrl+Z(Windows) or ctrl+D(Unix) to stop.
    Enter starting trip mileage5

    Enter end of trip mileage10

    Enter amount of gas needed to refeul vehicle2

    Your MPG for trip distance entered was 0.000000
    Enter starting trip mileage-1

    Enter end of trip mileage-1

    Enter amount of gas needed to refeul vehicle-1

    Your MPG for trip distance entered was 0.000000[cameron@localhost Program5]$
    EDIT: Also do you know where I can find information on making my compiler give out more advanced warnings? I am new to linux and GCC so that would be awesome!

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Your mph variable is an int not a floating point number, so 2/10 would yield zero.

    I also recommend you try a do/while loop instead of the while loop, and get the sMileage value before the loop, then again right before the end of the loop.

    Jim
    Last edited by jimblumberg; 06-14-2013 at 09:04 PM.

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by jimblumberg View Post
    Your mph variable is an int not a floating point number, so 2/10 would yield zero.

    I also recommend you try a do/while loop instead of the while loop, and get the sMileage value before the loop, then again right before the end of the loop.

    Jim
    Unfourtunetly this assignment comes before we learn about "do while" loops. Since this is an online college course I am not for certain if we are allowed to go ahead in the book to do past program exercises. He doesn't mention anywhere in the syllabus that we would be allowed to do so, so i am guessing we are not.

    I keep looking at past programs and I can't see why my program isn't terminating when the sentinel is entered on the initial mileage line. Should I construct an "if" statement inside the while loop?

    EDIT: Making MPH an INT value made the program work for computing the MPH with the given values, but like above I am still having trouble with the sentinel working correctly. ctrl+d and a negative number both work, but it doesn't end as soon as the sentinel is triggered....

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Because your "sentinel" is not checked until after you do all the calculations, at the beginning of the loop. If you don't want to use a do/while loop then you need to find some way of skipping the calculations if you entered the sentinel value.

    Jim

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by jimblumberg View Post
    Because your "sentinel" is not checked until after you do all the calculations, at the beginning of the loop. If you don't want to use a do/while loop then you need to find some way of skipping the calculations if you entered the sentinel value.

    Jim
    ugh... I found another way. If I did:

    Code:
     if (sMileage > 0)
                else
                break;
    But my instructor has this in the sylabus:

    8) Is the program written WITHOUT break statements (except when used in a switch statement)? (Programs submitted with a break statement will receive an automatic zero with the exception that breaks can be used with a switch);
    Running out of ideas other than to just tell the user to enter in 3 negative numbers to make it work. Every example program in this chapter on while loops only uses 1 "scanf" function and gives no detail on what to do for multiple input functions using a sentinel. =(

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can do something like this
    Code:
    do
    {
       scanf var1;
       if(var1 > -1)
       {
           do the rest of input and calculations
        }
    } while(var1 > -1);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ok, I got it to work almost completely. DON'T GIVE UP ON ME YET! :P

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
        #define CUTOFF 0
    
    int main()
    {
    float gas, mpg;
    int sMileage, fMileage, tMileage;
    
    printf("\nThis program will calculate your Miles Per Gallon(MPG) between two distances.");
    printf("\nTo stop entering data, type a negative number for input");
    printf("\nor simotaneusly press ctrl+Z(Windows) or ctrl+D(Unix) to stop.");
    printf("\nEnter starting trip mileage");
    
    sMileage = 0;
    fMileage = 0;
    gas = 0;
    while (scanf("%d", &sMileage) != EOF || sMileage < CUTOFF){
        printf("\nEnter end of trip mileage");
            scanf("%d", &fMileage);
        printf("\nEnter amount of gas needed to refeul vehicle");
            scanf("%f", &gas);
    
        tMileage = (fMileage - sMileage);
    
        mpg = tMileage / gas;
        printf("\nYour MPG for trip distance entered was %f", mpg);
        
        printf("\nEnter starting trip mileage");
    }
    
    
    return 0;
    }
    So I figured out that in order to make it terminate on the first input(sMileage) I had to start it outside the loop and make the "scanf" part of the sentinel. The only problem is that only ctrl+d works to stop the program. Negative numbers don't work at all, so I think I am just going to use the EOF as the break until I can get to "Do While" loops.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    looks like you while condition is wrong... should be

    Code:
    scanf("%d", &sMileage) != EOF && sMileage > CUTOFF
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by vart View Post
    looks like you while condition is wrong... should be

    Code:
    scanf("%d", &sMileage) != EOF && sMileage > CUTOFF
    haha thanks, but I just sent it in for grading. Was due at midnight. Defiently will keep that in mind.

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Cameron Taylor View Post
    EDIT: Also do you know where I can find information on making my compiler give out more advanced warnings? I am new to linux and GCC so that would be awesome!
    In case you haven't found out yet, gcc uses command line switches to turn on warnings. I usually use -Wall and -Wextra but there are lot more. See Warning Options - Using the GNU Compiler Collection (GCC).
    I also suggest compiling with debugging symbols (-g) and learn to use a debugger as early as possible.
    So your command should look like:
    Code:
    gcc -g -Wall -Wextra -o foo foo.c
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-18-2012, 08:43 AM
  2. nbin=fopen("input.txt","a"); doesn't work?
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 06-23-2011, 02:57 PM
  3. Need "if","for loop",&"else" source codes
    By dn_angel_07 in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2009, 10:01 PM
  4. Working with "Sentinel-Exit (-1)".. :{
    By MAV_DevWantB in forum C Programming
    Replies: 5
    Last Post: 09-10-2009, 02:01 PM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM