d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Problem With Getline() Delimiter > Program Skips Over Getline() Input
Add Reply New Topic New Poll
Member
Posts: 1,522
Joined: Feb 21 2004
Gold: 164.00
May 3 2012 01:38pm
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
Member
Posts: 1,522
Joined: Feb 21 2004
Gold: 164.00
May 20 2012 01:01pm
I've encountered this problem a few more times doing similar problems as well as file IO. Lately, I've been getting around the problem by clearing the input stream with this line of code:
Code
cin.ignore(numeric_limits<streamsize>::max(), '\n');

It seems like I run into this problem when I am using the >> operator for input which makes me think the operator is not reading the newline character and just leaves it in the buffer. I thought I could just do the following:
Code

char temp;
cin >> temp;

This gets rid of the newline character but also reads the next character from either the keyboard or the data file. Can anyone explain what the >> operator is doing or point me to a website where I can find out?

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll