newbie needs help
-
Hello, I'm currently trying to learn c++ and stuck on an exercise from my book. I don't have an answer guide, nor can I find the answer online. Here is the exercise: Design a structure called car that holds the following information about an automobile: its make as a string in a character array and the year it was built as an integer. Write a program that asks the user how many cars to catalog. The program then should use new to create a dynamic array of that many car structures. Next, it should prompt the user to input the make (which might consist of more than one word) and year information for each structure. Note that this requires some care, for it alternates reading strings with numeric data (see Chapter 4). Finally, it should display the contents of each structure. A sample run should look something like the following: How many cars do you wish to catalog? 2 Car #1: Please enter the make: Hudson Hornet Please enter the year made: 1952 Car #2: Please enter the make: Kaiser Please enter the year made: 1951 Here is your collection: 1952 Hudson Hornet 1951 Kaiser And here is my code: #include using namespace std; struct car { char make[30]; int year; }; int main() { int carsToLog, i; cout << "How many cars do you wish to catalog? "; cin >> carsToLog; car * pNum = new car[carsToLog]; for (i = 0;imake,29); cout << endl << "Please enter the year made: "; cin >> pNum[i]->year; } cout << endl << "Here is your collection: " << endl; for(i=0;iyear << " " << pNum[i]->make << endl; delete[] pNum; return 0; } I am getting the following errors from VS.Net: error C2819: type 'car' does not have an overloaded member 'operator ->' error C2227: left of '->make' must point to class/struct/union. When I take the array index out of the cin lines, it works, but will only access the first index of the dynamic array.. Thanks for the help in advance!
-
Hello, I'm currently trying to learn c++ and stuck on an exercise from my book. I don't have an answer guide, nor can I find the answer online. Here is the exercise: Design a structure called car that holds the following information about an automobile: its make as a string in a character array and the year it was built as an integer. Write a program that asks the user how many cars to catalog. The program then should use new to create a dynamic array of that many car structures. Next, it should prompt the user to input the make (which might consist of more than one word) and year information for each structure. Note that this requires some care, for it alternates reading strings with numeric data (see Chapter 4). Finally, it should display the contents of each structure. A sample run should look something like the following: How many cars do you wish to catalog? 2 Car #1: Please enter the make: Hudson Hornet Please enter the year made: 1952 Car #2: Please enter the make: Kaiser Please enter the year made: 1951 Here is your collection: 1952 Hudson Hornet 1951 Kaiser And here is my code: #include using namespace std; struct car { char make[30]; int year; }; int main() { int carsToLog, i; cout << "How many cars do you wish to catalog? "; cin >> carsToLog; car * pNum = new car[carsToLog]; for (i = 0;imake,29); cout << endl << "Please enter the year made: "; cin >> pNum[i]->year; } cout << endl << "Here is your collection: " << endl; for(i=0;iyear << " " << pNum[i]->make << endl; delete[] pNum; return 0; } I am getting the following errors from VS.Net: error C2819: type 'car' does not have an overloaded member 'operator ->' error C2227: left of '->make' must point to class/struct/union. When I take the array index out of the cin lines, it works, but will only access the first index of the dynamic array.. Thanks for the help in advance!
You have a pointer named pNum. That can be used with the '->' operator, and also the indexing operator '[]'. Either of pNum->make or pNum[0].make refer to the same object. When you use the index operation, you don't have a pointer any more, you have an indexed instance of the thing the pointer points to. So you can say: pNum[i].make or even &pNum[i]->make but the latter is just perverse :) Hope this helps, and welcome to the wonderful world of C++ Steve S Developer for hire
-
You have a pointer named pNum. That can be used with the '->' operator, and also the indexing operator '[]'. Either of pNum->make or pNum[0].make refer to the same object. When you use the index operation, you don't have a pointer any more, you have an indexed instance of the thing the pointer points to. So you can say: pNum[i].make or even &pNum[i]->make but the latter is just perverse :) Hope this helps, and welcome to the wonderful world of C++ Steve S Developer for hire
-
Steve S wrote: &pNum[i]->make but the latter is just perverse or (pNum + i)->make ?! :-D
TOXCCT >>> GEII power
This is a student project, not the obfiscated C++ coding contest. Any student who turns in either of those two perverse options had better have a good explination as to why they did that, and not the simple pNum[i].make! Otherwise they will lose points. I'm sure there is a good reason to use either of those forms. I can't think of one offhand though.
-
This is a student project, not the obfiscated C++ coding contest. Any student who turns in either of those two perverse options had better have a good explination as to why they did that, and not the simple pNum[i].make! Otherwise they will lose points. I'm sure there is a good reason to use either of those forms. I can't think of one offhand though.
-
Sick people :wtf:X| . . . teaching pointer arithmetic to beginners.:cool: The question "Do computers think?" is the same as "Can submarines swim?"