EDITED:
There are other issues in the code:
- Change the
scanf
is not for performance,date->&pyear
is totally wrong sincepyear
is a member ofdate
, whereever if you wanted accesspyear
, usedata->pyear
.&date->pyear
means&(date->pyear)
because the priority->
is higher than&
. - The function
scan_auto
,scan_date
andscan_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;
}