Dynamic Array of Structures, Loop problem!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 7
Reputation: binteron is an unknown quantity at this point 
Solved Threads: 0
binteron binteron is offline Offline
Newbie Poster

Dynamic Array of Structures, Loop problem!

 
0
  #1
Jun 24th, 2006
I am new to C++ and working through SAMS C++ primer. one of the excercies involves creating a dynamic array of structures to catalogue car information. I have the following code almost working and I am sure that I am missing something really simple.

The program compiles and runs as expected, however on the first run through the loop, the "make" of the car is not stored in the array. All subsequent loops capture the input and display properly.

Any help or point in the right direction would be appreciated.

thanks.


  1. //c++ primer ch 5 ex 6.
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct car {
  6.  
  7. char make[20];
  8. int year;
  9.  
  10.  
  11. };
  12. int c;
  13. int t = 1;
  14. int main() {
  15. cout <<"How many cars would you like to catalogue? ";
  16. cin >>c;
  17. cin.get();
  18.  
  19. car * catalogue = new car[c];
  20. car *ptr = &catalogue[0];
  21.  
  22. for (int i = 0;i < c; ++i)
  23. {
  24. cout <<"Car# "<<t<<":\n";
  25. cout <<"Enter make of car: ";
  26. cin.getline(ptr[i].make,20);
  27. cout <<"Enter year of car: ";
  28. cin >> ptr[i].year;
  29. cin.get();
  30. cout <<"\n";
  31. ++t;
  32. };
  33. delete [] catalogue;
  34.  
  35. cout <<"Your catalogue of cars: \n";
  36. for (int i = 0; i < c;++i)
  37. { cout <<catalogue[i].year<<" "<<catalogue[i].make<<"\n";
  38. };
  39.  
  40. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Dynamic Array of Structures, Loop problem!

 
0
  #2
Jun 24th, 2006
> car *ptr = &catalogue[0];
What does this achieve?

You can do this
cin.getline(catalogue[i].make,20);

> delete [] catalogue;
You need to do this after you've finished printing the data, not before.

> however on the first run through the loop, the "make" of the car is not stored in the array
Maybe you typed a space after the input of how many cars
So the get() only got rid of the space, not the newline which would immediately satisfy the getline.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7
Reputation: binteron is an unknown quantity at this point 
Solved Threads: 0
binteron binteron is offline Offline
Newbie Poster

Re: Dynamic Array of Structures, Loop problem!

 
0
  #3
Jun 25th, 2006
Hi Salem, Thanks for the quick reply. *ptr is something from a different book, it said pointer was best way to access dynamic arrays.

I have run the program many times and not entered a space after the number of cars, so that's a good thought, but not it. Also I had put a cin.get() after the input to see if there was something there causing the problem.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7
Reputation: binteron is an unknown quantity at this point 
Solved Threads: 0
binteron binteron is offline Offline
Newbie Poster

Re: Dynamic Array of Structures, Loop problem!

 
0
  #4
Jun 25th, 2006
Well, I moved the delete [] catalogue line to the end of the program and now the thing runs perfectly.

Thanks Salem! I new it would be something simple and painfully obvious when you look at it.....duhhh.

best regards
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Dynamic Array of Structures, Loop problem!

 
0
  #5
Jun 25th, 2006
> it said pointer was best way to access dynamic arrays.
It's the only way.

But both variables have the same type.
  1. car * catalogue = new car[c];
  2. car *ptr = &catalogue[0];
All you've actually achieved is changing a 9-letter variable name (with some meaning) for a 3-letter variable name (with very little meaning). ptr[n] and catalogue[n] do exactly the same thing.

What you've also created is a dangling pointer problem. By having two variables pointing at the same memory you introduce the possibility of accessing ptr[n] long after you've done delete [ ] catalogue.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7
Reputation: binteron is an unknown quantity at this point 
Solved Threads: 0
binteron binteron is offline Offline
Newbie Poster

Re: Dynamic Array of Structures, Loop problem!

 
0
  #6
Jun 25th, 2006
I see, so in essence i created an extra pointer, one that wasn't needed. I had wondered about that, I must have misinterpreted the first book i read. Thanks a bunch for taking the time to clear that up form me.

Cheers Salem.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC