C++ -- 指向结构数组的指针,该数组包含指向不工作的类数组的指针

辛克莱

我正在解决一个问题,该问题涉及从文件读取、将信息保存到结构和类的数组中,然后显示信息。为了简洁起见,我省略了一些函数定义。我的代码适用领域如下:

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) {

在输出上,它应该是这样的:

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

相反,我的 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

在我尝试解决这个问题时,我注意到在增加 ptr 并显示代理名称时,它首先打印 Hertz,然后是 Alamo,然后是 89502(与下一个名称相反),然后是乱码。显然,从第二和第三机构的输入文件(仅供参考,类似于显示格式)保存信息存在问题。非常感谢任何帮助。C 与库存数组的大小有关,b 与邮政编码数组的大小有关,a 与我试图保存和显示的 3 个机构有关。

您需要在两个例程中每次通过外部循环重置bc发生的事情是(在输入和输出例程中)b并且c在第一次通过外部 ( a) 循环后的值为 5

您可以通过移动的初始化解决问题bc进入while循环。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章