Thread: Please help with simulation of a parking machine meter

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    2

    Please help with simulation of a parking machine meter

    Hi,
    Please I need help writing a C program that simulates a parking machine meter.
    The instructions are on the following link:
    http://cop2270.mlahlou.info/files/HW4S13.pdf

    My problem is that when "printing the receipt", under the Garage Location I get a number (from 1 to 3). How can I make it print valet parking, garage parking and economy parking (as the example) for each case?

    Thank you in advance

    so far I have this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
    
    int opt = 0, h = 0, m = 0;
    float pay = 0;
    
    
    FILE*data;
    data = fopen ("parking_receipt.txt","w");
    
    void print()
    {
    fprintf(data, "\nRECEIPT\n\n \nGarage Location \t\tAmount of Time \t\t\tTotal Due"
    "\n%d \t\t%dh %dmin \t\t%.2f", opt, h, m, pay);
    }
    
    
    do{
    system("cls");
    printf("\nWELLCOME TO THE MIAMI INTERNATIONAL AIRPORT\n\n");
    printf("We offer you the following parking options:\n\n");
    printf("1. Valet Parking\n");
    printf("2. Garage Parking\n");
    printf("3. Economy Parking\n");
    printf("4. Print Receipt and/or Exit\n");
    printf("\nPlease select your parking option:");
    scanf("%d", &opt);
    
    switch(opt)
    {
    case 1:
    
    system("cls");
    printf("\nValet Parking\n\n");
    printf("\nRates:\n");
    printf("\t\t0 - 3 hours: $18.00");
    printf("\n\t\t3 - 24 hours: $30.00");
    printf("\n\t\tmax $30.00/day");
    printf("\n\nPlease enter the amount of hours that you're going to stay:");
    scanf("%d%d", &h, &m);
    
    
    
    if(h <= 3)
    {
    pay = 18;
    printf("%.2f\n", pay);
    print();
    
    }
    else
    {
    if(h > 3, h <= 24)
    {
    pay = 30;
    printf("%.2f\n", pay);
    print();
    
    }
    }
    
    getch();
    break;
    case 2:
    system("cls");
    printf("\nGarage Parking\n\n");
    printf("\nRates:\n");
    printf("\t\tEvery 20 min: $2.00");
    printf("\n\t\tmax $17.00/day");
    printf("\n\nmaximun rate applies after 160 min");
    printf("\n\nPlease enter the amount of hours and min that you're going to stay:");
    scanf("%d%d", &h, &m);
    
    if(h <= 2, m < 60, ((h*60) + m) <= 160)
    {
    pay = (h*6) + (m*.1);
    printf("%.2f\n", pay);
    print();
    
    }
    else
    {
    if(h > 2, m < 60, (h*60) + m > 160)
    {
    pay = 17;
    printf("%.2f\n", pay);
    print();
    
    }
    }
    
    getch();
    break;
    case 3:
    system("cls");
    printf("\nEconomy Parking\n\n");
    printf("\nRates:\n");
    printf("\t\t0 - 3 hours: $3.00/hour");
    printf("\n\t\tmax $8.00/day");
    printf("\n\nmaximun rate applies after 160 min");
    printf("\n\nPlease enter the amount of hours and min that you're going to stay:");
    scanf("%d%d", &h, &m);
    
    if(h <= 3, m < 60,((h*60) + m) < 160 )
    {
    pay = (h*3) + (m*.05);
    printf("%.2f\n", pay);
    print();
    
    }
    else
    {
    if(h > 3, m > 160,((h*60) + m) > 160 )
    {
    pay = 8;
    printf("%.2f\n", pay);
    print();
    
    }
    }
    
    getch();
    break;
    
    }
    }while(opt !=4);
    
    
    
    fclose(data);
    
    return 0;
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    If I undersand, you want to use the values 1, 2, 3, to select the text "valet parking", "garage parking" and "economy parking" for output.

    You would want an array of pointers to strings:

    Code:
    char *garage_location[] = { "valet parking", "garage parking", "economy parking" };
    and you reference them as:
    Code:
    garage_location[0]
    garage_location[1]
    garage_location[2]
    explicitly, or calculated as:
    Code:
    garage_location[location]
    For example, garage_location[1], is a pointer to the string, "garage parking".

    Also, you should indent the code properly, it's difficult to read.
    I am guessing that the part that is "printing the receipt" is not in there yet?

    Arrays naturally start at 0; you can either subtract 1 from your location variable to index (select) the string,
    or waste the first array element:
    Code:
    char *garage_location[] = { "", "valet parking", "garage parking", "economy parking" };
    Now garage_location[0] is a null string and the three strings of interest are referenced by garage_location[1], garage_location[2], garage_location[3].
    So you could use use the location variable directly as an index.
    Last edited by megafiddle; 06-08-2013 at 09:34 PM.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    You've declared a function inside a function. Some languages allow that, but not C. The print() function must be moved outside main().

    Code:
    void print(int opt, int h, int m, float pay)
    {
        fprintf(data, "\nRECEIPT\n\n \nGarage Location \t\tAmount of Time \t\t\tTotal Due"
                "\n%d \t\t%dh %dmin \t\t%.2f", opt, h, m, pay);
    }
    
    
    int main(int argc, char *argv[])
    {
        int opt = 0, h = 0, m = 0;
        float pay = 0;
    
        FILE *data = fopen("parking_receipt.txt","w");
    
        print(opt,h,m,pay);
    ...
    Also, please use [code][/code] tags.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Ok, I see the receipt printing function.
    Admittedly, I didn't read it very closely due to the lack of indentation.

    Just declare and initialize the pointers:
    Code:
    char *garage_location[] = { "valet parking", "garage parking", "economy parking" };
    and use them like this:
    Code:
        fprintf(data, "\nRECEIPT\n\n \nGarage Location \t\tAmount of Time \t\t\tTotal Due"
                "\n%s \t\t%dh %dmin \t\t%.2f", garage_location[opt-1], h, m, pay);
    Notice this uses %s for the string.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    2
    Thanks guys for your help I got it now. your the best!!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, if you want to go with megafiddle's suggestion, then this:
    Code:
    char *garage_location[] = { "valet parking", "garage parking", "economy parking" };
    should be:
    Code:
    const char *garage_location[] = { "valet parking", "garage parking", "economy parking" };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by jomoro View Post
    your the best!!
    10 Words You Need to Stop Misspelling - The Oatmeal
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-22-2009, 04:11 AM
  2. Machine Simulation
    By michelpc in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-05-2005, 04:40 PM
  3. HTTP traffic meter
    By Carlos in forum Windows Programming
    Replies: 3
    Last Post: 10-02-2003, 04:27 PM
  4. Progress meter color
    By Echidna in forum Windows Programming
    Replies: 3
    Last Post: 11-29-2002, 03:30 AM
  5. Machine language simulation
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 06-20-2002, 03:52 PM

Tags for this Thread