0

[The code is working now, Thanks for the help.]
I can get the program to print the first set of struct auto_t. When I try to print the other sets nothing happens or I get an error.

This is what I have to do.
Define a structure type auto_t to represent an automobile. Include components for the make and model (strings), the odometer reading, the manufacture and purchase dates (use another user-defined type called date_t ), and the gas tank (use a user-defined type tank_t with components for tank capacity and current fuel level, giving both in gallons). Write I/O functions scan_date , scan_tank , scan_auto , print_date , print_tank , and print_auto , and also write a driver function that repeatedly fills and displays an auto structure variable until EOF is encountered in the input file.

Here is a small data set to try :

Mercury Sable 99842 1 18 2001 5 30 1991 16 12.5
Mazda Navajo 123961 2 20 1993 6 15 1993 19.3 16.7

Here is the code that works:
[If you see any thing wrong with the code that I missed I don't mind the feed back]

#include <stdio.h>
#define SIZE 20
// the structures
typedef struct       //struct for date
{
        int month,day,year;
} date_t;

typedef struct      //struct for the tank info
{
        double capacity;
        double curent_Fuel;
} tank_t;

typedef struct      //the struct for the automobie
{
        char make[SIZE];
        char model[SIZE];
        int odometer;
        date_t manufact;
        date_t purchase;
        tank_t tank;
} auto_t;

//the function 
void print_date(date_t da);
void print_tank(tank_t ta);
void print_auto(auto_t au);
int scan_date(date_t *date);
int scan_tank(tank_t *tank);
int scan_automobile(auto_t *automo);

    //Start of program
    int main (void)
{
            auto_t car;
            int stat = 1;

            FILE *Car_data;                   //file used
            Car_data = fopen("car.txt", "r");// has the date for the cars like make, model ect.
            if (Car_data==NULL){
               printf("ERROR: File failed to open");
               getch();
               exit(1);
               fclose(Car_data);}
            else
                while(stat>0)
           {                
                stat=fscanf(Car_data, "%s %s %d %d %d %d %d %d %d %lf %lf", &car.make,
                                                                            &car.model,
                                                                            &car.odometer,
                                                                            &car.manufact.month,
                                                                            &car.manufact.day,
                                                                            &car.manufact.year,
                                                                            &car.purchase.month,
                                                                            &car.purchase.day,
                                                                            &car.purchase.year,
                                                                            &car.tank.capacity,
                                                                            &car.tank.curent_Fuel);

                if (stat==11)
                {
                    print_auto(car);
                    printf("Maufactured date:");
                    print_date(car.manufact);
                    printf("\nPurchased date:");
                    print_date(car.purchase);
                    printf("\nTank capacity and current fuel");
                    print_tank(car.tank);
                }
           }    

        getch();  // Just used to keep the data on the sreen for testing pupose
        return(0);
}


    int scan_date(date_t *date)
    {
        int res;
        res=scanf("%d %d %d", &(*date).month, &(*date).day, &(*date).year);
        if(res==3)
         res=1;
        else if(res !=EOF)
         res=0;
        return(res);
    }

    int scan_tank(tank_t *tank)
    {
        int res;
        res=scanf("%lf %lf", &(*tank).capacity, &(*tank).curent_Fuel);
        if(res==2)
         res=1;
        else if(res !=EOF)
         res=0;
        return(res);
    }

    int scan_automobile(auto_t *automo)
    {
        int res;
        res=scanf("%s %s %d %d %d %d %d %d %d %lf %lf", &(*automo).make,
                                                        &(*automo).model,
                                                        &(*automo).odometer,
                                                        &(*automo).manufact.month,
                                                        &(*automo).manufact.day,
                                                        &(*automo).manufact.year,
                                                        &(*automo).purchase.month,
                                                        &(*automo).purchase.day,
                                                        &(*automo).purchase.year,
                                                        &(*automo).tank.capacity,
                                                        &(*automo).tank.curent_Fuel);
        if(res==11)
         res=1;
        else if(res !=EOF)
         res=0;
        return(res);
    }

    void print_date(date_t da)
    {
        printf("\n%d-%d-%d", da.month, da.day, da.year);
    }

    void print_tank(tank_t ta)
    {
        printf("\n%2.2lf %2.2lf\n", ta.capacity, ta.curent_Fuel);
    }

    void print_auto(auto_t au)
    {
        printf("\nVehicle \n%s %s %d %d %d %d %d %d %d %lf %lf\n", au.make,
                                                     au.model,
                                                     au.odometer,
                                                     au.manufact.month,
                                                     au.manufact.day,
                                                     au.manufact.year,
                                                     au.purchase.month,
                                                     au.purchase.day,
                                                     au.purchase.year,
                                                     au.tank.capacity,
                                                     au.tank.curent_Fuel);
    }
mebrandon
  • 13
  • 6

2 Answers2

0

There are a couple of things I don't understand in your code which might get you in trouble :

    if (Car_data==NULL){
       printf("ERROR: File failed to open");
       getch();
       exit(0);
       fclose(Car_data);}

Why are you using exit(0) when dealing with an error ? May be check this.

if(stat==11)
print_auto(car);
// rest of the code

Aside from indentation, I am not sure you get that here, if (stat == 11), only print_auto(car) will be executed. You need to do this if you want conditions and loops to embrace more that one line of code :

if(condition)
{
   // code
}
else
{
  // code
}
loop()
{
  // code
}

I think there is a high pourcentage of chance that your errors come from this.

Community
  • 1
  • 1
Badda
  • 1,329
  • 2
  • 15
  • 40
  • Thanks, you are right about `if(stat==11)` i just got ride of it from my code and I think I got it working, still have to add some things and test it a bit more. – mebrandon Apr 28 '17 at 09:08
  • Glad I could help. If testing goes right, don't forget to [accept](http://stackoverflow.com/help/someone-answers) an answer so that the topic can be closed. – Badda Apr 28 '17 at 09:24
  • It works well enough for me but, for some reason it is printing the last vehicle info twice.it is not exiting the `while` after it prints mazda data. – mebrandon Apr 28 '17 at 09:29
  • Can you edit your post so I can see what your current code is ? – Badda Apr 28 '17 at 09:34
  • Just did. The code i changed is in the `else while(stat>0)` of the `main`. – mebrandon Apr 28 '17 at 09:48
  • I don't get when stat is supposed to be equal to 0 in order to exit the loop. fscanf is supposed to be equal to 11 if everything goes right, isn't it ? – Badda Apr 28 '17 at 09:59
  • It is supposed to equal 0 when there is nothing left in the file to read, `EOF`. Yes if everything goes right stat is supposed to equal 0. I think I got it, I need to put an `if (stat == 11) {..}` with the print stuff inside th `{}` after the `if`. – mebrandon Apr 28 '17 at 10:13
  • Oh yeah I see. Yes I think what you have just said should work just fine. – Badda Apr 28 '17 at 10:16
  • Thanks, yeah with the small bit of testing I did it work – mebrandon Apr 28 '17 at 10:21
0

This is not really an answer but the way you format your code is very error prone:

For example this piece of code:

res=scanf("%lf %lf", &(*tank).capacity, &(*tank).curent_Fuel);
if(res==2)
res=1;
else if(res !=EOF)
res=0;
return(res);

should rather be formatted like this:

res=scanf("%lf %lf", &(*tank).capacity, &(*tank).curent_Fuel);
if(res==2)
  res=1;
else if(res !=EOF)
  res=0;
return(res);
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Thanks, most of the time I try to format it like that but, because I using different versions of the Dev c++. I know dev c++ is not the best but it is what 2 of my programming teachers used for homework, so I use it to make sure the code works for them. I usually use Visual Studio. – mebrandon Apr 28 '17 at 09:41