0

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.

4
  • A general suggestion - Unless you are forbidden from using std::string, use std::string for the variables that use an array of char. That might still not solve your problem but it should help. Commented Feb 13, 2019 at 5:42
  • It is forbidden, however, thank you for the suggestion. Commented Feb 13, 2019 at 5:44
  • That's unfortunate. It will be difficult to see where the problem is without a minimal reproducible example. Try to create one. Commented Feb 13, 2019 at 5:48
  • ptr=rentalAgencyArray; //set pointer back to the start of array This is unnecessary (as is creating ptr in min the first place). The array decays to a pointer (and passed by value) when you call the functions. Commented Feb 13, 2019 at 5:51

2 Answers 2

1

You need to reset b and c each time through the outer loop in both routines. What's happening is (in both the input and output routine) b and c have the value of 5 after the first time through the outer (a) loop.

You can solve the problem by moving the initialization of b and c into the while loop.

while(a < 3)
{
   int b = 0, c = 0;
   // ... rest of code ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

If you're allowed use for loops you would be less likely to make this error.
Thank you for the assistance and time spent. I did not notice that minor detail.
It works for the most part. It properly displays zipcodes and data for all three agencies. It's not displaying the first year, first make, or agency 2's name and 3's name properly but I haven't gotten the chance to identify the probable causes.
If you know how to use a debugger set some breakpoints to make sure the data is being stored correctly into your RentalCar and RentalAgency class. If you don't know how to use a debugger you can use some temporary output statements to check. Might lead you to the remaining problem. I can't see anything else obvious in the code you showed!
Thanks again for the assistance and advice!
0

If it's stopping earlier than expected, it seems like it's a problem coming from the loop condition.

If you're getting gibberish, it seems like a problem with your indices where you're reaching outside the array which is why you're getting garbage values.

It would also help in terms of readability if you had more descriptive variable names. a? b? c?

1 Comment

The gibberish factor is definitely from going out of bounds. It shouldn't be a loop condition problem, as it simply isn't displaying on the second iteration after the name. On the third interpretation, it's interpreting the zipcode to be the next agency name, which should not be the case. To me this seems like the problem is coming from pointer syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.