string input to char array

This is a discussion on string input to char array within the C++ Programming forums, part of the General Programming Boards category; So, as my sig says I am working my way through C++ Primer Plus and I'm stuck on one of ...

  1. #1
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89

    string input to char array

    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:
    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
    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.

    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):
    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;
    }
    Thanks all.
    Any help I give may be classified as:
    The Blind leading the Blind...
    Currently working through:
    "C++ Primer Plus"

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    1) It should be cin.getline instead of getline. getline - C++ Reference

    cin>> and cin.getline don't play nice together.

    2)You should clear the input buffer after the first cin>> because there is a trailing newline sitting in the input buffer that the getline call picks up. Or you can always use getlines and parse the results to the desired data types using stringstreams.
    Woop?

  3. #3
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Thanks for your reply.

    I put the clear in after the first cin.

    Now, to get the program to behave I had to use the following code:
    Code:
    cin.get();
    cin.getline(carReport[i].make, 20);
    Is that the way it's supposed to be as just using cin.getline(carReport[i].make, 20); made the program run into the next input and messing it up.
    Any help I give may be classified as:
    The Blind leading the Blind...
    Currently working through:
    "C++ Primer Plus"

  4. #4
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    It believe it should look like the following:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    struct car
    {
        char make[20];
        int year;
    };
    int main()
    {
       int size;
    
        cout << "Enter how many vehicles you wish to add:\n";
        cin >> size;
        cin.ignore();
    	
    
        car * carReport = new car [size];
    
        for (int i = 0; i < size; i++)
            {
                cout << "Car #" << i + 1 << endl;
                cout << "Enter car make:";
    			
                cin.getline(carReport[i].make, 20);
    			
                cout << "Enter year of manufacture: \n";
                cin >> carReport[i].year;
                cin.ignore();
            }
        //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;
    }
    I think cin.get() will work instead of cin.ignore(), but I think it's better to use cin.ignore() since it's purpose is to discard characters in the input.


    prog-bman:"You should clear the input buffer after the first cin>> because there is a trailing newline sitting in the input buffer that the getline call picks up"

    How come there is a trailing newline character that getline picks up, yet you can do consecutive cin >> statements without that problem (don't have to use .ignore)? Ex:

    Code:
    cout << "Enter test1: " << endl;
    cin >> test1;
    cout << "Enter test2: " << endl;
    cin >> test2;
    cout << "Enter test3: " << endl;
    cin >> test3;
    Just curious, thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21