C++ 在C+中从内存中清除回车符+;

C++ 在C+中从内存中清除回车符+;,c++,C++,我有以下代码: int main() { // Variables char name; // Take the users name as input cout << "Please enter you name..." << endl; cin >> name; // Write "Hello, world!" and await user response cout << "Hello, " << name <<

我有以下代码:

int main()
{
// Variables
char name;

// Take the users name as input
cout << "Please enter you name..." << endl;
cin >> name;


// Write "Hello, world!" and await user response
cout << "Hello, " << name << "!" << endl;
cout << "Please press [ENTER] to continue...";
cin.get();

return 0;
阻止这一切发生?我知道这是可能的,就像我以前做过的那样,但我不记得它是什么,也不记得在哪里可以找到它。事先非常感谢。

最简单的答案:

int main()
{
// Variables
char name;

// Take the users name as input
cout << "Please enter you name..." << endl;
cin >> name;
cin.get(); // get return here


// Write "Hello, world!" and await user response
cout << "Hello, " << name << "!" << endl;
cout << "Please press [ENTER] to continue...";
cin.get();

return 0;
}
intmain()
{
//变数
字符名;
//将用户名作为输入
姓名;
cin.get();//在此处获取return
//写下“你好,世界!”并等待用户响应

cout首先,name是一个字符串,您在
char
中读取它

char name;
应该是

string name;
同时添加
#包括

现在要从缓冲区中清除换行符,可以执行以下操作:

std::cin.ignore(1);

在阅读
名称之后

可以告诉流忽略接下来的N个字符

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

这将导致
cin
最多跳过1000个字符,或者直到它找到(并删除)换行符(
'\n'
)为止。可以根据需要指定其他限制或终止字符。

确实要使用输入中直到换行符为止的所有字符作为名称。
目前,您的代码只读取第一个单词

#include <iostream>
#include <string>

int main()
{
    // Variables
    std::string name;

    // Take the users name as input
    // Read everything upto the newline as the name.
    std::cout << "Please enter you name..." << std::endl;
    std::getline(std::cin, name);

    // Write "Hello, world!" and await user response
    // Ignroe all input until we see a newline.
    std::cout << "Hello, " << name << "!\n";
    std::cout << "Please press [ENTER] to continue..." << std::flush;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
}
#包括
#包括
int main()
{
//变数
std::字符串名;
//将用户名作为输入
//把所有的东西读到新行作为名字。

std::cout
cin
忽略回车符,
'\n'
之前的值存储在变量中,
'\n'
保留在输入流中。当调用
cin.get()
时,它获取输入流中已经存在的值(即
'\n'
),因此被跳过

为了避免这种情况,蒂姆的答案是完美的

cin >> name;
cin.get(); // get return here
或者你也可以这样做

(cin >> name).get(); // get return as soon as cin is completed.
更新:正如Loki Astari所指出的,当对数组
名称
使用
cin>
运算符时,它将读取到第一个空格字符。在“Tim Hoolihan”中,名称之间有一个空格。因此名称数组将存储
{T'、'i'、'm'、'\n}
作为值,
'\n'
标记字符串的结尾。应避免对字符串使用字符数组,而应使用
#include
头文件中的类
字符串。字符串之间可以包含空格字符,而数组不能

#include<iostream>
#include<string>

int main()
{
  string name; // string is a class, name is the object
  cout << "Please enter you name..." << endl;
  (cin >> name).get(); 
  cout << "Hello, " << name << "!" << endl;
  // String using the "power" of an array
  cout << "First two characters of your name are " << name[0] << name[1] << endl;
  cout << "Please press [ENTER] to continue...";
  cin.get();
  return 0;
}
#包括
#包括
int main()
{
string name;//string是一个类,name是对象
cout name.get();

我同意吗,这就是他们在大学一年级向我们展示的。抓住那个额外的字符是一个简单的解决方案:)我确信有一个替代方法。@tunetosuraj。因为
操作符>
应用于字符串
名称
时,将读取到第一个空格字符。在“Tim Hoolihan”中,这是空格在名称之间。然后应用
cin.get()
时,输入上仍有一堆文本要读取,因此不会等待。交互式用户输入是基于行的(当您点击
时缓冲区会刷新)因此,在处理手动用户输入时,您应该一次读取一行。@LokiAstari啊!很抱歉我错过了
char name
。OP应该将其修复为
string name
,并放置
\include
头文件。我们在读取名称后忽略第一个字符。这恰好是新行。+1是最佳实践答案。我的答案是肯定的只有管道胶带才能回答
#include<iostream>
#include<string>

int main()
{
  string name; // string is a class, name is the object
  cout << "Please enter you name..." << endl;
  (cin >> name).get(); 
  cout << "Hello, " << name << "!" << endl;
  // String using the "power" of an array
  cout << "First two characters of your name are " << name[0] << name[1] << endl;
  cout << "Please press [ENTER] to continue...";
  cin.get();
  return 0;
}