First let me start out that saying Pointers are probly my least favorite part of programming. I can get pointers to work with single variables and I think I can as well for single structures, when it comes to pointers and arrays I understand slightly but not fully.
Now when it comes to making a pointer to an Array of structures I feel dumbfounded and have been struggling over this for a few days.
I am trying to make a pointer to the array of Structures and using the pointer send the array to the display function showCarInfo().
I have looked through just about every book I can at my college library and none of them have any information on using Pointers and Structure Arrays. I know that people will not do my homework for me here and I have what i've been trying posted below, this is part of my final Exam and any help would be appreciated.
#include <cstring>
#include <iomanip>
#include <iostream>
using namespace std;
const int SIZE = 3;
struct CarInfo
{
string carMake;
string carModel;
int carYear;
string carColor;
};
void getCarInfo(CarInfo [], int);
void showCarInfo(CarInfo [], int);
int main()
{
CarInfo cars[SIZE], *carsPtr;
carsPtr = cars;
getCarInfo(cars, SIZE);
showCarInfo(cars, SIZE);
system("PAUSE");
return 0;
}
void getCarInfo(CarInfo cars[], int SIZE)
{
for ( int i = 0 ; i < SIZE ; i++ )
{
cout << "Enter the Car Make: ";
cin >> cars[i].carMake;
cout << "Enter the Car Model: ";
cin >> cars[i].carModel;
cout << "Enter the Car Year: ";
cin >> cars[i].carYear;
cout << "Enter the Car Color: ";
cin >> cars[i].carColor;
cout << endl;
}
}
void showCarInfo(CarInfo cars[], int SIZE)
{
cout << "Car #:" << setw(10) << "Make"
<< setw(10) << "Model"
<< setw(10) << "Year"
<< setw(10) << "Color"
<< endl;
cout << "----------------------------------------------" << endl;
for ( int j = 0 ; j < SIZE ; j++)
{
cout << "Car " << (j + 1) << ":" << setw(10) << cars[j].carMake
<< setw(10) << cars[j].carModel
<< setw(10) << cars[j].carYear
<< setw(10) << cars[j].carColor
<< endl;
}
}
If what I understand is correct I have properly assigned carsPtr the memory address of cars[0]. However whenever I try to display anything ,after having recieved the input, using the code:
cout << *(carsPtr+1).carMake;
I get the following errors:
H:\COMS-171 Winter\Projects\FE1.cpp In function `int main()':
27 H:\COMS-171 Winter\Projects\FE1.cpp `carMake' has not been declared
27 H:\COMS-171 Winter\Projects\FE1.cpp request for member of non-aggregate type before ';' token
My book did not have more than a few sentences on Pointers to Arrays of Structures and using multiple books this is the best I have been able to come up with so far. If I can just get the pointer to work I'm fairly certain I can get it to the function with ease.