Thread: Help with this c program

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    1

    Help with this c program

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
     FILE *outfile;
     
     char ch, name;
     float fee, discfee;
     
     outfile = fopen("flightinfo.txt","r");
     
     if (outfile = NULL)
     {
       printf("Record not found!\n");
       exit(1);
     }
     
     printf("Enter the name of passenger for ticket fee discount: ");
     scanf("%s", &name);
     
     
     if (fee >= 15000.00)
       discfee = fee * 0.80;
     else if (fee >= 10000.00)
       discfee = fee * 0.85;
     else if (fee >= 5000.00)
       discfee = fee * 0.90;
     else
       discfee = fee * 0.95;
     
     printf("The ticket fee after discount is $%8.2f\n", discfee);
     
     return 0;
    }
    BELOW is extract from the txt file:

    Louis
    CJ777
    4578.00
    New York
    London
    2016 11 1
    Economy
    1
    Tom
    DL992
    3824.00
    New York
    Tokyo
    2016 12 1
    Economy
    1



    I want to calculate the ticket fee after discount from original fee in the txt file, but I have trouble with the codes up there. Please tell me how to do it. Thanks.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Two quick suggestions.

    1.) Re-read your text book about the different types of variables. What is the difference between a single character and a string?
    2.) Find and study some documentation for scanf(), make sure you always use the correct format specifier for the type, and remember when retrieving a string make sure you use the proper width specifier. By the way the "%s" specifier requires an array of char, not a single character.


    Jim

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Also, "fee" is being used before it is given a valid value.

    Code:
    if (outfile = NULL)
    Note the difference between = and ==

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-15-2016, 03:29 PM
  2. Replies: 7
    Last Post: 03-15-2016, 06:35 AM
  3. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  4. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM

Tags for this Thread