Hello,
I'm writing a program for class that holds information from a parking garage and calculates the cost of parking for each vehicle and the total revenue generated. The code I've written uses a struct Car which contains variables for Tag#(string), Make(string), Cost(int), Time in(int b/w 1 & 24), and Time out(int b/w 1 & 24). I'm having trouble doing some simple math to calculate the cost for a single car. I'm apparently having trouble with declaring my variable and the way I'm stating the variables inside of the equation.
Also my function protoype is returning a link error
Any help would be greatly appreciated seeing that this is due tomorrow.
I've highlighted the lines that I'm having issues with.
Here is the error message I'm receiving:
error C2228: left of '.Cost' must have class/struct/union type
Here's the code:
/***************************************************
* Author: Robert McGahee Date: 6/15/09 *
* Program: Parking Garage Project *
* This program takes the tag numberand the make *
* of a vehicle and calculates the time that it *
* was in the parking garage and bills the car *
* $10 for the first hour and $5 for every hour *
* afterwards. It also diplays the total made by *
* the garage for the day. *
*-------------------------------------------------*
* This syntax is the intellectual property of *
* the author. You may use the code and modify it,*
* as far as this header remains intact. *
* In no way shall the author be liable for any *
* losses or damages resulting from the use of this*
* program. Use AS-IS at your own risk. *
***************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
<code>int Total(struct Car Carlist[],int Cost,int i1);</code>
struct Car
{
string Tag;
string Make;
int Time_in;
int Time_out;
int Cost;
};
int main()
{
Car Carlist[100];
int i1;
int i;
int Revenue;
char Answer;
Answer='Y';
i1=0;
cout<<"****************************"<<endl;
cout<<"* WELCOME TO THE GARAGE *"<<endl;
cout<<"****************************"<<endl;
system("pause");
while(Answer=='Y'||Answer=='y')
{
system("cls");
cout<<"Please enter the tag number: ";
cin>>Carlist[i1].Tag;
cout<<"Please enter the make of the car: ";
cin>>Carlist[i1].Make;
cout<<"Please enter the hour (1-24) they entered the garage: ";
cin>>Carlist[i1].Time_in;
cout<<"Please enter the hour (1-24) they left the garage: ";
cin>>Carlist[i1].Time_out; <code>Carlist.Cost[i1]=(Carlist.Time_out[i1]-Carlist.Time_in[i1])*5+5;</code>
cout<<"Would you like to enter another car's information? (Y/N): ";
cin>>Answer;
if(Answer=='y'||Answer=='Y')
i1++;
}
Revenue=Total(Carlist,i1,Cost);
cout<<setfill(' ')<<left<<setw(10)<<" Tag #"<<right<<setw(15)<<"Make"<<right<<setw(8)<<"Time in"<<right<<setw(9)<<"Time out"<<right<<setw(5)<<"Cost"<<endl;
for(i=0;i<=i1;i++)
{
cout<<setfill(' ')<<left<<setw(10)<<Carlist[i].Tag<<right<<setw(15)<<Carlist[i].Make<<right<<setw(8)<<Carlist[i].Time_in<<right<<setw(9)<<Carlist[i].Time_out<<right<<setw(5)<<"$ "<<Carlist[i].Cost<<endl;
}
cout<<endl;
cout<<setfill(' ')<<left<<setw(33)<<"Total Cars"<<i1<<right<<setw(14)<<"Total Revenue"<<Revenue<<endl;
system("pause");
return 0;
}
int Total(Car Carlist[],int Cost, int i1)
{
int i;
int Tot_Cost;
Tot_Cost=0;
for(i=0;i<=i1;i++)
{
Tot_Cost=Carlist[i].Cost+Tot_Cost;
}
return Tot_Cost;
}
Thank you.
Without delving in you have several problems.
first
#define MAX_CAR 100
Then Carlist[ MAX_CAR ]
Also you need an overurn check on your data entry! no more then MAX_CAR's entered.
Also your Total function should be exclusive of the number of entered cars (il) not inclusive! Meaning don't use (i <= il) use (i < il)
Also your code gets unreadable. This isn't the ancient days of basic!
Use something like...
for (nCar = 0; nCar < nCarCount; nCar++ )
Also use unsigned int instead of int. You are dealing with unsigned numbers, so use that data type!
Also you can use things like
TotCost += CarList[ nCar ].Cost;
Also you need to typedef your struct. Otherwise you have to declare struct with each Car Carlist[] declaration!
typedef struct Car
{
string Tag;
string Make;
int Time_in;
int Time_out;
int Cost;
} Car;
Also you have an AoS (Array of Structures) not a SoA (Structure of Arrays.
Your Array is Carlist[] with individual data members within each.
Not an array of Cost[] or Time_out[] or Time_in[].
Please post your corrections. Would be nice to see your solution!
Also when done with that re-examine your math expression for cost!
If you leave within the same hour you're only charging $5.
And shorting yourself every following hour!
Thank you very much. After reading your post I feel that I have a much better understanding of how to use my variables when using an array of structures.
I actually did fix my assignment without using much of what you just taught me but, I tried the methods and I feel they will be of great help in the near future.
This is only my 8th program so I didn't want to use the methods that haven't been discussed in class as of yet.
Thank you again!
My new syntax is as follows:
/***************************************************
* Author: Robert McGahee Date: 6/15/09 *
* Program: Parking Garage Project *
* This program takes the tag numberand the make *
* of a vehicle and calculates the time that it *
* was in the parking garage and bills the car *
* $10 for the first hour and $5 for every hour *
* afterwards. It also diplays the total made by *
* the garage for the day. *
*-------------------------------------------------*
* This syntax is the intellectual property of *
* the author. You may use the code and modify it,*
* as far as this header remains intact. *
* In no way shall the author be liable for any *
* losses or damages resulting from the use of this*
* program. Use AS-IS at your own risk. *
***************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Car
{
string Tag;
string Make;
int Time_in;
int Time_out;
int Cost;
};
int main()
{
Car Carlist[100];
int i1;
int i;
int Revenue;
char Answer;
Answer='Y';
i1=0;
cout<<"*****************************"<<endl;
cout<<"* WELCOME TO THE GARAGE *"<<endl;
cout<<"* --------------------------*"<<endl;
cout<<"* $10 for the first hour *"<<endl;
cout<<"* $5 for every hour after *"<<endl;
cout<<"* Minimum of 1 hour charged *"<<endl;
cout<<"*****************************"<<endl;
system("pause");
while(Answer=='Y'||Answer=='y')
{
system("cls");
cout<<"Please enter the tag number: ";
cin>>Carlist[i1].Tag;
cout<<"Please enter the make of the car: ";
cin>>Carlist[i1].Make;
cout<<"Please enter the hour (1-24) they entered the garage: ";
cin>>Carlist[i1].Time_in;
cout<<"Please enter the hour (1-24) they left the garage: ";
cin>>Carlist[i1].Time_out;
Carlist[i1].Cost=((Carlist[i1].Time_out-Carlist[i1].Time_in)-1)*5+10;
cout<<"Would you like to enter another car's information? (Y/N): ";
cin>>Answer;
if(Answer=='y'||Answer=='Y')
i1++;
}
Revenue=0;
for(i=0;i<=i1;i++)
{
Revenue=Carlist[i].Cost+Revenue;
}
system("cls");
cout<<"***************************************"<<endl;
cout<<"* Today's results for the garage are: *"<<endl;
cout<<"***************************************"<<endl<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Tag #"<<left<<setw(10)<<"Make"<<right<<setw(15)<<"Time in"<<right<<setw(15)<<"Time out"<<right<<setw(12)<<"Cost"<<endl<<endl;
for(i=0;i<=i1;i++)
{
cout<<setfill(' ')<<left<<setw(10)<<Carlist[i].Tag<<left<<setw(10)<<Carlist[i].Make<<right<<setw(15)<<Carlist[i].Time_in<<right<<setw(15)<<Carlist[i].Time_out<<right<<setw(10)<<"$ "<<Carlist[i].Cost<<endl;
}
cout<<endl;
cout<<setfill(' ')<<left<<setw(15)<<"Total Cars: "<<i1+1<<right<<setw(20)<<"Total Revenue: $"<<Revenue<<endl;
system("pause");
return 0;
}