Solved

Simulation of a parking machine meter

Posted on 2013-06-07
6
296 Views
Last Modified: 2013-06-16
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

So far I have this, but when I enter the time the program stops working

#include <stdio.h>
#include <stdlib.h>

int main (void)
{

   int opc = 0, num1 = 0, num2 = 0;
   FILE*data;
   data = fopen ("parking_recipt.txt","r");
   do{
       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. Exit\n");
       printf("\nPlease select your parking option:");
       scanf("%d", &opc);

       switch(opc)
       {
       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", &num1);

               for (num1 = 0; num1<= 24; ++num1)
               {


                   if(num1 <= 3)
                   {
                    num2 = 18;
                    printf("data, %d\n", num2 );
                    fprintf("data, %d\n", num2 );

                   }
                   if(num1 > 3)
                   {
                    num2 = 30;
                    printf("data, %d\n", num2 );
                    fprintf("data, %d\n", num2 );
                   }

               }

               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", &num1);
               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", &num1);
               getch();
               break;

       }
   }while(opc !=4);



fclose(data);

    return 0;
}
0
Comment
Question by:lolo07
6 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 39231161
Hi lolo07,

IMO there are at least two problems regarding the creation/writing to the txt file. First you open the for read-only, you need to pass "w" instead of "r" to fopen. Next your fprintf calls are wrong, the first argument has to be the FILE pointer, i.e.:

  fprintf(data, "%d\n", num2 );

writes the number stored in num2 as text to the file pointed to by data.

Further (regarding the assignment) I think there are two things missing yet: the user can't enter minutes as requested in the assignment and some more text has to be written to the txt file in the requested format.

Hope that helps,

ZOPPO
0
 

Author Comment

by:lolo07
ID: 39232280
Thank you ZOPPO,

I did fix it, but now when "printing the receipt", under Garage Location I get a number (from 1 to 3). How can I make it print valet parking, garage parking and economy parking for each case?

I have this:

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



lolo07
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 39232678
Hm - I think the code where the receipt is printed is missing in your last comment, right? So I can't really say how to solve it.

In general you IMO need to print different strings depending on the value of a number. There are some ways to do it, an easy one is to even use a switch/case i.e. somehow like this:

switch( number )
{
 case 1:
  printf( "valet parking" );
  break;
 case 2:
  printf( "garage parking" );
  break;
 case 3:
  printf( "economy parking" );
  break;
}

ZOPPO
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 14

Expert Comment

by:frankhelk
ID: 39237188
The most simple way would work with an array of string constants. More code writin would be a switch construct. For both look at this example:
#include <stdlib.h>

const char *parktype[4] = {"0", "Garage", "Valet", "Economy"};

int main(void)
{
	
    int i = rand() % 3;

	printf("Array:\n");
	printf("  %d %s \n", i, parktype[i]);
	
	printf("\nSwitch:\n");
	switch(i)
	{
		case 1:
			printf("  %d Garage\n", i);
			break;
		case 2:
			printf("  %d Valet\n", i);
			break;
		case 3:
			printf("  %d Economy\n", i);
			break;
		default:
			printf("  %d ???\n", i);
			break;
	}

	
	return 0;
}

Open in new window


Please remind that array indices start at 0. You have to substract 1 to get the index or cope with the value 0 in another way (what I did by giving it a separate string value). The usage of rand() and the % operator is for educational purposes - RTFM.
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 39239306
Re-posting the comment that I posted in your other, duplicate question:





A couple things with your code:

1) The print() function is defined within the main() function which isn't correct. You need to move it outside the main() function. Also, I'd give it a more unambiguous name, like printReceipt(). You'll also need to pass the variables that it needs in as parameters (the data, opt, h, m, and pay variables)

2) Your Option 4 is redundant - your code is already calling the print() function to print the receipt, so option 4 doesn't really make any sense. You might want to rethink the logic in your menu system. Or maybe that doesn't matter for your assignment. That's up to you.

That being said, your question itself seems to be about the print() function itself, and how to print the text "Valet Parking", "Garage Parking", "Economy Parking" etc. depending on what the integer variable "opt" has been set to.

There are a number of ways to do it, but one option is to define a static array of char strings, and use the "opt" variable to reference one of the elements of that array. I'm not 100% sure on the exact syntax of the char array definition, but I think this is correct:

void printReceipt(FILE*data, int opt, int h, int m, float pay)
{
  char* parkingtypes[] = {"na", "Valet Parking", "Garage Parking", "Economy Parking"};

  fprintf(data, "\nRECEIPT\n\n \nGarage Location \t\tAmount of Time \t\t\tTotal Due\n%s \t\t%dh %dmin \t\t%.2f\n", parkingtypes[opt], h, m, pay);
}


Or, if you haven't gotten to arrays and pointers, yet, another simple way is to use a few IF statements. Again I haven't checked for perfect syntax but basically something like this:

void printReceipt(FILE*data, int opt, int h, int m, float pay)
{

  fprintf(data, "\nRECEIPT\n\n \nGarage Location \t\tAmount of Time \t\t\tTotal Due\n");

  if( opt == 1 ) {
     fprintf(data, "Valet Parking");
  } else if( opt == 2 ) {
     fprintf(data, "Garage Parking");
  } else if( opt == 3 ) {
     fprintf(data, "Economy Parking");
  }
  fprintf(data, "\t\t%dh %dmin \t\t%.2f\n", parkingtypes[opt], h, m, pay);
}
0
 

Author Closing Comment

by:lolo07
ID: 39250965
Thanks guys help me allot and sorry for giving the pts late
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C Programming - If Statement 8 77
What is sub-make ? 2 60
How to learn Linux? 10 61
Android development question 2 37
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question