| | |
Dynamic Array of Structures, Loop problem!
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2006
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
//c++ primer ch 5 ex 6. #include <iostream> using namespace std; struct car { char make[20]; int year; }; int c; int t = 1; int main() { cout <<"How many cars would you like to catalogue? "; cin >>c; cin.get(); car * catalogue = new car[c]; car *ptr = &catalogue[0]; for (int i = 0;i < c; ++i) { cout <<"Car# "<<t<<":\n"; cout <<"Enter make of car: "; cin.getline(ptr[i].make,20); cout <<"Enter year of car: "; cin >> ptr[i].year; cin.get(); cout <<"\n"; ++t; }; delete [] catalogue; cout <<"Your catalogue of cars: \n"; for (int i = 0; i < c;++i) { cout <<catalogue[i].year<<" "<<catalogue[i].make<<"\n"; }; }
> car *ptr = &catalogue[0];
What does this achieve?
You can do this
> 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.
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.
•
•
Join Date: Jun 2006
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
> it said pointer was best way to access dynamic arrays.
It's the only way.
But both variables have the same type.
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.
It's the only way.
But both variables have the same type.
C++ Syntax (Toggle Plain Text)
car * catalogue = new car[c]; car *ptr = &catalogue[0];
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.
![]() |
Similar Threads
- Creating dynamic array structures (C++)
- Passing array of structures into a function (C++)
- dynamic array of structures problem (C++)
Other Threads in the C++ Forum
- Previous Thread: System modal dialog boxes
- Next Thread: Help, Won't Run
Views: 4459 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






