gpt4 book ai didi

c - 高级 C 赋值错误

转载 作者:行者123 更新时间:2023-11-30 21:30:54 25 4
gpt4 key购买 nike

我的作业遇到问题。我的程序已经运行到了可以运行的位置,但我在使其正常工作时遇到了问题。我应该定义一个结构类型“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。

最佳答案

编辑:

代码中还有其他问题:

  1. 更改 scanf 并不是为了性能,date->&pyear 完全是为了性能错误,因为 pyeardate 的成员,如果您想访问 pyear,请使用 data->pyear&date->pyear 表示&(date->pyear) 因为优先级 -> 高于 &
  2. 调用函数 scan_autoscan_datescan_tank两次,我知道我不想那样,所以

//scan_auto(&车辆);
if(scan_auto(&车辆) == EOF)
if 语句中的行也会被执行,所以注释掉上面的代码,其他“scan”函数也是如此。编辑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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com