Link to home
Start Free TrialLog in
Avatar of lolo07
lolo07

asked on

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

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;
}
Avatar of Zoppo
Zoppo
Flag of Germany image

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
Avatar of lolo07
lolo07

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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);
}
Avatar of lolo07

ASKER

Thanks guys help me allot and sorry for giving the pts late