错误的细节写在c编程的文件上
wrong detail is written on the file in c programming
我正在使用 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");
}
它为所有客户显示了第二个循环信息中的汽车类型,但计算工作正常:(希望你们能帮助我。将不胜感激!
您需要将所有客户的汽车类型信息保存在一个数组中。否则,您所有客户的车型将与最后一位客户的车型相同。
您需要进行以下更改:
...
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
{
...
我正在使用 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");
}
它为所有客户显示了第二个循环信息中的汽车类型,但计算工作正常:(希望你们能帮助我。将不胜感激!
您需要将所有客户的汽车类型信息保存在一个数组中。否则,您所有客户的车型将与最后一位客户的车型相同。
您需要进行以下更改:
...
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
{
...