Question

Having trouble with my assignment. I have my program to where it will run, but I'm having issues getting it to work properly. I'm supposed to and define a structure type 'auto_t' and include components for the make, model, manufacture and purchase dates, fuel cap, fuel level, and odometer. Each divided into I/O functions (scan_auto, scan_date, scan_tank, print_auto, print_date, print_tank) and also write a driver function to repeatedly fill and display an auto structure variable until EOF is encounter in the input file. Example data set is: Mercury Sable 1 18 2001 5 30 1991 16 12.5 99892

Here's my code so far:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
char make[15];
char model[30];

int mmon;
int mday;
int myear;

int pmon;
int pday;
int pyear;

float fuelcap;
float fuellevel;

float odometer;
} auto_t;

/* Takes in make and model */

int scan_auto(auto_t *vehicle) {
return scanf("%s %s", vehicle->make, vehicle->model);
}

/* Takes in manufacture and purchase dates */

int scan_date(auto_t *date) {
return scanf("%d %d %d %d %d %d"
        , &date->mmon
        , &date->mday
        , &date->myear
        , &date->pmon
        , &date->pday
        , &date->pyear);            
}

/* Takes in fuel and odometer info */

int scan_tank(auto_t *tank) {
return scanf("%f %f %f"
        , &tank->fuelcap
        , &tank->fuellevel
        , &tank->odometer);

void print_auto(auto_t vehicle) {
printf("%s %s \n"
    , vehicle.make
    , vehicle.model);
}

void print_date(auto_t date) {
printf("%d %d %d %d %d %d \n"
    , date.mmon
    , date.mday
    , date.myear
    , date.pmon
    , date.pday
    , date.pyear);
}

void print_tank(auto_t tank) {
printf("%f %f %f \n"
    , tank.fuelcap
    , tank.fuellevel
    , tank.odometer);
}

/* Driver */

int main(void) {
auto_t vehicle, date, tank;

while(1) {
    printf("Please enter the make, then model of the car: ");
    scan_auto(&vehicle);
        if(scan_auto(&vehicle) == EOF){
            break;
        }
    printf("Please enter the manufacture and purchase dates (ex. 1 18 2001): ");        
    scan_date(&date);
        if(scan_date(&date) == EOF){
            break;
        }
    printf("Please enter the fuel cap, current fuel level (in gallons), and the current      odometer reading: ");
    scan_tank(&tank);
        if(scan_tank(&tank) == EOF){
            break;
        }
/* Prints all of the cars information */    
    print_auto(vehicle);
    print_date(date);
    print_tank(tank);

}
return 0;       
}

My issues are that when I run the program the data in not taken in properly by the scanf function and the scan_tank is being skipped. The print functions at the end also seem to be printing the wrong data (the make and model are not even shown) and I'm not sure if it's something wrong with the scan or print functions I'm using. Also I can't seem to get the programs loop to end and the program keeps going until I close it. Any advice would be greatly appreciated. And I'm coding in C and using TextWrangler.

Was it helpful?

Solution

EDITED:

There are other issues in the code:

  1. Change the scanf is not for performance, date->&pyear is totally wrong since pyear is a member of date, whereever if you wanted access pyear, use data->pyear . &date->pyear means &(date->pyear) because the priority -> is higher than &.
  2. The function scan_auto, scan_date and scan_tank are invoked twice, I know I didn't want that, so

//scan_auto(&vehicle); if(scan_auto(&vehicle) == EOF) The line in if statement will be executed as well, so comment the above one, so are other "scan" function. EDITED 2:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
char make[15];
char model[30];

int mmon;
int mday;
int myear;

int pmon;
int pday;
int pyear;

float fuelcap;
float fuellevel;

float odometer;
} auto_t;

int scan_auto(auto_t *vehicle) {
return scanf("%s %s", vehicle->make, vehicle->model);
}

int scan_date(auto_t *date) {
return scanf("%d %d %d %d %d %d"
        , &date->mmon
        , &date->mday
        , &date->myear
        , &date->pmon
        , &date->pday
        , &date->pyear);
}
int scan_tank(auto_t *tank) {
return scanf("%f %f %f"
        , &tank->fuelcap
        , &tank->fuellevel
        , &tank->odometer);
}
void print_auto(auto_t vehicle) {
printf("%s %s \n"
    , vehicle.make
    , vehicle.model);
}
void print_date(auto_t date) {
printf("%d %d %d %d %d %d \n"
    , date.mmon
    , date.mday
    , date.myear
    , date.pmon
    , date.pday
    , date.pyear);
}

void print_tank(auto_t tank) {
printf("%f %f %f \n"
    , tank.fuelcap
    , tank.fuellevel
    , tank.odometer);
}
int main()
{
auto_t vehicle;
auto_t date;
auto_t tank;
while(1) {
    printf("Please enter the make, then model of the car: \n");
    //scan_auto(&vehicle);
    if(scan_auto(&vehicle) == EOF){
        break;
    }
    printf("Please enter the manufacture and purchase dates (ex. 1 18 2001): \n");
    //scan_date(&date);
        if(scan_date(&date) == EOF){
            break;
        }
    printf("Please enter the fuel cap, current fuel level (in gallons), and the current      odometer reading: \n");
    //scan_tank(&tank);
        if(scan_tank(&tank) == EOF){
            break;
        }
    print_auto(vehicle);
    print_date(date);
    print_tank(tank);

    }
return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top