C ++中std :: cin对象的规则是什么?

问题描述 投票:0回答:2

[我正在编写一个供个人使用的小程序,用于练习学习C ++及其功能,它是MLA引用生成器(我正在写带有数十篇引用的大论文)。

由于缺乏更好的方法(我不理解类或在您的主程序中使用其他.cpp文件,所以不要打扰告诉我,我会在有更多时间的时候进行处理),正在为每种引用类型编写函数。如果我有更多时间,我可能会为每个重用的代码将其分解为一个函数。

我的问题是:std :: cin对象如何工作?我目前正在使用std :: cin >>读取我希望是单个单词的字符串,并使用getline(std :: cin,string)读取带有空格的字符串。但是,我没有得到正确的输出。我只想知道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: Hooked
Author first name: Jerbear
Article title: Title of web page: title
Publication date: Web address: www.win.com
Date accessed: 4/29/09
Hooked, Jerbear. Title. <www.win.com>. 4/29/09. 

您可以看到,出了点问题,因为我的输入被跳过了。

c++ iostream cin
2个回答
7
投票

这里发生的是,当您按Enter键时,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”(ws表示w hitespace)解决了问题:

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

但是如果在参数中这样做,更容易看到您错过了它:

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

3
投票

[当使用>>运算符时,cin会读取直到下一个空格字符,但不会处理空格。所以当你有

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

第二个调用将只处理换行符,您将没有机会键入任何输入。

相反,如果您打算在getline之后使用operator >>,则可以在致电std::cin.ignore()之前致电getline吃换行符。

编辑:它按您的预期工作

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

因为第二个调用将忽略所有前导空白。

© www.soinside.com 2019 - 2024. All rights reserved.