基本字符串输入 [英] Basic String input
问题描述
我是新来的,这么光明我。
我刚刚遇到了这一段代码,允许用户在命令提示符下输入字符串。我知道他们做什么,这是伟大的。但我有一个关于cin和getline()函数的问题。
字符串名称;
cout<< 请输入你的全名: ;
cin>>名称 ;
cout<< Welcome<<名称<< endl;
cout<< 请再次输入您的全名:;
getline(cin,name);
cout<< 更好,谢谢<名称<< endl;
return 0;
现在当输出的时候,我得到了一些类似的东西:(使用john smith作为输入)
请输入您的全名:john smith
欢迎John
请再次输入您的全名:这是更好的感谢史密斯
我理解为什么会发生这种情况,getline仍然从输入缓冲区读取,知道如何解决它。我的问题是,为什么在请再次输入您的全名后面没有换行符?当我将代码修改为:
字符串名称;
cout<< 请输入你的全名: ;
cin>>名称 ;
cout<< Welcome<<名称<< endl;
cout<< 请再次输入您的全名:;
cin.ignore(256,'\\\
');
getline(cin,name);
cout<< 更好,谢谢<名称<< endl;
return 0;
突然我再次输入您的全名后,得到换行符。这真的不是一个真正的问题。但我不介意知道发生了什么,如果有人可以帮助我。谢谢!
当你输入John Smith作为输入时, cin> >
因此,在第一个<$ c后面的名字 $ c> cin ,name变量将包含 John
。在缓冲区中仍然会有 Smith \\\
,您已使用以下方法解决此问题:
cin.ignore(256,'\\\
');
注意:Konrad Rudolph建议,或您的代码中的任何其他魔法数字。而是使用 std :: numeric_limits< std :: streamsize> :: max()
。下面是文档对 istream :: ignore
的第一个参数的说明:
要提取(并忽略)的最大字符数。
如果这正是numeric_limits< streamsize> :: max()
,没有限制:
std :: numeric_limits< std :: streamsize> :: max(),'\\\
');
我的问题是,为什么没有换行符再次输入你的全名:?
因为您没有输出到stdout,用户没有机会按Enter键。 getline
将从缓冲区中读取 Smith \\\
,它将立即继续。它不会在控制台中回显任何换行符 -
getline
不会这样做。
突然,当你再次输入全名后,我得到一个换行符。这真的不是一个真正的问题。但我不介意知道发生了什么,如果有人可以帮助我。非常感谢!
这是用户输入的换行符,
- 在输入缓冲区中插入
\\\
li>
- 刷新输入缓冲区
- 将输入光标向下移动一行
- 将输入光标移动到
行
I'm new to this so bare with me. 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.
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)
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:
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!
You see, when you enter "John Smith" as a input first cin >> name
will not read the entire line, but the the contents of the line until the first space.
So, after the first cin
, name variable will contain John
. There will still be Smith\n
in the buffer, and you've solved this using:
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
:
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.
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. getline
will read Smith\n
from the buffer, and it will continue immediately. It will not echo any newline characters to your console - getline
doesn't do that.
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 user enters with the Enter
key, it is not coming from your program.
Edit Pressing Enter in terminal usually (depending on the terminal setup) does few separate things:
- Inserting
\n
into the input buffer - Flushing the input buffer
- Shifting the input cursor one line down
- Moving the input cursor to the beginning of the line
这篇关于基本字符串输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!