2016-05-28 3 views
-1

Ich benutze Switch Case und habe Schleife und wenn/sonst in dem Fall. Selbst wenn kein Fehler entdeckt wird, kann ich immer noch nicht herausfinden, warum es nicht den richtigen Autotyp in der Datei anzeigen würde. Es ist frustrierend, dass mein Fälligkeitstermin morgen ist. Die Codierung erfolgt im Fall Nummer 3. Es ist das Ziel, die Informationen in die Datei customer_info zu schreiben. hier ist meine Codierung und die Ausgabe:falsches Detail ist auf der Datei in c Programmierung geschrieben

Codierung:

#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"); 
} 

enter image description here

Es zeigte die Art des Autos auf der zweiten Schleife Info für alle Kunden, sondern die Berechnung funktioniert gut :(hoffe, euch kann mir helfen mit thi. wird sehr geschätzt !!

Antwort

0

Sie müssen die Fahrzeugtyp Informationen in einem Array für alle Kunden zu halten.Andernfalls der Fahrzeugtyp für alle Ihre Kunden wird der gleiche wie der Fahrzeugtyp der sein letzter Kunde

Sie müssen diese Änderungen vorzunehmen:

... 

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 
      { 
       ... 
+1

vielen Dank für Ihre Hilfe ist es wirklich hilfreich !!! :) – husnatasnim