[英]I made this c++ program which counts the words but when the text input is more than 1 word it just gives me 18446744073709551615
我是 c++ 的初学者和菜鸟,我想创建这个程序,它接受用户输入的段落,然后一个单词然后找到所有单词的使用位置。
这是代码
#include <iostream>
using namespace std;
int main()
{
string user_text;
string user_word;
cout << "Enter the text: ";
cin >> user_text;
cout << "What is the word that you want to find: ";
cin >> user_word;
cout << user_text.find(user_word);
}
它给了我这个错误
Enter the text: well Hello this is 1
What is the word that you want to find: 18446744073709551615
D:\Projects\C++\Visual Studio\WordCounter\x64\Debug\WordCounter.exe (process 20076) exited with code 0.
string.find()
返回 position。 它以无符号的size_t
类型返回它。
您显示的值是string::npos
,这意味着找不到该词。 找不到该词的最可能原因是:
cin >> user_text;
不做你认为它做的事。 这将读取一个单词。 您需要使用getline
来读取整行而不是单个单词。
请参阅: https://en.cppreference.com/w/cpp/string/basic_string/getline
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.