MastersOfWar
MastersOfWar

Reputation: 1

I made this c++ program which counts the words but when the text input is more than 1 word it just gives me 18446744073709551615

I am a beginner and noob to c++ and i thought to create this program which takes in user input paragraph and then a word then finds where all the word is used.

this is the code

#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);

}

and it gives me this error

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.

Upvotes: 0

Views: 83

Answers (1)

Jeffrey
Jeffrey

Reputation: 11430

string.find() returns a position. It returns it with type size_t which is unsigned.

The value you are showing is string::npos, which means the word was not found. The most likely reason for the word to not be found is that:

cin >> user_text; doesn't do what you think it does. This will read a single word. You need to use getline instead to read a full line and not just a single word.

Please see: https://en.cppreference.com/w/cpp/string/basic_string/getline

Upvotes: 2

Related Questions