C++ 字符串短语以空格结尾?
我正在编写一个简单的代码来了解更多关于字符串的信息。当我运行代码时,它不会打印我的姓氏。有人能解释一下原因吗?我使用字符串短语来存储它,它似乎只存储了我的名字。这是代码C++ 字符串短语以空格结尾?,c++,string,c-strings,C++,String,C Strings,我正在编写一个简单的代码来了解更多关于字符串的信息。当我运行代码时,它不会打印我的姓氏。有人能解释一下原因吗?我使用字符串短语来存储它,它似乎只存储了我的名字。这是代码 #include <iostream> #include <string> #include <cstring> using namespace std; int main() { cout << "Exercise 3B" << endl; cou
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting;
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
cin >> phrase;
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
cout当您调用cin>>短语;
时,它只读取第一个非空格字符之前的字符串。如果您想在名称中包含空格,最好使用getline(cin,短语);
重要提示:getline()
将读取流缓冲区中的任何内容,直到第一个\n
。这意味着当您输入cin>>问候语;
时,如果按enter键,getline()
将读取该\n
之前尚未读取的所有内容,而该内容在短语
变量中没有任何内容,使其成为空字符串。一个简单的解决方法是调用getline()
两次。例如
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting; //IMPORTANT: THIS ASSUME THAT GREETING IS A SINGLE WORD (NO SPACES)
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
string rubbish_to_be_ignored;
getline(cin,rubbish_to_be_ignored); //this is going to read nothing
getline(cin, phrase); // read the actual name (first name and all)
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
在ubuntu 14.04上测试,当你调用cin>>短语;
时,它只读取第一个非空格字符的字符串。如果你想在名字中包含空格,最好使用getline(cin,短语);
重要提示:getline()
将读取流缓冲区中的任何内容,直到第一个\n
。这意味着当您输入cin>>问候语;
时,如果按enter键,getline()
将读取该\n
之前尚未读取的所有内容,而该内容在短语
变量中没有任何内容,使其成为空字符串。一个简单的解决方法是调用getline()
两次。例如
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting; //IMPORTANT: THIS ASSUME THAT GREETING IS A SINGLE WORD (NO SPACES)
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
string rubbish_to_be_ignored;
getline(cin,rubbish_to_be_ignored); //this is going to read nothing
getline(cin, phrase); // read the actual name (first name and all)
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
在ubuntu 14.04上测试
我使用字符串短语来存储它,它似乎只存储了我的名字
这是有道理的
cin >> phrase;
将在输入中遇到空白字符时停止读取
要阅读全名,您可以使用以下方法之一
使用两个调用cin>
std::string first_name;
std::string last_name;
cin >> first_name >> last_name;
使用getline
读取整行。getline
将读取一行中的所有内容,包括空格字符
getline(cin, phrase);
我使用字符串短语来存储它,它似乎只存储了我的名字
这是有道理的
cin >> phrase;
将在输入中遇到空白字符时停止读取
要阅读全名,您可以使用以下方法之一
使用两个调用cin>
std::string first_name;
std::string last_name;
cin >> first_name >> last_name;
使用getline
读取整行。getline
将读取一行中的所有内容,包括空格字符
getline(cin, phrase);
它对我有用。你确定你看不到想要的输出吗?是的。也许我的编译器没有一个它需要做字符串的文件。请你发布一张你输出的图片。在我的编译器中,字符串这个词甚至没有亮起到另一种颜色。如果它工作正常,它不会吗?术语警告:你把编译器和文本编辑器弄混了。编辑器显示并允许您编辑源代码。编译器将源代码转换为目标代码(链接器将目标代码转换为可执行程序)。编译器不会使文字变亮;如果您正确编写了源代码,则只会显示一个漂亮的“全部完成”键入消息。啊,明白了!谢谢你告诉我,因为我一直在想。它对我有效。你确定没有看到所需的输出吗?是的。可能我的编译器没有需要执行字符串的文件。请你发布一张输出的图片。在我的编译器中,字符串一词甚至不会亮起到另一种颜色。如果是工作正常,不是吗?术语警告:编译器与文本编辑器混淆。编辑器显示并允许您编辑源代码。编译器将源代码转换为目标代码(链接器将目标代码转换为可执行程序).编译器不会让文字变亮;如果你正确编写了源代码,它只会显示一个漂亮的“全部完成”类型的消息。啊,明白了!谢谢你告诉我,因为我一直在想。谢谢。我想可能就是这样!谢谢。我想可能就是这样!