( Note : It is a C program ). ( Note : It is a C program ). 1- Car information 2
ID: 3708549 • Letter: #
Question
( Note : It is a C program ).
( Note : It is a C program ). 1- Car information 2- Bills 3- Display bills 4- Exit Please choose from the menu 1- Car information The first choice will enable the user to insert the car information (ID of the car owner, car number, mobile number of the car owner). For car number, make the user inserts three characters followed by the numerical number (e.g.. X Y X 1234 will be stored in three characters and one integer number). This in formation will be saved in cars.txt file. Sample output: Insert customer ID: 6584 Insert car numberXY X 1234 Insert customer mobile number 05123456 Do you want to insert another car (Y/N)?1N
Explanation / Answer
#include<stdio.h>
#include<process.h>
#include<string.h>
int main()
{
FILE *fptr,*fptr1;
char ch1;
int choice;
int id,custID,month,year,consumption;
double bill;
char carNo[12];
char mobileNo[10];
while(1)
{
printf("1- Car information 2- Bills 3- Display bills 4- Exit ---------- ");
printf("Please choose from the menu: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
fptr=fopen("cars.txt","w");
if(fptr==NULL)
{
printf("Could not find the file!!");
exit(0);
}
printf("Insert customer ID: ");
scanf("%d",&id);
printf("Insert car number: ");
scanf("%s",carNo);
printf("Insert customer mobile number: ");
scanf("%s",mobileNo);
fprintf(fptr,"%d ",id);
fprintf(fptr,"%s ",carNo);
fprintf(fptr,"%s ",mobileNo);
fclose(fptr);
break;
case 2:
fptr1=fopen("Bills.txt","w");
printf("Insert customer ID or 0 to go back. ");
scanf("%d",&custID);
if(custID==id)
{
printf("Customer ID: %d ",custID);
}
else
{
printf("Customer is not exist");
}
printf("Month : ");
scanf("%d",&month);
printf("Year : ");
scanf("%d",&year);
printf("Consumption : ");
scanf("%d",&consumption);
if(consumption<10)
{
bill=consumption*10;
}
else if(consumption>=10 && consumption<=20)
{
bill=consumption*8;
}
else if(consumption>=21 && consumption<=55)
{
bill=consumption*6.5;
}
else if(consumption>=56 && consumption<=160)
{
bill=consumption*4;
}
else if(consumption>160)
{
bill=consumption*2;
}
printf("The oil bill is : %f ",bill);
fprintf(fptr,"%d %d %d %d %f",custID,month,year,consumption,bill);
fclose(fptr);
break;
case 3:
while(fscanf(fptr,"%d %d %d %d %f",&custID,&month,&year,&consumption,&bill) == 1)
{
printf("%d %d %d %d %f",custID,month,year,consumption,bill);
}
fclose(fptr1);
break;
case 4:exit(0);
break;
}
printf("Do you want to insert another car(y/n)");
scanf("%c",&ch1);
if(ch1=='n')
{
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.