C++ 基本字符串输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18803871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:13:41  来源:igfitidea点击:

Basic String input

c++stringinputgetlinecin

提问by user2779581

I've just came across this bit of code that allows users to input strings in the command prompt. I'm aware of what they do and it's all great. But I have a question in regards to the cin and getline() functions.

我刚刚遇到了这段代码,它允许用户在命令提示符中输入字符串。我知道他们所做的一切都很棒。但是我有一个关于 cin 和 getline() 函数的问题。

string name ;
cout << "Please enter your full name: " ;
cin >> name ;
cout << "Welcome " << name << endl ;
cout << "Please enter your full name again please: " ;
getline(cin , name) ;
cout << "That's better, thanks " << name << endl ;
return 0 ;

Now when this is output, I get something along the lines of: (using john smith as the input)

现在,当这是输出时,我得到了以下内容:(使用 john smith 作为输入)

Please enter your full name: john smith
Welcome John
Please enter your full name again: That's better thanks Smith

I understand why this happens, the getline is still reading from the input buffer and I know how to fix it. My question is, why is there no newline coming after the "Please enter your full name again: "? When I alter the code to:

我明白为什么会发生这种情况,getline 仍在从输入缓冲区读取数据,我知道如何修复它。我的问题是,为什么在“请再次输入您的全名:”之后没有换行符?当我将代码更改为:

string name ;
cout << "Please enter your full name: " ;
cin >> name ;
cout << "Welcome " << name << endl ;
cout << "Please enter your full name again please: " ;
cin.ignore( 256, '\n') ;
getline(cin , name) ;
cout << "That's better, thanks " << name << endl ;
return 0 ;

Suddenly I get a newline after you enter your full name again. It's not really a huge issue to be honest. But I wouldn't mind knowing what happened if anyone can help me. Thanks!

在你再次输入你的全名后,我突然得到一个换行符。老实说,这并不是一个大问题。但如果有人可以帮助我,我不介意知道发生了什么。谢谢!

采纳答案by Nemanja Boric

You see, when you enter "John Smith" as a input first cin >> namewill not read the entire line, but the the contents of the line until the first space.

你看,当你输入“John Smith”作为输入时,首先cin >> name不会读取整行,而是读取该行的内容,直到第一个空格。

So, after the first cin, name variable will contain John. There will still be Smith\nin the buffer, and you've solved this using:

因此,在第一个cin, name 变量之后将包含John. Smith\n缓冲区中仍然会有,并且您已经使用以下方法解决了这个问题:

cin.ignore( 256, '\n') ;

Note:As Konrad Rudolph suggested, you really shouldn't use 256 or any other magic numbers in your code. Rather use std::numeric_limits<std::streamsize>::max(). Here is what docs says about the first argument to istream::ignore:

注意:正如 Konrad Rudolph 所建议的,您真的不应该在代码中使用 256 或任何其他幻数。而是使用std::numeric_limits<std::streamsize>::max(). 以下是文档对第一个参数的说明istream::ignore

Maximum number of characters to extract (and ignore). If this is exactly numeric_limits<streamsize>::max(), there is no limit: As many characters are extracted as needed until delim (or the end-of-file) is found.

要提取(和忽略)的最大字符数。 如果正好是numeric_limits<streamsize>::max(),则没有限制:根据需要提取尽可能多的字符,直到找到 delim(或文件结尾)。

cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n') ;

My question is, why is there no newline coming after the "Please enter your full name again: "?

我的问题是,为什么在“请再次输入您的全名:”之后没有换行符?

Because you're not outputing one to the stdout, and the user didn't get chance to press Enter. getlinewill read Smith\nfrom the buffer, and it will continue immediately. It will not echo any newline characters to your console - getlinedoesn't do that.

因为您没有向标准输出输出一个,并且用户没有机会按 Enter。getline将从Smith\n缓冲区读取,并将立即继续。它不会向您的控制台回显任何换行符 -getline不会这样做。

Suddenly I get a newline after you enter your full name again. It's not really a huge issue to be honest. But I wouldn't mind knowing what happened if anyone can help me. Thanks!

在你再次输入你的全名后,我突然得到一个换行符。老实说,这并不是一个大问题。但如果有人可以帮助我,我不介意知道发生了什么。谢谢!

It is the newline that userenters with the Enterkey, it is not coming from your program.

它是用户Enter密钥输入的换行符,它不是来自您的程序。

EditPressing Enter in terminal usually (depending on the terminal setup) does few separate things:

通常在终端中按 Enter编辑(取决于终端设置)会做一些单独的事情:

  1. Inserting \ninto the input buffer
  2. Flushing the input buffer
  3. Shifting the input cursor one line down
  4. Moving the input cursor to the beginning of the line
  1. 插入\n输入缓冲区
  2. 刷新输入缓冲区
  3. 将输入光标向下移动一行
  4. 将输入光标移动到行首