I'm using switch case and have loop and if/else in the case. Even there is no error detected,i still cant figure out why would it wont display the correct type of car in the file. It is frustrating bc my due date is tomorrow. The coding is at case number 3. It is aim to write the information on customer_info file. here's my coding and the output:
coding :
#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");
}
It showed the type of car on the second loop info for all customers but the calculation works just fine :( hope you guys can help me with thi. will be much appreciated!!
You need to keep the car type information in an array for all customers. Otherwise the car type for all of your customer will be the same as the car type of the last customer.
You need to make these changes:
...
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
{
...