gpt4 book ai didi

c - C 编程中文件中写入了错误的详细信息

转载 作者:行者123 更新时间:2023-11-30 19:38:48 26 4
gpt4 key购买 nike

我正在使用 switch case,并且在 case 中有循环和 if/else。即使没有检测到错误,我仍然无法弄清楚为什么它不会在文件中显示正确的汽车类型。令人沮丧的是我的预产期是明天。编码位于案例编号 3 处。其目的是将信息写入 customer_info 文件。这是我的编码和输出:

编码:

    #include<stdio.h>

void display_cars();
float total_price_A(int);
float total_price_B(int);
float total_price_C(int);
void readcustomerrent_info();

main()
{

int i,selection,answer;
int noofcustomer;
char car;

printf("Number of customers: ");
scanf("%d", &noofcustomer);

char fullname[noofcustomer][50], ic[noofcustomer][50], phonenum[noofcustomer][50], license[noofcustomer][50];
int renthour[noofcustomer];
float total_price[noofcustomer];

printf("\t==================================================");
printf("\n\t\t CAR RENTAL SYSTEM\n");
printf("\t==================================================\n\n");

do
{

printf("\t\t ----..Main Menu..----\n\n\t1 - Insert Customer Information \n\t2 - Select Car & Hour \n\t3 - Read Customer and Rent Information\n");

printf("\nChoose your menu selection\n");
scanf("%d", &selection);

switch(selection)
{
case 1: printf("----Please fill in the information below----\n\n");



for(i=0;i<noofcustomer;i++)
{
printf("Your Name : ");
fflush(stdin);
gets(fullname[i]);


printf("Your IC : ");
fflush(stdin);
gets(ic[i]);


printf("Your Telephone Number : ");
fflush(stdin);
gets(phonenum[i]);


printf("Your License Number : ");
fflush(stdin);
gets(license[i]);

printf("\n\n");


}

break;


case 2: printf("\n----Select Car & Hour----\n");

display_cars();
for(i=0;i<noofcustomer;i++)
{
printf("Choose the car\n");
fflush(stdin);
scanf("%c", &car);

printf("Enter the rent hour : ");
scanf("%d", &renthour[i]);

if(car=='A')
{
total_price[i] = total_price_A(renthour[i]);
printf("\nTotal Price For The Rent: RM%.2f", total_price[i]);
printf("\n\n");

}
else if(car=='B')
{
total_price[i] = total_price_B(renthour[i]);
printf("\nTotal Price For The Rent: RM%.2f", total_price[i]);
printf("\n\n");

}
else if(car=='C')
{
total_price[i] = total_price_C(renthour[i]);
printf("\nTotal Price For The Rent: RM%.2f", total_price[i]);
printf("\n\n");
}
else
{
printf("\nThere's no such thing in our system. Kindly please try another letter\n");
printf("\n\n");
}
}


break;



case 3: printf("\n---Receipt and Read Customer Information----\n");

FILE *myfile;
myfile = fopen("customer_info.txt","w");

for(i=0;i<noofcustomer;i++)
{
fprintf(myfile,"Name : %s",fullname[i]);
fprintf(myfile,"\nIC : %s",ic[i]);
fprintf(myfile,"\nTelephone Number : %s",phonenum[i]);
fprintf(myfile,"\nLicense Number : %s",license[i]);

if(car=='A')
{
fprintf(myfile,"\nCar : Audi");
fprintf(myfile,"\nTotal Price : %.2f",total_price[i]);
fprintf(myfile,"\n\n");
}
else
if(car=='B')
{
fprintf(myfile,"\nCar : Corolla");
fprintf(myfile,"\nTotal Price : %.2f",total_price[i]);
fprintf(myfile,"\n\n");
}
else
if(car=='C')
{
fprintf(myfile,"\nCar : Axia");
fprintf(myfile,"\nTotal Price : %.2f",total_price[i]);
fprintf(myfile,"\n\n");
}
else
{
fprintf(myfile,"\nCar : -");
fprintf(myfile,"\nTotal Price : -");
fprintf(myfile,"\n\n");
}
}

fclose(myfile);

//readcustomerrent_info();

break;


default: printf("Your entered invalid selection.");

}

printf("\n\nDo you like to continue? (Yes-1/No-0): ");
scanf("%d", &answer);

}while(answer == 1);

printf("\nExit the system..... THANK YOU FOR USING THE SYSTEM\n\n\n");
}

enter image description here

它在第二个循环信息中为所有客户显示了汽车类型,但计算效果很好:(希望你们能帮助我。将不胜感激!!

最佳答案

您需要将所有客户的汽车类型信息保存在一个数组中。否则,您所有客户的汽车类型将与最后一个客户的汽车类型相同。

您需要进行以下更改:

...

printf("Number of customers: ");
scanf("%d", &noofcustomer);

...

char car[noofcustomer];//holds the car type for each customer

...

case 2: printf("\n----Select Car & Hour----\n");
display_cars();
for(i=0;i<noofcustomer;i++)
{
printf("Choose the car\n");
fflush(stdin);
scanf("%c", &car[i]);//save the car type for each customer

...

if(car[i]=='A')//use an index
{
...

case 3: printf("\n---Receipt and Read Customer Information----\n");

FILE *myfile;
myfile = fopen("customer_info.txt","w");

for(i=0;i<noofcustomer;i++)
{
if(car[i]=='A')//use the previously populated array of car types for each customer
{
...

关于c - C 编程中文件中写入了错误的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37500001/

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