Thread: Reading from a file and skipping lines to find string in file

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    Reading from a file and skipping lines to find string in file

    Program background:

    Ticket Sales Details

    You will sell tickets in advance and at the door. Prices for buying in advance and at the door will be given. Also, the total number of tickets sold in advance will be given. Each guest will have a unique number. If there are n tickets sold in advance, then these guests will be numbered 0 through n-1. As the event starts, requests to buy tickets at the door may be made and these guests will be numbered sequentially, starting at the lowest unassigned number. The maximum number of guests will be 1000.

    The first line of the file contains the following three values, separated by spaces:
    Cost of the presales tickets (in dollars), Cost of the tickets at the door (in dollars), and the number of presale tickets. The first two values will be positive real numbers to two decimal places and the last will be a positive integer.

    The second line of the file will contain one positive integer representing the number of auction items followed by a positive real value to two decimal places (at most) representing the minimum bid increment, in dollars. The first value is guaranteed to be 1000 or less and the second will be in between 1 and 50, inclusive.

    The third line of the file will contain all the prices of the auction items, in dollars, separated by spaces, in order. Thus, the first price is the price of item 0, the next price is the price of item 1, and so on. These values will be real numbers represented to up to 2 decimal places.

    The fourth line of the file will contain the three following positive integers pertaining to the raffle: the number of raffle tickets available, the cost of a raffle ticket in dollars, and the number of raffle prizes. (It's strange to have raffle tickets that don't cost a whole number of dollars.)

    The fifth line of the file will contain each of the values of the raffle items, in dollars, separated by spaces, in order. Thus, the first price is the price if item 0, the next price is the price of item 1, and so on. These values will be real numbers with upto 2 decimal places.

    The sixth line of the file will contain ten positive integers representing the number of each of the drinks 0 through 9, in order, that are in stock.

    The seventh line of the file will contain ten positive real numbers with upto 2 decimal places representing the price of each of the drinks 0 through 9, in order.

    The eighth line of the file will contain a single positive integer, numEvents, representing the number of events that occur at the charity ball. These events are split into two groups: actions by guests at the ball and awards given (raffle, auction, person, totalrevenue). All of the actions precede all of the awards.

    You will produce exactly one line of output for each event described. Here are the formats of each event that could occur:

    If a patron buys a ticket at the door, a command will be on a line by itself:

    BUY TICKET k

    where k is a positive integer indicating the number of tickets bought at the door. These guests will be numbered as previously mentioned. You are guaranteed that the total number of tickets bought, including presales, will not exceed 1000.

    This is what I have so far and I cannot figure out why it wont calculate the total revenue. I am not completely sure if it is even accessing the if statement in main.

    Code:
     #include <stdio.h> #include <stdlib.h>
     #include <string.h>
     #define N 1000
    //Function for the tickets bought.
    int buy_tickets(int x);
    int num_presale,cost_presale, cost_at_door, num, total_revenue=0;
    int guests[N] = {0};
    char buyticket[];
    
    
    int main(){
        FILE * ifp =fopen("auction01.txt","r");
    // Used to find the string Buy Ticket in the file auction01.txt
     char command_ticket[10] = "BUY TICKET";
     char command[20];
    
    
    //Call the numbers from the file auction01.txt
     fscanf(ifp,"%d", &cost_presale);
     fscanf(ifp,"%d", &cost_at_door);
     fscanf(ifp,"%d", &num_presale);
     fscanf(ifp,"%s", command);
    // Using an if statement to find the string "Buy Ticket"
         if ( strcmp(command ,command_ticket) == 0)
            {
            fscanf(ifp, "%s", command_ticket);
            fscanf(ifp ,"%d", &num);
            // Calling the function buy_ticket
        int buy_tickets(num);
     }
    
    
    // Here for reference to make sure the file is being read
        printf("Presale cost: %d\nCost at door: %d\nNumber of presale: %d\n\n", cost_presale, cost_at_door, num_presale);
    
    
    fclose(ifp);
    // Print the total revenue in the file from the presold tickets and cost_at_door.
        printf("The total revenue is $%.2lf\n", total_revenue);
    
    
    return 0;
    }
    // function to calculate the the number of tickets
    int buy_tickets(int x){
        int i, first_ticket, last_ticket, total;
        static int guest_count = 0;
    
    
        for (i=0 ; i<x ; i++){
            if (num_presale != 0 ){
                total_revenue += cost_presale;
                num_presale--;
          }
          else{
            total_revenue += cost_at_door;
          }
    
    
          guests[guest_count] = guest_count;
          if (i == 0)
            first_ticket = guest_count;
          if (i == x-1)
            last_ticket = guest_count;
        }
        total = first_ticket - last_ticket;
        printf("The number of sold tickets are %d - %d =%d", first_ticket, last_ticket, total );
    return num;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    I have successfully read the first three numbers in the file.

    Here is the text file that I am reading from.
    15 25 200
    3 10
    17.99 22.99 109.99
    100 2 4
    5.99 99.99 20.00 49.99
    10 10 10 10 10 10 10 10 10 10
    3.99 5.99 7.99 8.00 5.00 5.00 5.00 6.00 7.00 9.99
    5
    BUY TICKET 8
    BUY TICKET 10
    BUY TICKET 1
    BUY TICKET 3
    TOTAL REVENUE

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Before you get to buying tickets, you need to deal with lines 2 to 8 of the text file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Quote Originally Posted by Salem View Post
    Before you get to buying tickets, you need to deal with lines 2 to 8 of the text file.
    So would I need to use

    fscanf(ifp, "%d", &test);

    where test is for the values in between lines 2 through 8.

    Also I figured out why it was not printing out the print statement in my function so I will re-post my code.

    Code:
     #include <stdio.h> #include <stdlib.h>
     #include <string.h>
     #define N 1000
    
    
    //Function for the tickets bought.
    int buy_tickets(int x);
    int num_presale,cost_presale, cost_at_door, num, total_revenue=0;
    int guests[N] = {0};
    
    
    int main(){
        FILE * ifp =fopen("auction01.txt","r");
    // Used to find the string Buy Ticket in the file auction01.txt
        char command_ticket[10] = "BUY TICKET";
        char command[20];
    
    
     //Call the numbers from the file auction01.txt
     fscanf(ifp,"%d", &cost_presale);
     fscanf(ifp,"%d", &cost_at_door);
     fscanf(ifp,"%d", &num_presale);
     fscanf(ifp,"%s", command);
     fscanf(ifp, "%s", command_ticket);
     //total_revenue = (cost_presale*num_presale);
    
    
       //Using an if statement to find the string "Buy Ticket"
         if ( strcmp( command , command_ticket) == 0){
                fscanf(ifp ,"%d", &num);
                // Calling the function buy_ticket
                 buy_tickets(num);
                 printf("%d", buy_tickets(num));
    
    
     }
        printf("%d\n", buy_tickets(num));
    
    
        //Here for reference to make sure the file is being read
        printf("Presale cost: %d\nCost at door: %d\nNumber of presale: %d\n\n", cost_presale, cost_at_door, num_presale);
    
    
    fclose(ifp);
        //Print the total revenue in the file from the presold tickets and cost_at_door.
    printf("The total revenue is $%.2lf\n", total_revenue);
    
    
    return 0;
    }
    //function to calculate the the number of tickets
    int buy_tickets(int x){
        int i, first_ticket, last_ticket, total;
        static int guest_count = 0;
    
    
        for (i=0 ; i<x ; i++){
            if (num_presale != 0 ){
                total_revenue += cost_presale;
                num_presale--;
          }
    
    
          else{
            total_revenue += cost_presale;
          }
    
    
          guests[guest_count] = guest_count;
         for ( i = 0; i>guest_count ; i++){
            if (i == 0)
                first_ticket = guest_count;
            if (i == x-1)
                last_ticket = guest_count;
            }
        }
        printf("The number of sold tickets are %d - %d \n\n", first_ticket, last_ticket );
    
    
        total_revenue = (cost_presale * num_presale) + (cost_at_door*guest_count);
    return  (num, total_revenue );
    }

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    3
    we still have to scan in the other lines even though we arent using them for the first part of the program. They are used for later parts. So scan in the rest of the lines like you did the first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Skipping feww lines while copying froma 1 file to another
    By alienator in forum C Programming
    Replies: 10
    Last Post: 01-07-2012, 02:23 AM
  2. reading a file with getline and skipping lines..
    By kocmohabt33 in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2011, 12:37 AM
  3. skipping lines with an input text file
    By kwikness in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2006, 09:11 AM
  4. skipping \n when reading from file
    By Spedge in forum C Programming
    Replies: 3
    Last Post: 08-26-2003, 08:05 AM
  5. Skipping 2 next line when reading in from file?
    By aspand in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2003, 10:22 AM

Tags for this Thread