简体   繁体   English

C ++ —指向结构数组的指针,该结构包含指向无法正常工作的类数组的指针

[英]C++ — Pointer to array of structs containing a pointer to an array of classes not working

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: 相反,我的ptr变量似乎出现错误,如下所示:

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. 在解决此问题的尝试中,我注意到在增加ptr并显示代理商名称时,它首先打印了Hertz,然后打印了Alamo,然后打印了89502(与下一个名称相反),然后出现乱码。 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. 显然,从输入文件(第二个FYI,类似于显示格式)中保存信息给第二个和第三个代理商存在问题。 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. C与库存数组的大小有关,b与邮政编码数组的大小有关,而a与我要保存和显示的3个代理商有关。

You need to reset b and c each time through the outer loop in both routines. 在这两个例程中,您每次都需要通过外循环来重置bc 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. (在输入和输出例程中)发生的情况是,在第一次通过外部( a )循环之后, bc的值为5。

You can solve the problem by moving the initialization of b and c into the while loop. 您可以通过将bc的初始化移入while循环来解决该问题。

while(a < 3)
{
   int b = 0, c = 0;
   // ... rest of code ...
}

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? B' c? C?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM