简体   繁体   中英

cin.getline() is removing the first letting of my input in C++

When I use this following code, the first string input is displayed correctly, but every string input afterwards is missing the first letter.

 #include <iostream>
 #include <iomanip>
 #include <string>
 using namespace std;

 int main()
 {
    //declare arrays
    string cd_Name[20] = {""};
    string cd_Artist[20]= {""};

    //declare variables
    int numCD = 0;

    cout << "Enter number of CD's: ";
    cin >> numCD;
    if (numCD <= 20)
    {   
        for (int x = 0; x < numCD; x++)
        {
            cout << "Enter name of CD " << x + 1 << ": ";
            cin >> cd_Name[x];
            cin.ignore();
            getline(cin, cd_Name[x]);
            cout << "Enter the artist of " << cd_Name[x] << ": ";
            cin.get();
            getline(cin, cd_Artist[x]);
            cout << endl;
        }//end of for loop

        cout << "CD Names                         Artists" << endl;
        cout << "========                         =========" << endl;

        for (int x = 0; x < numCD; x++)
        {
            cout << cd_Name[x] <<"                "<< cd_Artist[x] << endl;
        }//end of for loop
    }
    else    
        cout << "You can only enter a Max of 20 CD's" << endl;
    //end of if

    system ("pause");
    return 0;
 }//end of main function

An example of the output is shown below:

Enter the number of CD's: 3
Enter name of CD 1: The Battle of Los Angeles
Enter the artist of The Battle of Los Angeles: Rage Against the Machine

Enter name of CD 2: So Far So Good
Enter the artist of o Far So Good: Brian Adams

Enter name of CD 3: Amarte es un Placer
Enter the artist of marte es un Placer: Luis Miguel

CD Names                              Artists
=========                            ==========
The Battle of Los Angeles            ise Against the Machine
o Far So Good                        rian Adams
marte es un Placer                   uis Miguel

I believe you will find this is caused by the cin.ignore() call.

the .ignore() of the istream base class functions by

Extract[ing] characters from the input sequence and discard[ing] them

The default value is to discard One (1) character.

In short,

    cin >> cd_Name[x];
    cin.ignore();
    getline(cin, cd_Name[x]);

Will get the name inputted by the user, Ignore the first character, then extract the string to your array.

If you called cin.ignore(2) it would remove the first two characters, and so on.

Please view this article for more information (http://www.cplusplus.com/reference/istream/istream/ignore/)

[EDIT] You may find your getline and ignore calls are arbitrary try using this in your loop: cin will deliver the resulting string directly into the variable given, you should not need getline, nor ignore to achieve this.

    char Name_tmp[50];
    char Artist_tmp[50];
    for (int x = 0; x < numCD; x++)
    {
        cout << "Enter name of CD " << x + 1 << ": ";
        cin.get(Name_tmp, 50); //get 50 character (max length of string) into string variable
        cout << "Enter the artist of " << cd_Name[x] << ": ";
        cin.get(Artist_tmp, 50); //get 20 character (max length of string) into string variable
        cout << endl;
        cd_Name[x] = Name_tmp;
        cd_Artist[x] = Artist_tmp;
    }//end of for loop

In short, I'm extracting a set number of characters (less can be taken, but no more) from the users input into a temporary C-string, then assigning those values into your array.

Note, I made use of the istream::get method.

[Edit 2] I noticed David Schwartz's post as to the implementation of using

system ("pause");

Unfortunately, I believe David is an avid linux programmer, and is not familiar with Visual Studio - When a program in run from VS (unless done so without debugging) the program output window closes before you can view the results. This may also occur with some other IDE's.

My recommendation is to use something like this instead

char tmp[20];
cin.get(tmp, 20);
return;

This will pause and wait for the user to provide some input (a return should count as input in the case of MS Visual Studio)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM