我的作业有麻烦。我的程序可以在其运行的地方运行,但是我在使程序正常运行方面遇到问题。我应该定义一个结构类型'auto_t',并包括制造,型号,制造和购买日期,加油站盖,油位和里程表的组件。每个函数都分为I / O函数(scan_auto,scan_date,scan_tank,print_auto,print_date,print_tank),并且还编写了一个驱动程序函数来重复填充和显示auto结构变量,直到在输入文件中遇到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);
}
/ *驱动程序* /
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。
最佳答案
编辑:
代码中还有其他问题:
更改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
语句中的行也将执行,因此注释上面的一行,其他“扫描”功能也是如此。
编辑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;
}
关于c - 高级C分配中的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23072508/