I'm trying to do some of my C++ homework at the moment and I've run into a problem. Essentially, the program is supposed to have a structure of type car that has a make and year. It's supposed to make a dynamic array of that structure and let the user input the make and year of each of the cars that they catalog. It then displays their "collection". Here is what the output is supposed to look like after a user has input their information:
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
The problem is that with my current code, if I try and run it, it neglects to give the user a chance to input anything, and I don't know why.
#include <iostream>
//#include <string>
usingnamespace std;
int main()
{
struct car
{
char make[20];
int year;
};
int numCars;
int x = 1;
cout << "How many cars do you wish to catalog? ";
cin >> numCars;
cout << endl;
car * pt = new car[numCars];
for (int i = 0; i < numCars; i++)
{
x += i;
cout << "Car #" << x << ":" << endl;
cout << "Please enter the make: ";
cin.get(pt[i].make, 20);
cout << "Please enter the year made: ";
cin >> pt[i].year;
cout << endl;
}
cout << "Here is your collection: " << endl;
for (int z = 0; z < numCars; z++)
{
cout << pt[z].year << " " << pt[z].make << endl;
}
return 0;
}
Oh yeah. I forgot to mention that the make has to be able to contain one or more words. That works fine with one word, but if you try more than that then it doesn't. :(
Oh yeah. I forgot to mention that the make has to be able to contain one or more words. That works fine with one word, but if you try more than that then it doesn't. :(
You have to replace ALL instances of cin >> var. Using the >> operator will not remove the newline character from the stream. Thus cause your problems. >> also won't handle multiple worlds with spaces on a single line.
But that's not the problem. The chapter my class is on in the book has not explained if-then statements and such yet. The program that it wants me to write expects that the user will input the correct type of data, thus we will not encounter that kind of a problem.
The problem that I am experiencing is different than the one outlined in that article. For some reason my program will neglect to give the user a chance to enter any input about the information for the cars. Copy the code into Visual Studio and try it and you'll see what I'm talking about.
If I try to use cin.getline() it just neglects to let the user enter input about the make of the car, and lets them only enter the year that the car was made.
Using the >> operator will not remove the newline character from the stream.
.
So after you enter the numcars it keeps the newline character in the stream. After you prompt to enter the make, that newline is used to submit a blank make for the car. And the cycle continues after you enter the year.
After each line of accepting input via cin >> var; you will need to add std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); for cin to ignore all newline characters. This still won't fix the multiple word problem though.