簡體   English   中英

C++中std::cin object的規則是什么?

[英]What are the rules of the std::cin object in C++?

我正在編寫一個供我個人使用的小程序來練習學習 C++ 及其功能,一個 MLA 引文生成器(我正在寫一篇有數十次引文的大型論文)。

由於缺乏更好的方法(我不理解類或在你的 main 中使用 other.cpp 文件,所以不要費心告訴我,當我有更多時間時我會努力),我正在寫每種引用類型的 function。 如果我有更多時間,我可能會將每個重用代碼分解為 function。

我的問題是:std::cin object 是如何工作的? 我目前正在使用 std::cin >> 讀取我希望是單個單詞的字符串,並使用 getline(std::cin, string) 讀取帶有空格的字符串。 不過,我沒有得到正確的 output。 我只想知道 std::cin 是如何工作的,以及為什么我總是意外地跳過一些輸入(例如,它跳過了 webPage 而不是讓我有機會輸入它)。

void webCit() {
    std::cout << "Leave any unknowns blank.\n";

    std::cout << "Author last name: ";
    std::string lastName;
    std::cin >> lastName;

    if (lastName.size() != 0) {
        lastName = lastName + ", ";
    }


    std::cout << "Author first name: ";
    std::string firstName;
    std::cin >> firstName;

    if (firstName.size() != 0) {
        firstName = firstName + ". ";
    }


    std::cout << "Article title: ";
    std::string articleTitle;
    getline(std::cin, articleTitle);

    if (articleTitle.size() != 0) {
        articleTitle = "\"" + articleTitle + ".\" ";
    }

    std::cout << "Title of web page: ";
    std::string pageTitle;
    std::cin >> pageTitle;

    if (pageTitle.size() != 0) {
        pageTitle = pageTitle + ". ";
    }

    std::cout << "Publication date: ";
    std::string publicationDate;
    getline(std::cin, publicationDate);

    if (publicationDate.size() != 0) {
        publicationDate = publicationDate + ". ";

    }

    std::cout << "Web address: ";
    std::string webAddress;
    getline(std::cin, webAddress);

    webAddress = "<" + webAddress + ">. ";

    std::cout << "Date accessed: ";
    std::string dateAccessed;
    getline(std::cin, dateAccessed);

    if (dateAccessed.size() != 0) {
        dateAccessed = dateAccessed + ". ";
    }

    std::string citation =
        lastName + firstName + articleTitle + pageTitle + publicationDate + webAddress + dateAccessed;

    std::cout << citation; //TEST; remove after
}

編輯:I/O

Leave any unknowns blank.
Author last name: Doe
Author first name: John
Article title: Title of web page: title
Publication date: Web address: www.win.com
Date accessed: 4/29/09
Doe, John. Title. <www.win.com>. 4/29/09. 

如您所見,出了點問題,因為我的輸入被跳過了。

這里發生的是std::cin >> firstName; 只讀取但不包括第一個空白字符,當您按下回車時,它包括換行符(或'\n' ),所以當它到達getline(std::cin, articleTitle); , '\n'仍然是std::cin中的下一個字符,並且getline()立即返回。

// cin = "Bloggs\nJoe\nMan of Steel, Woman of Kleenex\n"
std::cin >> lastName;
std::cin >> firstName;
// firstName = "Joe", lastName = "Bloggs", cin = "\nMan of Steel, Woman of Kleenex\n"
getline(std::cin, articleTitle);
// articleTitle = "", cin = "Man of Steel, Woman of Kleenex\n"

在調用 getline() 之前添加 ' std::cin >> std::ws wws意味着我們s步伐)可以解決問題:

std::cin >> firstName >> std::ws;
getline(std::cin, articleTitle);

但是如果你在論證中這樣做,更容易看出你錯過了哪里:

std::cin >> firstName;
getline(std::cin >> std::ws, articleTitle);

當您使用>>運算符時, cin會向上讀取直到下一個空格字符,但它不會處理空格。 所以當你有

std::cin >> str1;
std::getline(std::cin, str2);

第二個調用將只處理換行符,您將沒有機會輸入任何輸入。

相反,如果您打算在operator >>之后使用getline ,則可以在調用getline之前調用std::cin.ignore()來吃掉換行符。

編輯:當你這樣做時,它會像你預期的那樣工作

std::cin >> str1;
std::cin >> str2;

因為第二次調用將忽略所有前導空格。

暫無
暫無

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

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