Thread: Not sure why part of a program isn't working

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    3

    Not sure why part of a program isn't working

    I'm working on a program that reads data from a text file, and then puts it into an array of structures. The data is about cars, and is in the format

    Make(string) Model(string) Year(int) Highway MPG(int) City MPG(int)

    We are supposed put this data into a structure with the addition of a calculated average MPG, and then use the average MPG to sort the cars in ascending order before then printing the sorted list to a new text file. I've gotten the program to successfully read the data and put it into a struct, and then also print that struct onto a new text file. I am however running into a few issues that have me kind of stumped. First off I cannot get the average MPG part of the struct to calculate anything. I am using the following loop to fill my struct and then calculate the average MPG, but when the program outputs the new text file the avg_mpg part is always just 0.00.

    Here is the struct
    Code:
    struct car{
    
    
     char make[MAX_CHARS + 1];
     char model[MAX_CHARS + 1];
     int year;
     int city_mpg;
     int highway_mpg;
     double avg_mpg;
    
    
    };


    And here is the loop :
    Code:
     //Writes data from cars.txt into struct and calculates highway_mpg
    
    
        while(fscanf(fp, "%s %s %d %d %d", cars[i].make, 
                cars[i].model, &cars[i].year, &cars[i].city_mpg,
                &cars[i].highway_mpg) != EOF && (i < MAX_CARS))
         {
          cars[i].avg_mpg = (cars[i].highway_mpg + cars[i].city_mpg) / 2;
           i++;
           while(fgetc(fp) != '\n') {};
          }
    I've been working on this program all day, so I'm sure it's something simple that I am just missing due to tunnel vision, but I'd appreciate any tips.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The problem is likely just that you're using integer division.
    Code:
    cars[i].avg_mpg = (cars[i].highway_mpg + cars[i].city_mpg) / 2.0;
    You probably don't need this part:
    Code:
    while(fgetc(fp) != '\n') {};
    Think about your format, the first part of the format string (%s) will skip any white space before the make string starts.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    3
    Quote Originally Posted by whiteflags View Post
    The problem is likely just that you're using integer division.
    Code:
    cars[i].avg_mpg = (cars[i].highway_mpg + cars[i].city_mpg) / 2.0;
    You probably don't need this part:
    Code:
    while(fgetc(fp) != '\n') {};
    Think about your format, the first part of the format string (%s) will skip any white space before the make string starts.
    You're right about the integer division being an issue that I didn't notice, but that doesn't seem to be the underlying problem. Even if I declare avg_mpg as an int and use:

    cars[i].avg_mpg = cars[i].highway_mpg + cars[i].city_mpg;

    as a test instead of the current calculation, it just prints 0 to the new text file where it should have the value of highway_mpg + city_mpg. Is the statement I'm using simply not something you can do with a struct?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Is the statement I'm using simply not something you can do with a struct?
    No, you can. I would have told you if it was something you couldn't do.

    Also I'm not really sure what to tell you. It works for me.

    Code:
    #include <stdio.h>
    #define MAX_CHARS 64
    struct car{
        char make[MAX_CHARS + 1];
        char model[MAX_CHARS + 1];
        int year;
        int city_mpg;
        int highway_mpg;
        double avg_mpg;
    };
    int main(void)
    {
        /* Make(string) Model(string) Year(int) Highway MPG(int) City MPG(int) */
        const char *fakefile[3] =
        {
            "Honda Accord 2016 23 34",
            "Mazda CX-5 2016 26 35",
            "Jeep.Grand.Cherokee Laredo 2016 18 25"
        };
        struct car cars[3];
        int i = 0;
        
        while (i < 3 && sscanf(fakefile[i], "%s %s %d %d %d", cars[i].make, cars[i].model,
                &cars[i].year, &cars[i].city_mpg, &cars[i].highway_mpg) == 5)
        {
            cars[i].avg_mpg = (cars[i].city_mpg + cars[i].highway_mpg) / 2.0;
            printf("%s %s average mpg=%f\n", cars[i].make, cars[i].model, cars[i].avg_mpg);
            ++i;
        }
            
        return 0;
    }
    
    Honda Accord average mpg=28.500000
    Mazda CX-5 average mpg=30.500000
    Jeep.Grand.Cherokee Laredo average mpg=21.500000
    Maybe there is something you missed?

  5. #5
    Registered User
    Join Date
    Jul 2016
    Posts
    3
    Ok it turns out it was my sorting function that was causing all the values to be 0.00. Thanks for the help, now at least I know I've just got to go back and figure out where my sorting function is messing up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-25-2016, 08:49 AM
  2. Help with part of program
    By mytrademark in forum C Programming
    Replies: 3
    Last Post: 10-13-2011, 03:24 PM
  3. Problem with part of program
    By ammochck21 in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2006, 06:45 AM
  4. Replies: 3
    Last Post: 12-17-2003, 06:02 PM
  5. How do you branch to another part of the program?
    By gcn_zelda in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 12:27 PM

Tags for this Thread