Hi, I am new to c++ and programming. I was solving the following question:
Design a structure called car that holds the following information about an automobile: its make, as a string in a character array or in a string object, and the year it was built, as an integer. Write a program that asks the user how many cars to catalog. The program should then 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. 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
Here's my code for the problem:
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
|
// car.cpp -- creating a car structure to hold info about automobile
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct car
{
string make;
int year;
};
int main()
{
int num;
cout << "How many cars do you wish to catalog? ";
cin >> num;
car *catalog = new car[num];
for (int i = 0; i < num; ++i) {
cout << "Please enter the make: " << endl;
getline(cin, catalog[i].make);
cout << "Please enter the year made: " << endl;
(cin >> catalog[i].year).get();
}
for (int i = 0; i < num; ++i) {
cout << catalog[i].year << " " << catalog[i].make << endl;
}
delete [] catalog;
return 0;
}
|
Here's the output:
How many cars do you wish to catalog? 2
Please enter the make:
Please enter the year made:
Hudson Hornet
Please enter the make:
Please enter the year made:
0
0
I think there's something wrong with the input statements. I don't know what exactly.
Sorry for the long post. Thanks in advance for the help.