So, as my sig says I am working my way through C++ Primer Plus and I'm stuck on one of the programming examples. It's Chapter 5 exercise 6 for anyone with a copy handy.
The task is this:
I've got it pretty much done, except I can't work out how to get it to accept more than one word for the 'make'. Using standard cin I can get the program to work as long as the make is one word. I've tried cin, cin.get, cin.getline and various combinations which either give me a compile error or unexpected results.6. 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. Note that this requires some care because 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
Can someone give me shove in the right direction as I've been stuck on this for two days and I'm sick of reading the same things on google.
Here's my code so far (I'm obviously missing something):
Thanks all.Code:#include <iostream> struct car { char make[20]; int year; }; int main() { using namespace std; int size; cout << "Enter how many vehicles you wish to add:\n"; cin >> size; car * carReport = new car [size]; for (int i = 0; i < size; i++) { cout << "Car #" << i + 1 << endl; cout << "Enter car make:"; *** -- THIS IS WHERE IT ALL GOES WRONG, THIS IS THE LAST CODE I TRIED BEFORE POSTING HERE --*** getline(cin, carReport[i].make, 20); cout << "Enter year of manufacture: \n"; cin >> carReport[i].year; } //output array info cout << "Here is your collection:\n"; for (int i = 0; i < size; i++) { cout << "Car #" << i + 1 << endl; cout << carReport[i].year << " " << carReport[i].make << endl; } delete [] carReport; return 0; }