我的任务有问题。我的程序将运行到哪里,但是我遇到了让它正常工作的问题。我应该定义一个结构类型' auto_t'并包括制造,型号,制造和购买日期,燃料盖,燃料水平和里程表的组件。每个都分为I / O函数(scan_auto,scan_date,scan_tank,print_auto,print_date,print_tank),并且还编写驱动函数以重复填充和显示自动结构变量,直到在输入文件中遇到EOF。示例数据集是:Mercury Sable 1 18 2001 5 30 1991 16 12.5 99892
到目前为止,这是我的代码:
#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);
}
/ * 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;
}
我的问题是,当我运行程序时,scanf函数和scan_tank没有正确接收的数据被跳过。最后的打印功能似乎也打印了错误的数据(品牌和型号甚至没有显示),我不确定扫描或打印功能是否有问题。使用。此外,我似乎无法让程序循环结束,程序一直持续到关闭它为止。任何建议将不胜感激。我在C中编码并使用TextWrangler。
答案 0 :(得分:0)
编辑:
代码中还有其他问题:
scanf
不是为了表现,date->&pyear
是完全的
错误,因为pyear
是date
的成员,如果您想要访问pyear
,请使用data->pyear
。 &date->pyear
表示
&(date->pyear)
因为优先级->
高于&
。scan_auto
,scan_date
和scan_tank
两次,我知道我不想那样,所以 //scan_auto(&vehicle);
if(scan_auto(&vehicle) == EOF)
if
语句中的行也将被执行,因此请注释以上内容,其他&#34; scan&#34;功能。
编辑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;
}