C ++中std :: cin对象的规则是什么? [英] What are the rules of the std::cin object in C++?
问题描述
我正在写一个小程序,用于我个人使用实践学习C ++和其功能,一个MLA引文生成器(我正在写一个大型文件与几十个引文)。
I am writing a small program for my personal use to practice learning C++ and for its functionality, an MLA citation generator (I'm writing a large paper with tens of citations).
由于缺乏一个更好的方法来做(我不明白类或使用其他.cpp文件在你的主,所以不要打扰告诉我,我当我有更多的时间,我会工作),我为每种类型的引文写一个功能。如果我得到更多的时间,我可能会把它分解为每个重用代码的函数。
For lack of a better way to do it (I don't understand classes or using other .cpp files inside your main, so don't bother telling me, I'll work on that when I have more time), I am writing a function for each type of citation. I might break this down into a function for each reused code if I get more time.
我的问题是:std :: cin对象如何工作?我目前正在使用std :: cin >>读取字符串,我希望是单个单词,getline(std :: cin,string)用于带空格的字符串。我没有得到正确的输出,虽然。我只想知道std :: cin的工作原理,以及为什么我意外地跳过一些输入(例如,它跳过webPage,而不是给我一个机会输入它)。
My question is: how does the std::cin object work? I am currently reading in with std::cin >> for the strings I expect to be single words, and getline(std::cin, string) for the strings with spaces. I am not getting the right output, though. I just want to know how std::cin works and why I keep unexpectedly skipping over some some inputs (for instance, it skips over webPage instead of giving me a chance to input into it).
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.
正如你所看到的,出现了问题,因为我的输入被跳过了。 >
As you can see, something is going wrong, because my input is getting skipped over.
推荐答案
这里发生的是 std :: cin> firstName;
只读取但不包括第一个空白字符,其中包括换行符(或'\\\
getline(std :: cin,articleTitle); ,
''\\\
' code>仍然是 std :: cin
中的下一个字符,而 getline()
会立即返回。 / p>
What is happening here is that std::cin >> firstName;
only reads up to but not including the first whitespace character, which includes the newline (or '\n'
) when you press enter, so when it gets to getline(std::cin, articleTitle);
, '\n'
is still the next character in std::cin
, and getline()
returns immediately.
// 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"
$ b b
添加 std :: cin>> std :: ws
'( ws
意思 w
hite s
Adding 'std::cin >> std::ws
' (ws
meaning w
hites
pace) before your calls to getline() fixes the problem:
std::cin >> firstName >> std::ws;
getline(std::cin, articleTitle);
但是,如果你在参数中做到这一点, >
But it is easier to see where you missed it if you do it in the argument:
std::cin >> firstName;
getline(std::cin >> std::ws, articleTitle);
这篇关于C ++中std :: cin对象的规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!