Thread: i need help with my loop in C

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Question i need help with my loop in C

    //The problem with my programm is when i try to come out of the loop by entering -1 it does not...Plz help me out guys i need help ....
    // Puttsardara dey (KAZDAX)





    #include <stdio.h>

    int main()
    {
    int avg, gal, drive, cnt ,mg;
    cnt = 1;

    clrscr();

    while ( gal != -1 ){
    printf("\nEnter the gallons used ( -1 to end ): ");
    scanf("%d", &gal);
    printf("\nEnter the miles driven: ");
    scanf("%d", &drive);
    mg = drive/gal;
    printf("\nThe miles / gallon for this tank was %d\n",mg);

    cnt++;
    }

    if ( gal == -1 ) {
    avg = mg/cnt;
    printf("\nThe overall average miles/gallon for this tank was %d\n",avg);
    }




    getch();
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    while ( gal != -1 ){
    printf("\nEnter the gallons used ( -1 to end ): ");
    scanf("%d", &gal);
    printf("\nEnter the miles driven: ");
    scanf("%d", &drive);
    mg = drive/gal;
    printf("\nThe miles / gallon for this tank was %d\n",mg);

    cnt++;
    }

    if ( gal == -1 ) {
    avg = mg/cnt;
    printf("\nThe overall average miles/gallon for this tank was %d\n",avg);
    }
    You should change the order here. Try this:

    Code:
    do
    { 
        printf("\nEnter the gallons used ( -1 to end ): "); 
        scanf("%d", &gal); 
    
        if( gal != -1 ) 
        {
            printf("\nEnter the miles driven: "); 
            scanf("%d", &drive); 
            mg = drive/gal; 
            printf("\nThe miles / gallon for this tank was %d\n",mg); 
            cnt++; 
        }
    }
    while ( gal != -1 );
    
    avg = mg/cnt; 
    printf("\nThe overall average miles/gallon for this tank was %d\n",avg);
    You may want to tweak your math a bit. (You may not need to, I'm tired and don't feel like compiling this or working it out in my head.) Anyway, that will fix your loop problem.

    You could replace the do/while with a simple 'while' but you'd want to make sure and initialize 'gal' first before using it.

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

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    Mmmm, I dont see any problem with ur code except that even after you enter -1, it will to calculate the miles/gallon and then exit the loop. The loop is getting exitted when the value of gal is -1...
    Help everyone you can

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Are you sure you want to use integer divison in calculating the avg?

    Coding a while loop like

    int c;
    while(c != -1) { }

    is very bad as there is a chance that
    the uninitialized variable c could equal -1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM