簡體   English   中英

cin.getline()奇怪的行為

[英]cin.getline() strange behavior

以下是從一本書中練習一些階級繼承的練習。 但問題出在客戶端,而不是類設計。 (BaseCore,baseDMA,缺少DMA和hasDMA是BTW類)。

// usedma.cpp -- polymorphic example (compile with dma.cpp)

#include <iostream>
#include "dma.h" // includes <iostream>

const int ELEMENTS = 1;
const int LENGTH = 30;

int main()
{
    using std::cin;
    using std::cout;

    BaseCore *pArr[ELEMENTS];
    char tempDate[LENGTH];
    char kind;

    for (int i = 0; i < ELEMENTS; i++)
    {
        cout << "\nEntering data for element #" << i + 1 << "\n\n";
        cout << "Enter the date it was created: ";
        cin.getline(tempDate, LENGTH - 1);
        cout << "Enter 1 for baseDMA, 2 for lacksDMA, or 3 for hasDMA: ";
        while (cin >> kind && kind != '1' && kind != '2' && kind != '3')
            cout <<"Wrong data. Please, try again: ";
        while (cin.get() != '\n')
            continue;
        char tempLabel[LENGTH];
        int tempRating;
        cout << "Enter the label: ";
        cin.getline(tempLabel, LENGTH - 1);
        cout << "Enter the rating: ";
        cin >> tempRating;
        if (kind == '1') // baseDMA
            pArr[i] = new baseDMA(tempDate, tempLabel, tempRating);
        if (kind == '2') // lacksDMA
        {
            char tempColor[LENGTH];
            cout << "Enter the color: ";
            cin.getline(tempColor, LENGTH - 1);
            pArr[i] = new lacksDMA(tempDate, tempLabel, tempColor, tempRating);
        }
        if (kind == '3') // hasDMA
        {
            char tempStyle[LENGTH];
            cout << "Enter the style: ";
            cin.getline(tempStyle, LENGTH - 1);
            pArr[i] = new hasDMA(tempDate, tempLabel, tempStyle, tempRating);
        }
        while (cin.get() != '\n')
            continue;
    }

    cout << "\n";
    for (int i = 0; i < ELEMENTS; i++)
    {
        pArr[i]->View();
        cout << "\n";
    }
    cout << "Done.\n";

    std::cin.get();
    return 0;
}

樣品執行:

輸入元素#1的數據

輸入創建日期:2012.01.01

輸入1表示baseDMA,輸入2表示lacksDMA,輸入3表示hasDMA:2

輸入標簽:lacksDMA

輸入評分:15

輸入顏色:藍色

創作日期:2012.01.01

標簽:缺乏DMA

評分:15

顏色:

完成。

似乎為Color成員分配了空字符。 這種行為發生在if (kind == '2')if (kind == '3')語句中(在這種情況下使用樣式成員)。

如果我放一個cin.get(); 就在cin.getline()之前它工作正常但我必須按一個額外的鍵才能讓程序請求輸入。

為什么會這樣? 如果輸入隊列中有一個'\\ n'掛起,cin.getline()將丟棄它並在變量中放入'\\ 0',我可以理解。 但程序要求我輸入顏色,讓我正常輸入。 另外,如果我放了一個cin.get(),那么程序不應該等待執行中的額外鍵擊,它應該擺脫額外的'\\ n'。 我在這里錯過了什么?

cout << "Enter the rating: ";
        cin >> tempRating;

istream::getline()operator>>在流中留下尾隨\\n 它導致在你的一個if語句中調用getline以獲得空輸入。

當控制流到達for循環結束while (cin.get() != '\\n')語句時,流是空的 - 它正在等待輸入,看起來好像你還在輸入顏色。

cin.ignore()調用cin.ignore()並且它會工作。

請注意,如果您在輸入語句后面放置一個“調試cout”,那么這種錯誤就會很明顯。 你獲得tempRating的方式還有一個問題。 如果輸入無效輸入,例如“xy”,則會在cin上設置錯誤標志,程序將進入無限循環。 始終檢查輸入操作是否成功。

如果我放一個cin.get(); 就在cin.getline()之前它工作正常但我必須按一個額外的鍵才能讓程序請求輸入。

在我看來,當你不放cin.get()時,你的getline會得到一個空字符。 然后,當你輸入cin.get時,你得到那個空字符,你的getline工作正常..

但是你一定要進行調試,看看究竟發生了什么!

暫無
暫無

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

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