C++:为什么读取字符串时总是以空格结尾?

发布于 2024-09-03 15:15:31 字数 578 浏览 2 评论 0原文

使用类型 std::string 接受一个句子,作为练习(我没有太多使用 C++ 中的字符串),我检查一个字符是否是元音。我得到了这个:

for(i = 0; i <= analyse.length(); i++) {
if(analyse[i] == 'a' || analyse[i] == 'e' [..etc..]) {
 ...vowels++;    
} else { ...
 ...consonants++;
}

如果字符串都是一个单词,那么这工作正常,但第二个我添加一个空格(IE:aeio aatest)它只会计算第一个块并将空格计算为辅音,并停止阅读该句子(退出 for 循环或其他内容)。

空格算作无字符== null吗?或者 std::string 有什么奇怪的地方?,了解为什么会发生这种情况会很有帮助!

编辑: 我只是通过 std::cin 接受字符串,例如:

std::string analyse = "";
std::cin >> analyse;

Using type std::string to accept a sentence, for practice (I haven't worked with strings in C++ much) I'm checking if a character is a vowel or not. I got this:

for(i = 0; i <= analyse.length(); i++) {
if(analyse[i] == 'a' || analyse[i] == 'e' [..etc..]) {
 ...vowels++;    
} else { ...
 ...consonants++;
}

This works fine if the string is all one word, but the second I add a space (IE: aeio aatest) it will only count the first block and count the space as a consonant, and quit reading the sentence (exiting the for loop or something).

Does a space count as no character == null? Or some oddity with std::string?, It would be helpful to know why that is happening!

EDIT:
I'm simply accepting the string through std::cin, such as:

std::string analyse = "";
std::cin >> analyse;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

影子是时光的心 2024-09-10 15:15:31

我猜您正在使用 your_stream >>> 之类的内容读取字符串。你的字符串;。字符串的运算符 >> 被定义为与 scanf%s 转换工作方式(大约)相同,它会一直读取直到它遇到空格——因此,operator>> 会执行相同的操作。

您可以使用 std::getline 读取整行输入。您可能还想查看 回答 我发布了上一个问题(提供了 std::getline 的一些替代方案)。

I'd guess you're reading your string with something like your_stream >> your_string;. Operator >> for strings is defined to work (about) the same as scanf's %s conversion, which reads up until it encounters whitespace -- therefore, operator>> does the same.

You can read an entire line of input instead with std::getline. You might also want to look at an answer I posted to a previous question (provides some alternatives to std::getline).

难理解 2024-09-10 15:15:31

我无法从您粘贴的代码中看出,但我会冒险猜测您正在使用流提取运算符(流>>字符串)读取字符串。

流提取运算符在遇到空格时停止。

如果这不是发生的事情,您能否向我们展示您如何填充字符串以及它的内容是什么?

如果我是对的,那么您将需要一种不同的方法将内容读入字符串。 std::getline() 可能是从文件读取的最简单的方法。它在换行符处停止,而不是在空格处停止。


根据编辑的问题进行编辑:
使用这个(仔细检查语法。我不在我的编译器前面。):

std::getline(std::cin, analyze); 

当您按“enter”时,这应该停止阅读。

I can't tell from the code that you have pasted, but I'm going to go out on a limb and guess that you're reading into the string using the stream extraction operator (stream >> string).

The stream extraction operator stops when it encounters whitespace.

If this isn't what's going on, can you show us how you're populating your string, and what its contents are?

If I'm right, then you're going to want a different method of reading content into the string. std::getline() is probably the easiest method of reading from a file. It stops at newlines instead of at whitespace.


Edit based on edited question:
use this (doublecheck the syntax. I'm not in front of my compiler.):

std::getline(std::cin, analyze); 

This ought to stop reading when you press "enter".

书信已泛黄 2024-09-10 15:15:31

如果您想读取整行(包括空白),那么您应该使用 getline 进行读取。大致如下:

#include <string>
istream& std::getline( istream& is, string& s );

要阅读整行内容,请执行以下操作:

string s;
getline( cin, s );
cout << "You entered " << s << endl;

PS:这个词是“辅音”,而不是“同意”。

If you want to read in an entire line (including the blanks) then you should read using getline. Schematically it looks like this:

#include <string>
istream& std::getline( istream& is, string& s );

To read the whole line you do something like this:

string s;
getline( cin, s );
cout << "You entered " << s << endl;

PS: the word is "consonant", not "consenent".

水染的天色ゝ 2024-09-10 15:15:31

istream 上的 >> 运算符通过空格分隔字符串。如果你想获取整行,可以使用readline(cin,destination_string)。

The >> operator on an istream separates strings on whitespace. If you want to get a whole line, you can use readline(cin,destination_string).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文