字符串短语以 space 结尾?
String phrase ends at space?
我正在编写一个简单的代码来了解有关字符串的更多信息。当我 运行 我的代码时,它不会打印我的姓氏。有人可以解释为什么吗?我使用字符串短语来存储它,它似乎只存储了我的名字。这是代码。
#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;
}
当您调用 cin >> phrase;
时,它只读取字符串直到第一个非 space 字符。如果您想在您的名字中包含 space,最好使用 getline(cin,phrase);
。
重要提示:getline()
将读取流缓冲区中的任何内容,直到第一个 \n
。这意味着当您输入 cin >> greeting;
时,如果您按下 ENTER,getline()
将读取 \n
之前尚未读取的所有内容,这对您的 phrase
变量来说没有意义,使它成为一个空字符串。一个简单的方法是调用 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;
}
假设您将该代码存储在文件 Whosebug.cpp 中。示例 运行:
Chip Chip@04:26:00:~ >>> g++ Whosebug.cpp -o a.out
Chip Chip@04:26:33:~ >>> ./a.out
Exercise 3B
Kaitlin Stevers
String arrays
Please enter a greeting:
Hello
The greeting you entered was: Hello
Enter your full name
Kaitlin Stevers
Hello, how are you today Kaitlin Stevers?
测试于 ubuntu 14.04
I used string phrase to store it and it only appears to have stored my first name.
有道理。
cin >> phrase;
在输入中遇到空白字符时将停止读取。
要阅读全名,您可以使用以下方法之一。
两次调用 cin >>
。
std::string first_name;
std::string last_name;
cin >> first_name >> last_name;
使用getline
读取整行。 getline
将读取一行中的所有内容,包括空白字符。
getline(cin, phrase);
我正在编写一个简单的代码来了解有关字符串的更多信息。当我 运行 我的代码时,它不会打印我的姓氏。有人可以解释为什么吗?我使用字符串短语来存储它,它似乎只存储了我的名字。这是代码。
#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;
}
当您调用 cin >> phrase;
时,它只读取字符串直到第一个非 space 字符。如果您想在您的名字中包含 space,最好使用 getline(cin,phrase);
。
重要提示:getline()
将读取流缓冲区中的任何内容,直到第一个 \n
。这意味着当您输入 cin >> greeting;
时,如果您按下 ENTER,getline()
将读取 \n
之前尚未读取的所有内容,这对您的 phrase
变量来说没有意义,使它成为一个空字符串。一个简单的方法是调用 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;
}
假设您将该代码存储在文件 Whosebug.cpp 中。示例 运行:
Chip Chip@04:26:00:~ >>> g++ Whosebug.cpp -o a.out
Chip Chip@04:26:33:~ >>> ./a.out
Exercise 3B
Kaitlin Stevers
String arrays
Please enter a greeting:
Hello
The greeting you entered was: Hello
Enter your full name
Kaitlin Stevers
Hello, how are you today Kaitlin Stevers?
测试于 ubuntu 14.04
I used string phrase to store it and it only appears to have stored my first name.
有道理。
cin >> phrase;
在输入中遇到空白字符时将停止读取。
要阅读全名,您可以使用以下方法之一。
两次调用
cin >>
。std::string first_name; std::string last_name; cin >> first_name >> last_name;
使用
getline
读取整行。getline
将读取一行中的所有内容,包括空白字符。getline(cin, phrase);