I'm working on a problem that involves reading from a file, saving the information to an array of structs and classes, and then displaying the information. I've omitted some function definitions for the sake of trying to be brief. The applicable areas of my code are as follows:
void getInputFile (RentalAgency *ptr) {
int a=0, b=0, c=0;
int tempYear;
float tempPrice;
char tempMake[264], tempModel[264];
bool tempAvailable;
ifstream inputStream;
int *zipCodePtr=(*ptr).zipcode; //create pointer to struct zipcodes
RentalCar *inventoryPtr=(*ptr).inventory; //create pointer to an array of classes
while (a<3) {
On output, it should look like this:
Hertz 93619
2014 Toyota Tacoma, $115.12 per day, Available: 1
2012 Honda CRV, $85.1 per day, Available: 0
2015 Ford Fusion, $90.89 per day, Available: 0
2013 GMC Yukon, $110.43 per day, Available: 0
2009 Dodge Neon, $45.25 per day, Available: 1
Alamo 89502
2011 Toyota // more information is posted similar to above
Instead, there seems to error with my ptr variable, as it displays:
Hertz 93619
2014 Toyota Tacoma, $115.12 per day, Available: 1
2012 Honda CRV, $85.1 per day, Available: 0
2015 Ford Fusion, $90.89 per day, Available: 0
2013 GMC Yukon, $110.43 per day, Available: 0
2009 Dodge Neon, $45.25 per day, Available: 1
Alamo
89502 //It stops printing here
In my attempt to resolve this issue, I noticed upon incrementing ptr and displaying the agency name, it first printed Hertz, then Alamo, then 89502 (as opposed to the next name), then gibberish. Clearly there is an issue with saving information from the input file (which FYI, is similar to the format of the display) for the second and third agencies. Any assistance is greatly appreciated. C relates to the size of the inventory array, b relates to the size of the zipcode array, and a relates to 3 agencies I'm trying to save and display.
std::string, usestd::stringfor the variables that use an array ofchar. That might still not solve your problem but it should help.ptr=rentalAgencyArray; //set pointer back to the start of arrayThis is unnecessary (as is creatingptrinminthe first place). The array decays to a pointer (and passed by value) when you call the functions.