access or storage problem?

Any ideas why the final for loop is not displaying the values I'm trying to store at the respective locations? And as always any other feedback on how to improve is quite welcome.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// C++ Primer Plus-- Chapter 5, Programming Exercise 7

#include<iostream>

using namespace std;

struct car
{
       char make[30];
       int model;
};

int main()
{
    cout << "How many cars do you wish to catalog? ";
    
    int logs;
    cin >> logs;
    cin.get();
    
    car *CaR = new car[logs];
    
    int count;
    
    for (count = 0; count <= (logs - 1); count++)
    {
        cout << "\nCar #" << count + 1 << " :\n\nPlease enter the make: ";
        
        cin.getline(CaR[logs].make, 30);  
        cout << "\nPlease enter the year made: ";
        cin >> CaR[logs].model;
        cin.get();
    }
    
    cout << "\nHere is your collection:\n";
   
    for (count = 0; count < logs; count++)
        cout << CaR[count].model << " " << CaR[count].make << endl;
       // THE ABOVE OUTPUTS ARE DISPLAYING GARBAGE.
    
    
    delete [] CaR;
    cin.get();
    return 0;
}
You're saving all the data at the same (off the end of your array) location CaR[logs], overwriting the previous data each time, and then trying to read is sequentially from locations CaR[count].
Thank you for finding the mistake, I was so focused on the last loop I didn't catch that I usedCaR[logs]when I meant CaR[count] . I'm glad I found these forums. Thanks, again Moschops. Now I can laugh at myself and go on to exercise 8 at last.
Topic archived. No new replies allowed.