Thread: Coding Problem-Help plz

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    Coding Problem-Help plz

    HI, I'm new at this so I just need a little bit of assistance in this. I have done the source code and everything for this problem but can't do 1 thing. I need the program to end when I type 99 hours. Below is the problem and source code. I would appreciate anyone's help. thanks.

    A parking garage charges a $2.00 minimum fee to park for up to 3 hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of 3 hours. The maximum charge is $10.00. (Hint: That means you dont have to calculate for anything over 18 hours.) Write a program that calculates and prints the parking charges for a number of cars. The user will enter the number of hours in hours and tenths of an hour. For example, the user will enter 4.5 for 4 hours and 30 minutes. The user will enter 99 hours to signal that the program should end.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int car[3];
    	double hours[3],  total_hours  = 0;
    	double charge[3], total_charge = 0;
    	int i;
    	for ( i = 0; i < 3; ++i )
    	  //car[i]=0;
    	{
    		printf("Enter car number? ");
    		scanf("%d", &car[i]);
    
    		printf("Enter hours parked? ");
    		scanf("%lf", &hours[i]);
    
    		if ( hours[i] < 3 )
    		{
    			charge[i] = 2.0;
    		}
    		else if ( hours[i] < 18 )
    		{
    			charge[i] = 2.0 + (hours[i] - 3.0) * 0.50;
    		}
    		else
    		{
    			charge[i] = 10.0;
    		}
    
    	  //printf("%1f%1f\n", "car" "hours" "charge");
    
    	  //for (car=1; car < 3; ++car) {
    		//printf("%d%1f%1f\n", car,hours,charge[hours]);
    		//printf("%.2f%.2f\n", total_hours[hours],total_charge[charge]);
    
    		total_hours = total_hours + hours[i];
    		total_charge = total_charge + charge[i];
    	}
      printf("%s  %s   %s\n", "car", "hours", "charge");
    
    	for (i=0; i < 3; ++i) {
    	 printf("%d    %.1f     %.2f\n", car[i], hours[i], charge[i]);
    	}
    	printf("    %.2f    %.2f\n", total_hours/*[hours]*/,total_charge/*[charge]*/);
    
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Great, so you've posted some source code. What's the problem? What does it do? What doesn't it do that it should? What does it do that ti shouldn't? What output does it generate now? What output is it supposed to output? Starting to understand now? In other words: Ask Smart Questions!


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    end the program

    I need the program to end when I type 99 hours...I dont know how to do that!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    break will break out of a loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Like
    Code:
    int i = 0;
    while(1) {
        scanf("%i", &i);
        if(i==99) break;
        printf("%i", i);
    }
    printf("\ndone\n");
    Input/output:
    Code:
    2
    3
    7
    85
    200
    99
    
    done
    Or you can put another condition in the for loop:
    Code:
    for ( i = 0; i < 3 && input != 99; ++i )
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Thanks very much!

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    oh sorry, one more thing, how do I put this into my code?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In here
    Code:
    printf("Enter hours parked? ");
    		scanf("%lf", &hours[i]);
    
    /* . . . right here . . . */
    
    		if ( hours[i] < 3 )
    		{
    			charge[i] = 2.0;
    		}
    Add something like
    Code:
    if(hours[i] == 99) break;
    That will break out of the
    Code:
    for ( i = 0; i < 3; ++i )
    	  //car[i]=0;
    	{
    loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Thanks alot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. One more coding problem
    By zrepaer in forum C Programming
    Replies: 1
    Last Post: 04-09-2009, 05:37 PM
  3. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. Strange problem. PLZ HELP
    By Veneran in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2005, 07:47 PM