簡體   English   中英

cin.getline()刪除了我在C ++中輸入的第一個字母

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

當我使用下面的代碼時,第一個字符串輸入正確顯示,但是此后的每個字符串輸入都缺少第一個字母。

 #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

輸出示例如下所示:

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

我相信您會發現這是由cin.ignore()調用引起的。

istream基類的.ignore()函數通過

從輸入序列中提取字符並丟棄它們

默認值為丟棄一(1)個字符。

簡而言之,

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

將獲得用戶輸入的名稱,忽略第一個字符,然后將字符串提取到數組中。

如果調用cin.ignore(2),它將刪除前兩個字符,依此類推。

請查看本文以獲取更多信息(http://www.cplusplus.com/reference/istream/istream/ignore/)

[編輯]您可能會發現自己的getline和ignore調用是任意的,請在循環中嘗試使用此命令:cin將結果字符串直接傳遞到給定的變量中,您既不需要getline,也無需忽略即可實現這一點。

    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

簡而言之,我正在從輸入到臨時C字符串的用戶中提取出一定數量的字符(可以容納的字符數更少,但不能容納更多),然后將這些值分配給數組。

注意,我使用了istream :: get方法。

[編輯2]我注意到David Schwartz的帖子中關於使用的實現

system ("pause");

不幸的是,我相信David是一個狂熱的Linux程序員,並且對Visual Studio並不熟悉-當從VS運行程序時(除非在未調試的情況下這樣做),程序輸出窗口將關閉,然后您才能查看結果。 其他一些IDE也可能發生這種情況。

我的建議是改用這樣的東西

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

這將暫停並等待用戶提供一些輸入(對於MS Visual Studio,返回應計為輸入)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM