I've created a RentalAgency struct which contains a name, zip code, and inventory. I've also created a RentalCar class which contains the members of the inventory. I'm trying to read in the data from the text file into each respective place and I'm having trouble with it.
struct RentalAgency {
char name[25]; //25 characters max
int zipcode[5]; //5 digits in zipcode
RentalCar inventory[5]; //5 cars
};
class RentalCar {
int m_year;
char m_make[256], m_model[256]; //256 characters max
float m_price;
bool m_available;
public:
RentalCar();
RentalCar(int, char[], char[], float, bool);
void setYear(int);
void setMake(char[]);
void setModel(char[]);
void setPrice(float);
void setAvailable(bool);
int getYear();
char* getMake();
char* getModel();
float getPrice();
bool getAvailable();
void print();
float estimateCost(int);
};
I'm attempting to read from this text file.
Hertz 93619
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
Alamo 89502
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 112.83 1
2010 Toyota Corolla 50.36 1
Budget 93035
2008 Ford Fiesta 42.48 0
2009 Dodge Charger 55.36 1
2012 Chevy Volt 89.03 0
2007 Subaru Legacy 59.19 0
2010 Nissan Maxima 51.68 1
So far I've set up a function to read the data. I've managed to create a for loop that reads in the Rental Agency name, but I get stuck when it comes to the zip code.
void input(struct RentalAgency data[])
{
char inputFile[50]; //50 characters max
char tmp;
std::ifstream inputStream;
std::cout << "Enter input file name: ";
std::cin >> inputFile;
inputStream.open(inputFile);
for(int i = 0; i < 3; i++) //3 agencies
{
inputStream >> data[i].name;
for(int j = 0; j < 5; j++)
{
inputStream >> tmp;
data[i].zipcode[j] = tmp;
}
}
}
My output when I print the data is:
data[0].name = Hertz //correct
data[0].zipcode[0] = 57 //wrong
data[0].zipcode[1] = 51 //wrong
data[0].zipcode[2] = 54 //wrong
data[0].zipcode[3] = 49 //wrong
data[0].zipcode[4] = 57 //wrong
What I want is:
data[0].zipcode[0] = 9
data[0].zipcode[1] = 3
data[0].zipcode[2] = 6
data[0].zipcode[3] = 1
data[0].zipcode[4] = 9
char[6]. Problem solved. But unless you like dealing with buffer overflows and memory corruption, and you wish to write modern C++ code, you will want to usestd::strings instead of plainchararrays.int[5]. Either that or the assignment is bonkers.