Arrays with structure

I'm studying for my finals that's at the end of the month. There is a question that goes:

Construct a C++ program that interactively accepts the data below into an array of five structures.

Car number------------Miles Driven------------Gallons used
25--------------------1450--------------------62
36--------------------3240--------------------136
44--------------------1792--------------------76
52--------------------2360--------------------105
68--------------------2114--------------------67

so far, I've come up with this, but with an operand error (which I do not understand):
#include <iostream>
#include <iomanip>

using namespace std;

const int SIZE=5;

struct CarInfo
{
int CarNum, Miles, Gallons;
};

int main()
{
int i;
CarInfo details[SIZE]={};

cout << "Car Number" << setw(15) << "Miles Driven" << setw(15) <<
"Gallons used" << endl;
cout << "----------" << setw(15) << "------------" << setw(15) <<
"------------" << endl;

for (i=0; i < SIZE; i++)
{
CarInfo details;
cout << "Enter the car number: " << endl;
cin >> details[i].CarNum;
cout << "Enter the miles driven :" << endl;
cin >> details[i].Miles;
cout << "Enter the gallons of fuel used" << endl;
cin >> details[i].Gallons;
}

cout << details[i].CarNum << setw(15) << details[i].Miles << setw(15)
<< details[i].Gallons << endl;

return 0;
}

Can you guide me?
You have details defined at line 8 and again at line 25. Remove the definition at line 25.

When posting code, please use code tags. Highlight your code and press the <> button to the right of the edit window. That way you see what line number we're referring to.
1
2
3
CarInfo details; // THIS LINE MAKES NO SENSE. WHY IS IT HERE?
cout << "Enter the car number: " << endl;
cin >> details[i].CarNum;



Also,
1
2
cout << details[i].CarNum << setw(15) << details[i].Miles << setw(15)
<< details[i].Gallons << endl;
i is 5 at this point. That's really bad.
Last edited on
Topic archived. No new replies allowed.