Text decoder

The purpose of this program is to find the abbreviations in a text and to print the meaning of the abbreviations. I cant get it to print out the right abbreviations. It just prints out all of them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include <iostream>
#include <string>// FIXME include the string library
using namespace std;

int main() {
   string userText;

  cout << "Enter text: " << endl;
  getline(cin, userText);
  cout << "You entered: " << userText << endl;
  if (userText.find ("BFF")) {
     cout << "BFF: best friend forever" << endl;
  }
  if (userText.find ("IDK")) {
       cout << "IDK: I don't know" << endl;
  }
  if (userText.find ("JK")) {
     cout << "JK: just kidding" << endl;
  }
  if (userText.find ("TMI")) {
     cout << "TMI: too much information" << endl;
  }
  if (userText.find ("TTYL")) {
     cout << "TTYL: talk to you later" << endl;
  }

   
   return 0;
}
This is what it should look like.

Enter text: IDK if I'll go. It's my BFF's birthday.
You entered: IDK if I'll go. It's my BFF's birthday.
BFF: best friend forever
IDK: I don't know
This is what it should look like:
 
    if (userText.find ("BFF") != string::npos)


http://www.cplusplus.com/reference/string/string/find/
Can you explain why?
Follow the link and read the description of the function, what it does, what value is returned:
http://www.cplusplus.com/reference/string/string/find/

The find() function returns a number which represents the position within the string of the item being searched for. If the item isn't found, the return value is the largest number possible - which in this context means string::npos.

Last edited on
Got it! thanks!
Topic archived. No new replies allowed.