algorithm - wrong detail is written on the file in c programming -


i'm using switch case , have loop , if/else in case. there no error detected,i still cant figure out why wont display correct type of car in file. frustrating bc due date tomorrow. coding @ case number 3. aim write information on customer_info file. here's coding , 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");        {     printf("\t\t     ----..main menu..----\n\n\t1 - insert customer information \n\t2 - select car & hour \n\t3 - read customer , rent information\n");     printf("\nchoose menu selection\n");    scanf("%d", &selection);     switch(selection)    {      case 1: printf("----please fill in 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 car\n");                 fflush(stdin);                 scanf("%c", &car);                 printf("enter rent hour : ");                scanf("%d", &renthour[i]);                 if(car=='a')                {                    total_price[i] = total_price_a(renthour[i]);                    printf("\ntotal price rent: rm%.2f", total_price[i]);                    printf("\n\n");                 }                else if(car=='b')                {                    total_price[i] = total_price_b(renthour[i]);                    printf("\ntotal price rent: rm%.2f", total_price[i]);                    printf("\n\n");                 }                else if(car=='c')                {                    total_price[i] = total_price_c(renthour[i]);                    printf("\ntotal price rent: rm%.2f", total_price[i]);                    printf("\n\n");                }                else                {                    printf("\nthere's no such thing in our system. kindly please try letter\n");                    printf("\n\n");                }              }                break;        case 3:  printf("\n---receipt , 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 continue? (yes-1/no-0): ");     scanf("%d", &answer);    }while(answer == 1);     printf("\nexit system..... thank using system\n\n\n"); } 

enter image description here

it showed type of car on second loop info customers calculation works fine :( hope guys can me thi. appreciated!!

you need keep car type information in array customers. otherwise car type of customer same car type of last customer.

you need make these changes:

...  printf("number of customers: "); scanf("%d", &noofcustomer);  ...  char car[noofcustomer];//holds car type each customer   ...  case 2:  printf("\n----select car & hour----\n");          display_cars();          for(i=0;i<noofcustomer;i++)          {             printf("choose car\n");             fflush(stdin);             scanf("%c", &car[i]);//save car type each customer              ...              if(car[i]=='a')//use index             {             ...  case 3:  printf("\n---receipt , read customer information----\n");           file *myfile;          myfile = fopen("customer_info.txt","w");           for(i=0;i<noofcustomer;i++)          {            if(car[i]=='a')//use populated array of car types each customer            {               ... 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -