I'm in the process of refreshing the basic C++ things that I learned and expanding on that prior knowledge by reading C++ Primer Plus by Stephen Prata. I'm working on one of the exercises from chapter 5 which is shown below:
Chapter 5 Exercise 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 Hronet
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
This is obviously an easy program to write and the code that I have is as follows:
Code
#include <iostream>
#include <string>
struct car // Define the car structure
{
std::string make; // Using the standard string definition
// Rather than doing a global "using namespace std"
int year;
};
int main()
{
using namespace std;
char delim = '#'; // Delimiting character for getline()
// This was done because the default new line delimiter
// was causing the getline() function to never prompt the
// user for input.
int numCars; // Number of cars to catalog
// Prompt user for input
cout << "How many cars do you wish to catalog? ";
cin >> numCars;
car * carCatalog = new car[numCars]; // Allocate storage for the catalog
char temp[30];
// Loop through to prompt user for information on each car to catalog
for (int i = 0; i < numCars; i++)
{
cout << "Car #" << (i + 1) << endl;
cout << "Please enter the make: ";
getline(cin, carCatalog[i].make, delim); // Store vehicle make
cout << "\nPlease enter the year made: ";
cin >> carCatalog[i].year; // Store vehicle year
}
// Display the users car collection
cout << "Here is your collection: ";
for (int i = 0; i < numCars; i++)
cout << endl << carCatalog[i].year << " " << carCatalog[i].make;
return 0;
}
I'm sure there are probably better ways to do this, but these are the methods that I have read about so far. The problem is that I was originally using the getline() function without specifying a delimiter and my program would never prompt the user for the vehicle make. It would skip that line each time through the loop and ask the user for the year the vehicle was made. After googling the getline() function I saw that I could change the delimiter and decided to give that shot. Once I changed the delimiter to # instead of a new line the code runs fine but I don't understand why there are new lines in the input stream that caused my original problem. Can anyone explain why that would happen with the code I've shown above?
Also, is it possible to monitor what is in the input stream from a debugger? I'm currently using Code::Blocks but if someone has a better IDE I'd be willing to at least try it out!
Thanks for your help.
This post was edited by LegendaryLemon on May 3 2012 01:39pm