我使用switch 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");
}
它显示所有客户的第二个循环信息上的汽车类型,但计算工作正常:(希望你们可以帮助我。将非常感谢!!
答案 0 :(得分:0)
您需要将汽车类型信息保存在一个阵列中供所有客户使用。否则,所有客户的车型将与最后一位客户的车型相同。
您需要进行以下更改:
...
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
{
...