Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Need hints on error (dynamic structure)

This topic is 5275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm doing an exercise in my book and I can't figure it out. I want to solve it on my own, but I'm just clueless. I know it has to do with creating a dynamic structure, and I can't decipher the problem by looking back in my book. Heres the code:
#include <iostream>            // initialize the namespace for using cin, cout and such

using namespace std;

struct car {         // the car structure

char make[30];   // make of the car, 30 characters for now (might try to make dynamic)

int year;        // the cars year

};

int main()
{
int catalog_Cars;                                      // The number of cars to catalog

cout << "How many cars do you wish to catalog? ";
cin >> catalog_Cars;                                   // -User tells the program how many

car * carInfo = new car[catalog_Cars];               /* attempt to create a dynamic structure
that is only so many structures long */
for (int cars=1; cars <= catalog_Cars; cars++)       // Create a loop for these structures

{
cout << "Car #" << cars << ":\n";
cout << "Please enter the make: ";
cin >> carInfo[cars-1].make;
cin >> carInfo[cars-1].year;
}
cout << "Here is your collection: \n";
for (int cars=1; cars <= catalog_Cars; cars++)           // Attemps to print the catalog of cars

{
cout << carInfo[cars-1].year << " " << carInfo[cars-1].make << "\n";
}
delete [] carInfo;
system("pause"); // keeps the program from just quitting when it finishes so I can see it

return 0;
}

I'm aware I also do not delete the dynamic array, but I think that just goes at the end. I'd appreciate someone pointing me to the problem area, or helping me narrow down what I need to look at to fix it. I don't want it fixed for me, not to be picky or anything. I just want to learn this...but am not sure where things went wrong. Maybe everywhere... [edited by - TheSimplerOne on June 6, 2004 5:11:02 PM] [edited by - TheSimplerOne on June 6, 2004 5:30:17 PM] [edited by - TheSimplerOne on June 6, 2004 8:48:11 PM]

##### Share on other sites
Not sure where the problem is. I am not that familiar with cout and cin, probably lies in cin.get(), but I don't know.

Anyway, your loop is very awkward. Usually people would start from 0 to n-1 when iterating through an array. But you start from 1 to n, but add -1 to every one of it. While it is technically ok, it just seems so disturbing. I would do it like this:
for ( int cars = 0; cars < catalog_Cars; ++cars ){}
edit: duh. now I see it.
cin.get(carInfo[cars-1]->make, 30);cin >> carInfo[cars-1]->year;
See that operator (->) ? That is used when using pointers. carInfo is a pointer, but carInfo[n] is not. So what operator is used when addressing members of a non-pointer variable?

[edited by - alnite on June 6, 2004 6:06:13 PM]

##### Share on other sites
Yea, I noticed that that might be the problem, so I changes the ->''s to .''s and it worked, but it just displayed garbage. You explained HOW to think about it and why it works now. And you provided the last piece of the puzzle on how to fix it. Thankyou so very much. That''s a pretty cool little program now, and it makes sense. Gotta remember when it is pointing to an element of an array and when it is pointing to a pointer.

Now...the only problem really left, what about car models that have spaces in the names? If I don''t use cin.getline then it will only take the first part of a two word make for the car. If I use cin.getline I either fubar it so it just creates an automatic mess with a run-time error or I make an infinite loop if I try using
cin.getline(carInfo[cars-1].make, 30).get();

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011321
• ### Who's Online (See full list)

There are no registered users currently online

×