GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Aug-2006, 21:59
rashidbb rashidbb is offline
New Member
 
Join Date: Aug 2006
Posts: 19
rashidbb is an unknown quantity at this point

c prog problem. i need help pro's!!!


i have a problem with this question,
can anyone offer me a solution for this.
a c prog question...

im really lost in this field...

question : Write a program that will repetitively read and calculate the parking charge to be paid by customers in a day. The program will stop when the users decide not to continue. The charge is based on the following table:

Vehicle Rate
Car First 1 hour (or part of it) : $2.00
Every subsequent hours (or part of it): $1.00
Bus First 1 hour (or part of it): $3.00
Every subsequent hours (or part of it): $1.50


Please incorporate appropriate input validation in your program
  #2  
Old 21-Aug-2006, 00:20
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,377
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: c prog problem. i need help pro's!!!


Quote:
Originally Posted by rashidbb
i have a problem with this question,
can anyone offer me a solution for this.
a c prog question...
Yes, many of us can offer a solution. But we don't get your grade, you do. If you will read the Guidelines, you will discover how to get the best help in writing the program yourself.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 21-Aug-2006, 10:05
davis
 
Posts: n/a

Re: c prog problem. i need help pro's!!!


Quote:
Originally Posted by rashidbb
i have a problem with this question,
can anyone offer me a solution for this.
a c prog question...

im really lost in this field...

question : Write a program that will repetitively read and calculate the parking charge to be paid by customers in a day. The program will stop when the users decide not to continue. The charge is based on the following table:

Vehicle Rate
Car First 1 hour (or part of it) : $2.00
Every subsequent hours (or part of it): $1.00
Bus First 1 hour (or part of it): $3.00
Every subsequent hours (or part of it): $1.50


Please incorporate appropriate input validation in your program

Do you want a C program or a C++ program? Your title says "C," but this is a C++ forum. I just want to make sure of what you're asking. The requirements are unclear to me. It seems to be saying that the program should:

continuously loop until the user quits
calculate the total parking revenues based on vehicle types and durations parked
accept input (read) --assumed that this means accept vehicles for parking

Here's what I got from your requirements:

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

typedef vector<float> vRevenue;

float calculate_revenue( vRevenue& v )
{
    float revenue = 0.0F;
    for( int i = 0; i < (int)v.size(); i++ )
    {
	revenue += v[i];
    }
    return revenue;
}

int get_term( const float time )
{
    int hours = (int)(time);
    int minutes = (int)((time - hours)*101);
    if( minutes > 0 )
    {
	++hours;
    }
    return hours;
}

void accept_input( vRevenue& v )
{
    int i = 1;
    float time = 0;
    int term = 0;
    while( i )
    {
	cout << "Enter vehicle to be parked: \n1 = car 2 = bus 0 = quit ";
	cin >> i;
	if( i == 0 )
	{
	    break;
	}
	cout << "Enter time to be parked in fractional portions of hours: \n zero time forces program to quit ";
	cin >> time;
	if( time == 0.0F )
	{
	    break;
	}
	else
	{
	    term = get_term( time );
        if( term > 24 )
        {
            cout << "sorry, no parking beyond one day, see ya!" << endl;
            break;
        }
	}
	switch( i )
	{
	    case 1: // car
	    {
		if( time > 1 )
		{
		    v.push_back( 2.00F );
		    --term;
		    v.push_back( (float)(term * 1.00F) );
		}
		else
		{
		    v.push_back( (float)(term * 2.00F) );
		}
	    }
	    break;
	    case 2: // bus
	    {
		if( time > 1 )
		{
		    v.push_back( 3.00F );
		    --term;
		    v.push_back( (float)(term * 1.50F) );
		}
		else
		{
		    v.push_back( (float)(term * 3.00F) );
		}
	    }
	    break;
	    case 0: // quit
	    default:
		i = 0;
		break;
	}
    }
}
	    
int main()
{
    vRevenue v;
    accept_input( v );
    float revenue = calculate_revenue( v );
    cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
    cout << "Total Revenue from Parked Vehicles: " << revenue << endl;

    return 0;
}

Output:

Code:
Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 1 Enter time to be parked in fractional portions of hours: zero time forces program to quit 28 sorry, no parking beyond one day, see ya! Total Revenue from Parked Vehicles: 0.00 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 1 Enter time to be parked in fractional portions of hours: zero time forces program to quit .5 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 0 Total Revenue from Parked Vehicles: 2.00 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 1 Enter time to be parked in fractional portions of hours: zero time forces program to quit 1.1 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 0 Total Revenue from Parked Vehicles: 3.00 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 2 Enter time to be parked in fractional portions of hours: zero time forces program to quit .1 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 0 Total Revenue from Parked Vehicles: 3.00 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 2 Enter time to be parked in fractional portions of hours: zero time forces program to quit 1.1 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 0 Total Revenue from Parked Vehicles: 4.50 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 1 Enter time to be parked in fractional portions of hours: zero time forces program to quit 1.1 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 2 Enter time to be parked in fractional portions of hours: zero time forces program to quit 1.1 Enter vehicle to be parked: 1 = car 2 = bus 0 = quit 0 Total Revenue from Parked Vehicles: 7.50

Note that I basically abort on any undesired input, so you may want to modify some elements of that kind of behavior in your code. Hopefully this will get you started on your own code. Read the blasted guidelines! I know that WaltP must get very tired of telling every newcomer the same thing over and over and over and over and over and over^999.


:davis:
  #4  
Old 21-Aug-2006, 10:41
Gamer_2k4's Avatar
Gamer_2k4 Gamer_2k4 is offline
Member
 
Join Date: Apr 2005
Location: Wisconsin
Posts: 117
Gamer_2k4 will become famous soon enough

Re: c prog problem. i need help pro's!!!


What? A mod is giving someone a full code solution?
  #5  
Old 21-Aug-2006, 11:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,377
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: c prog problem. i need help pro's!!!


Quote:
Originally Posted by Gamer_2k4
What? A mod is giving someone a full code solution?
:davis: is not a mod. Notice under his name: "Regular Member". It has been mentioned before, though.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #6  
Old 22-Aug-2006, 03:59
rajeev nair rajeev nair is offline
Member
 
Join Date: Aug 2006
Posts: 104
rajeev nair is on a distinguished road

Re: c prog problem. i need help pro's!!!


Hello,

I have written a code in C++ which calculates the parking fee and finds the totally revenue earned on termination of loop.

The program works like this:

1) prompts user to enter vehicle type car('c') or bus ('b').

2) On entering the vehicle type another prompt asks whether vehicle is leaving or entering.

3)If the vehicle is entering the vehicle number gets stored in array along with the time of parking.

ex: car[carnum] = carnum;

where car[1000] is an array and carnum is the number of car.


4) On choosing exit as an option the user is prompted to enter vehicle number.If the vehicle number exists in the array then the timer difference is calculated and parking fee is calculated.

The array is cleared

ex: car[carnum] = 0;

5)If the user terminates the loop then total revenue is calculated from total revenues got from car parking and bus parking fees.




My code:

CPP / C++ / C Code:
#include<stdio.h>
#include<string.h>
#include <time.h>
#include <conio.h>
#include <iostream.h>


time_t ttimeofday;
struct tm *tmtimeofday;
time_t first, second;

char str[100] , str1[100];



  float car()

{

	         static int  car[1000], t2[1000], t3[1000], carnum , c1 = 0;
	          char ch, m;
	          static float charge[1000], total = 0;
	          float rate = 2.00, extra = 1.00;

                       cout << "\n entrance or exit: ";
                      cout << "\n 'x' -> EXIT ";
                      cout << "\n 'e' -> ENTRANCE"<<endl;
                       cin >> ch;

                    switch(ch)

                {


                        case 'x':

                        cout << " Enter the car number " ;
                        cin  >> carnum;

                          if( car[carnum] == carnum)

                          {

                             second = time(NULL);
                             t3[carnum] = second;

                             time(&ttimeofday);
   	                         tmtimeofday = localtime(&ttimeofday);

   	                         strftime(str, 26,  "%d-%b-%Y", tmtimeofday);
 	                         strftime(str1,  26, "%H:%M:%S", tmtimeofday);

 	                         clrscr () ;

                             cout << "\n DAY: " << str;
 	                         cout <<" \n\n TIME OF EXIT: " << str1;
                             cout << "\n\n The parking Time for car no "<< carnum << " = " << difftime(t3[carnum],t2[carnum]) << " seconds \n\n";


                                  if(difftime(t3[carnum],t2[carnum])  <=  3600)

                                  {

                                   charge[carnum] = rate;
                                   cout << "Parking charge for car no " << carnum << " = "<<charge[carnum]<<" $ \n";
                                   cout << "\n HAVE A NICE DAY!!! \n\n";

	                       }

	                        if(difftime(t3[carnum],t2[carnum]) > 3600)

	                         {

		              charge[carnum]  =  rate + ( ( (difftime(t3[carnum],t2[carnum]) /3600) *extra ) );

		               cout << "Parking charge for car no " << carnum <<  " = "<<charge[carnum] << " $ \n";
		                cout << "\n HAVE A NICE DAY!!! \n\n";

	                           }


	                               total = total  +  charge[carnum];

	                              cout << "\n The current revenue from car parking fees =  " << total<<"$ \n\n\n";
	                              car[carnum] = 0;

                           }

                            else
                            cout << "\n The car num does not exist \n" << endl;

                            break;


                           case 'e':

                           first = time(NULL);
                           c1++;
                           car[c1] = c1;
                           t2[c1] = first;
                           break;

               }

                     return total;

}




 float  bus()

{

	     static int  bus[1000],  t5[1000],  t6[1000], busnum, b1 = 0;
	     static float buscharge[1000],total1 = 0;
	     float busrate =3.00 ,busextra = 1.50;
	     char ch,m;


        cout << "\n entrance or exit: ";
        cout << "\n 'x' -> EXIT ";
        cout << "\n 'e' -> ENTRANCE"<<endl;
        cin >> ch;




                  switch(ch)

         {


                case 'x':

                cout << "Enter the bus number " ;
                cin >> busnum;

                if(bus[busnum] == busnum)

                 {

                 second = time(NULL);
                 t6[busnum] = second;

                 time(&ttimeofday);
   	             tmtimeofday = localtime(&ttimeofday);

     	         strftime(str,  26, "%d-%b-%Y", tmtimeofday);
 	             strftime(str1, 26, "%H:%M:%S", tmtimeofday);

 	             clrscr();
 	             cout << "\n DAY: "<<str;
 	             cout << "\n\n TIME OF EXIT: "<<str1;
                 cout << "\n\n The parking Time for bus no " << busnum << " = " << difftime(t6[busnum],t5[busnum]) << " seconds \n\n\n\n";


                         if(difftime(t6[busnum] , t5[busnum]) <= 3600)

		                  {

		                  buscharge[busnum] = busrate;
		                  cou t<< "Parking charge for bus no " << busnum << " = " << buscharge[busnum] << " $ \n";
		                  cout << "\n HAVE A NICE DAY!!! \n\n";

		                  }

			              if(difftime(t6[busnum], t5[busnum]) > 3600)

			              {

				           buscharge[busnum] = busrate + ( ( (difftime(t6[busnum], t5[busnum] ) / 3600) *busextra ) );

				           cout << "Parking charge for bus no " <<busnum << " = "<<buscharge[busnum] <<" $ \n";
				           cout << "\n HAVE A NICE DAY!!! \n\n";

	                       }


	                      total1 = total1 + buscharge[busnum];


	                      cout << "\n The current revenue from bus parking fees =  " <<total1 << "$ \n\n\n";

	                      bus[busnum] = 0;

                 }

                else
                cout << "\n The bus num does not exist \n" << endl;

                break;





               case 'e':

               first=time(NULL);
               b1++;
               bus[b1]=b1;
               t5[b1]=first;

               break;


        }

        return total1;

}





     void main()


{
	    char g , v;
	    static float tot = 0, tot1 = 0;




            for(; ;)

          {

              clrscr();

             cout << "Enter the type of vehicle : ";
             cout << "\n 'c' -> CAR " << endl;
             cout << "\n 'b' -> BUS " << endl;
             cin >> v;

                    switch(v)

                   {

                     case 'c':
                     tot = tot + car();
                     break;

                     case 'b':
                     tot1 = tot1 + bus();
                     break;

                    }

               cout << "\n Enter 'y' to exit the loop or 'n' to continue: ";
               cin >> g;


               if(g == 'y')  break;



           }


              clrscr();

             time(&ttimeofday);
	   	    tmtimeofday = localtime(&ttimeofday);

	     	strftime(str ,  26, "%d-%b-%Y", tmtimeofday);
	 	    strftime(str1 , 26, "%H:%M:%S", tmtimeofday);


	 	      cout << "\n\n\n\n\t\t DAY: " << str;
 	          cout << "\n \t\t TIME " << str1;
              cout << "\n \t\t Today's total revenue = " << tot+tot1 << "$ \n\n\n";


}


The output for this code looks like this:



Enter the type of vehicle :
'c' -> CAR

'b' -> BUS

c

entrance or exit:

'x' -> EXIT
'e' -> ENTRANCE

e


Enter 'y' to exit the loop or 'n' to continue:

n

Enter the type of vehicle :
'c' -> CAR

'b' -> BUS

c

entrance or exit:
'x' -> EXIT
'e' -> ENTRANCE

x

Enter the car number

1




DAY: 22-Aug-2006

TIME OF EXIT: 15:52:04

The parking Time for car no 1 = 8 seconds

arking charge for car no 1 = 2 $

HAVE A NICE DAY!!!


The current revenue from car parking fees = 2$



Enter 'y' to exit the loop or 'n' to continue:

y



DAY: 22-Aug-2006
TIME 15:52:33
Today's total revenue = 2$



On substituting printf (for cout) , scanf( for cin) wherever they are used the program can be converted to c coding as I hav'nt used much of C++ concepts.
I am sorry I am unable to align this code.Even after alignment it goes back to same state.Sorry If I am breaking any rules.


Thank You,
Regards,
Rajeev
Last edited by LuciWiz : 22-Aug-2006 at 07:32. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #7  
Old 22-Aug-2006, 05:14
MichaelS-R MichaelS-R is offline
Junior Member
 
Join Date: Apr 2006
Location: Berkshire, UK
Posts: 65
MichaelS-R is on a distinguished road

Re: c prog problem. i need help pro's!!!


I know I am not a mod but I have to comment: pls use C tags for your code. And for the record: horizontal spacing is good, too much vertical spacing is bad!
CPP / C++ / C Code:
// Your code
Happy to help with anything I can read!
__________________
Michael

Dual Opteron 280 (2 x dual core) with 2Gb RAM, 2x36GB system drives, 2T on 3Ware 9500Mi RAID controller. Running Fedora Core 4. Using Anjuta IDE. Developemnt in C++ with MySQL (via mysql++).
  #8  
Old 22-Aug-2006, 06:19
rajeev nair rajeev nair is offline
Member
 
Join Date: Aug 2006
Posts: 104
rajeev nair is on a distinguished road

Re: c prog problem. i need help pro's!!!


hello,


I have finally formatted code.Hope it is in readable format.

My code:

Quote:
CPP / C++ / C Code:
    #include<stdio.h>
    #include<string.h>
    #include <time.h>
    #include <conio.h>
   #include <iostream.h>


    time_t ttimeofday;
   struct tm *tmtimeofday;
   time_t first, second;

   char str[100] , str1[100];



  float car()

{

   static int car[1000], t2[1000], t3[1000], carnum , c1 = 0;
   char ch, m;
   static float charge[1000], total = 0;
   float rate = 2.00, extra = 1.00;

  cout << "\n entrance or exit: ";
  cout << "\n 'x' -> EXIT ";
  cout << "\n 'e' -> ENTRANCE"<<endl;
  cin >> ch;

          switch(ch)

        {


          case 'x':

          cout << " Enter the car number " ;
          cin >> carnum;

              if( car[carnum] == carnum)

                 {

                   second = time(NULL);
                   t3[carnum] = second;

                   time(&ttimeofday);
                   tmtimeofday = localtime(&ttimeofday);

                   strftime(str, 26, "%d-%b-%Y", tmtimeofday);
                    strftime(str1, 26, "%H:%M:%S", tmtimeofday);

                    clrscr () ;

                   cout << "\n DAY: " << str;
                   cout <<" \n\n TIME OF EXIT: " << str1;
                   cout << "\n\n The parking Time for car no "<< carnum << " = " << difftime(t3[carnum],t2[carnum]) << " seconds \n\n";


                                  if(difftime(t3[carnum],t2[carnum]) <= 3600)

                                   {

                                    charge[carnum] = rate;
                                    cout << "Parking charge for car no " << carnum << " = "<<charge[carnum]<<" $ \n";
                                    cout << "\n HAVE A NICE DAY!!! \n\n";

                                   }

                                   if(difftime(t3[carnum],t2[carnum]) > 3600)

                                   {

                                  charge[carnum] = rate + ( ( (difftime(t3[carnum],t2[carnum]) /3600) *extra ) );

                                 cout << "Parking charge for car no " << carnum << " = "<<charge[carnum] << " $ \n";
                                 cout << "\n HAVE A NICE DAY!!! \n\n";

                                    }


                               total = total + charge[carnum];

                               cout << "\n The current revenue from car parking fees = " << total<<"$ \n\n\n";
                               car[carnum] = 0;

                      }

                      else
                      cout << "\n The car num does not exist \n" << endl;

                     break;


              case 'e':

              first = time(NULL);
              c1++;
              car[c1] = c1;
              t2[c1] = first;
              break;

        }

         return total;

}




float bus()

{

   static int bus[1000], t5[1000], t6[1000], busnum, b1 = 0;
   static float buscharge[1000],total1 = 0;
   float busrate =3.00 ,busextra = 1.50;
   char ch,m;


   cout << "\n entrance or exit: ";
   cout << "\n 'x' -> EXIT ";
   cout << "\n 'e' -> ENTRANCE"<<endl;
   cin >> ch;




               switch(ch)

              {


                case 'x':

               cout << "Enter the bus number " ;
               cin >> busnum;

                   if(bus[busnum] == busnum)

                     {

                       second = time(NULL);
                       t6[busnum] = second;

                       time(&ttimeofday);
                       tmtimeofday = localtime(&ttimeofday);

                       strftime(str, 26, "%d-%b-%Y", tmtimeofday);
                       strftime(str1, 26, "%H:%M:%S", tmtimeofday);

                      clrscr();
                      cout << "\n DAY: "<<str;
                      cout << "\n\n TIME OF EXIT: "<<str1;
                      cout << "\n\n The parking Time for bus no " << busnum << " = " << difftime(t6[busnum],t5[busnum]) << " seconds \n\n\n\n";


                                         if(difftime(t6[busnum] , t5[busnum]) <= 3600)

                                          {

                                              buscharge[busnum] = busrate;
                                              cout << "Parking charge for bus no " << busnum << " = " << buscharge[busnum] << " $ \n";
                                              cout << "\n HAVE A NICE DAY!!! \n\n";

                                            }

                                            if(difftime(t6[busnum], t5[busnum]) > 3600)

                                            {

                                             buscharge[busnum] = busrate + ( ( (difftime(t6[busnum], t5[busnum] ) / 3600) *busextra ) );

                                             cout << "Parking charge for bus no " <<busnum << " = "<<buscharge[busnum] <<" $ \n";
                                             cout << "\n HAVE A NICE DAY!!! \n\n";

                                            }


                                           total1 = total1 + buscharge[busnum];


                                          cout << "\n The current revenue from bus parking fees = " <<total1 << "$ \n\n\n";

                                          bus[busnum] = 0;

                      }

                      else
                      cout << "\n The bus num does not exist \n" << endl;

                      break;





                case 'e':

                first=time(NULL);
                 b1++;
                 bus[b1]=b1;
                  t5[b1]=first;

                  break;


           }

              return total1;

}





void main()


{
   char g , v;
   static float tot = 0, tot1 = 0;




             for(; ;)

                {

                   clrscr();

                   cout << "Enter the type of vehicle : ";
                   cout << "\n 'c' -> CAR " << endl;
                   cout << "\n 'b' -> BUS " << endl;
                   cin >> v;

                     switch(v)

                         {

                            case 'c':
                            tot = tot + car();
                            break;

                            case 'b':
                            tot1 = tot1 + bus();
                             break;

                           }

                 cout << "\n Enter 'y' to exit the loop or 'n' to continue: ";
                 cin >> g;

                if(g == 'y')  break;



             }


           clrscr();

           time(&ttimeofday);
           tmtimeofday = localtime(&ttimeofday);

           strftime(str , 26, "%d-%b-%Y", tmtimeofday);
           strftime(str1 , 26, "%H:%M:%S", tmtimeofday);


          cout << "\n\n\n\n\t\t DAY: " << str;
          cout << "\n \t\t TIME " << str1;
          cout << "\n \t\t Today's total revenue = " << tot+tot1 << "$ \n\n\n";


}


The output for this code looks like this:

Enter the type of vehicle :
'c' -> CAR

'b' -> BUS
c

entrance or exit:

'x' -> EXIT
'e' -> ENTRANCE

e

Enter 'y' to exit the loop or 'n' to continue:

n

Enter the type of vehicle :
'c' -> CAR

'b' -> BUS

c

entrance or exit:
'x' -> EXIT
'e' -> ENTRANCE

x

Enter the car number

1




DAY: 22-Aug-2006

TIME OF EXIT: 15:52:04

The parking Time for car no 1 = 8 seconds

arking charge for car no 1 = 2 $

HAVE A NICE DAY!!!


The current revenue from car parking fees = 2$



Enter 'y' to exit the loop or 'n' to continue:

y



DAY: 22-Aug-2006
TIME 15:52:33
Today's total revenue = 2$



Thank You,
Regards,
Rajeev
  #9  
Old 22-Aug-2006, 09:02
davis
 
Posts: n/a

Re: c prog problem. i need help pro's!!!


If this is some kind of an assignment for a class, it seems strange to me that you would want to actually log the time in and out for each vehicle. Naturally, in a real parking garage that would occurr, but who is going to sit at a student programmer's application for several hours so that the bill can be validated if the vehicle says in more than 1 hour, more than a day?

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <conio.h>  // non standard header!
#include <iostream.h>  // old style header declaration!


// look at all of these globals!
time_t ttimeofday;
struct tm *tmtimeofday;
time_t first, second;

char str[100] , str1[100];

// Look at the similarities between "car" and "bus" functions!
// These can easily be combined into 1 function that takes, yes, ARGUMENTS!

float car()
{
    static int car[1000], t2[1000], t3[1000], carnum , c1 = 0;
    char ch, m;
    static float charge[1000], total = 0;
    float rate = 2.00, extra = 1.00;

    cout << "\n entrance or exit: ";
    cout << "\n 'x' -> EXIT ";
    cout << "\n 'e' -> ENTRANCE"<<endl;
    cin >> ch;

    switch( ch )
    {
        case 'x':
            cout << " Enter the car number " ;
            cin >> carnum;

            if( car[carnum] == carnum )
            {
                second = time(NULL);
                t3[carnum] = second;

                time(&ttimeofday);
                tmtimeofday = localtime(&ttimeofday);

                strftime(str, 26, "%d-%b-%Y", tmtimeofday);
                strftime(str1, 26, "%H:%M:%S", tmtimeofday);

                clrscr () ; // non standard function!  Don't use it!

                cout << "\n DAY: " << str;
                cout <<" \n\n TIME OF EXIT: " << str1;
                cout << "\n\n The parking Time for car no "<< carnum << " = " << difftime(t3[carnum],t2[carnum]) << " seconds \n\n";

                if( difftime(t3[carnum],t2[carnum]) <= 3600 )
                {
                    charge[carnum] = rate;
                    cout << "Parking charge for car no " << carnum << " = "<<charge[carnum]<<" $ \n";
                    cout << "\n HAVE A NICE DAY!!! \n\n";  // don't need more than one exclamation mark...
                }

                if( difftime(t3[carnum],t2[carnum]) > 3600 )
                {
                    charge[carnum] = rate + ( ( (difftime(t3[carnum],t2[carnum]) /3600) *extra ) );

                    cout << "Parking charge for car no " << carnum << " = "<<charge[carnum] << " $ \n";
                    cout << "\n HAVE A NICE DAY!!! \n\n";
                }

                total = total + charge[carnum];

                cout << "\n The current revenue from car parking fees = " << total<<"$ \n\n\n";
                car[carnum] = 0;
            }
            else
                cout << "\n The car num does not exist \n" << endl;
            break;
        case 'e':
            first = time(NULL);
            c1++;  // so when we get to the 1001st car, we've exploited an array bounds overflow
            car[c1] = c1; // because we never checked to see if "c1" is larger than the static size of our array!
            t2[c1] = first;
            break;
    }
    return total;
}

float bus()  // nearly identical to "car" with the exception of the rate, why use two separate functions if
{            // the only real difference is the rate?
    static int bus[1000], t5[1000], t6[1000], busnum, b1 = 0;
    static float buscharge[1000], total1 = 0;
    float busrate =3.00, busextra = 1.50;
    char ch,m;

    cout << "\n entrance or exit: ";
    cout << "\n 'x' -> EXIT ";
    cout << "\n 'e' -> ENTRANCE"<<endl;
    cin >> ch;

    switch( ch )
    {
        case 'x':
            cout << "Enter the bus number " ;
            cin >> busnum;

            if( bus[busnum] == busnum )
            {
                second = time(NULL);
                t6[busnum] = second;

                time(&ttimeofday);
                tmtimeofday = localtime(&ttimeofday);

                strftime(str, 26, "%d-%b-%Y", tmtimeofday);
                strftime(str1, 26, "%H:%M:%S", tmtimeofday);

                clrscr();
                cout << "\n DAY: "<<str;
                cout << "\n\n TIME OF EXIT: "<<str1;
                cout << "\n\n The parking Time for bus no " << busnum << " = " << difftime(t6[busnum],t5[busnum]) << " seconds \n\n\n\n";

                if( difftime(t6[busnum] , t5[busnum]) <= 3600 )
                {
                    buscharge[busnum] = busrate;
                    cout << "Parking charge for bus no " << busnum << " = " << buscharge[busnum] << " $ \n";
                    cout << "\n HAVE A NICE DAY!!! \n\n";
                }

                if( difftime(t6[busnum], t5[busnum]) > 3600 )
                {
                    buscharge[busnum] = busrate + ( ( (difftime(t6[busnum], t5[busnum] ) / 3600) *busextra ) );

                    cout << "Parking charge for bus no " <<busnum << " = "<<buscharge[busnum] <<" $ \n";
                    cout << "\n HAVE A NICE DAY!!! \n\n";
                }

                total1 = total1 + buscharge[busnum];

                cout << "\n The current revenue from bus parking fees = " <<total1 << "$ \n\n\n";

                bus[busnum] = 0;
            }

            else
                cout << "\n The bus num does not exist \n" << endl;
            break;
        case 'e':
            first=time(NULL);
            b1++;
            bus[b1]=b1;
            t5[b1]=first;

            break;
    }
    return total1;
}

void main()  // this is not a legal signature for main!  It returns an int!
{
    char g, v;
    static float tot = 0, tot1 = 0;
    for( ; ; )
    {
        clrscr();

        cout << "Enter the type of vehicle : ";
        cout << "\n 'c' -> CAR " << endl;
        cout << "\n 'b' -> BUS " << endl;
        cin >> v;

        switch( v )
        {
            case 'c':
                tot = tot + car();
                break;
            case 'b':
                tot1 = tot1 + bus();
                break;
        } // no default cases in any of your switch statements!

        cout << "\n Enter 'y' to exit the loop or 'n' to continue: ";
        cin >> g;

        if( g == 'y' )  break;
    }
    clrscr();

    time(&ttimeofday);
    tmtimeofday = localtime(&ttimeofday);

    strftime(str , 26, "%d-%b-%Y", tmtimeofday);
    strftime(str1 , 26, "%H:%M:%S", tmtimeofday);

    cout << "\n\n\n\n\t\t DAY: " << str;
    cout << "\n \t\t TIME " << str1;
    cout << "\n \t\t Today's total revenue = " << tot+tot1 << "$ \n\n\n";
}


I've nested some comments in your code.

Basically, you could rewrite the program so that it has functions that take ARGUMENTS so that you don't have so much duplicity in your code.

Your use of a fixed sized array and failure to check the bounds of it when you blindly add a new vehicle to the array is commonplace among novice programmers.

Your application design says that the parking facility can accept 1000 cars and 1000 buses. You do not allow for 1500 cars and 500 buses, which in real life would suggest the possibility of missed revenue for the "day," which isn't being calculated on a daily basis, though is not necessarily stated in such words in the requirements.

Since the size of the parking facility was never limited in the requirements, your bounded arrays unnecessarily limit it.

Please do not use non-standard functions and headers in your code. conio.h and clrscr are not part of ANSI or ISO C. We want everyone to learn to C properly

You use a number of global variables in your code. Don't, unless you have a really good reason.

We don't mix C and C++ headers in the manner that you've chosen. If the file is a C++ file, then we use #include <cstdio> for example and not #include <stdio.h>

Your code tends to do too much in each of car and bus functions. This is similar in design to the "main does everything" kind of programming style. However, your code is simply one layer removed from main. Ideally, the code would simply add a vehicle of type bus or car to an appropriate storage type and produce an "entry" timestamp that follows the "vehicle" type.

For example:

CPP / C++ / C Code:
typedef struct st_vehicle
{
    int type;
    time_t t_enter;
    time_t t_exit;
} VEHICLE;

Now we can happily add as many vehicles of any declared type(s) to an array of VEHICLEs and realloc as necessary to keep from exceeding the bounds of our limiting array dimensions. Also, we can segment the program into some logical functioning blocks that separate "roles and responsibilities" better.

On event vehicle entry, we punch in the t_enter timestamp, denote the type and add the structure to an array or linked list.

On event vehicle exit, we punch the t_exit timestamp, difference the times and calculate the rate based on our rate rules.

By partitioning the application, we gain flexibility in making changes to it and making it easier to read and understand. Also, this piecemeal approach is a divide and conquer method that lets us focus on doing more simple tasks in a small section of code that can easily be independently tested and verified. Large "monolithic" functions tend to be cluttered, confusing and difficult to read, write and test. Changing monoliths are difficult at best, though in your case in this code, it is not terribly severe since the application is trivial at best. However, these lessons are better learned as soon as possible rather than later in life. In other words, do not try to do too much in one function.

Let me know if you'd like for me to post a "refactoring" of your code to give you a better idea of what I'm going on about.


:davis:
  #10  
Old 22-Aug-2006, 11:15
rajeev nair rajeev nair is offline
Member
 
Join Date: Aug 2006
Posts: 104
rajeev nair is on a distinguished road

Re: c prog problem. i need help pro's!!!


Hi,

Thank you for the review.I was just giving a rough solution for the problem,so my aim was just to write a code which would calculate the parking charge on a continous basis.

Although I do agree with the fact that function for bus and car could combined under one so that length of code could be reduced.

But regarding limiting size of storage to 1000,I dont agree.Since whenever a vehicle moves out the value in the array coressponding to that vehicle is cleared back to zero.

For ex:
every time a car exits

car[carnum] = 0

array is cleared.


If the number of cars reaches it's maximum limit then the array which gets cleared can be used to store (although I have'nt made it part of the code).

This code was just a crude example.I need to make further changes.Please post your version of code so I get a better understanding.


Thank You,
Regards,
Rajeev
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:43.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.