Text Abbreviation Code Problems

Hey there, I'm trying to write a program that will output the expanded version of text message lingo. For example, if the user inputs "LOL", the output will be "laughing out loud". I'm getting an error that says "comparison with string literal results in unspecified behavior." I have no idea what I'm doing wrong. Here's my code:


int main() {

string userText;
unsigned int i=0;

cout << "Input an abbreviation" << endl;
cin >> userText;

while(i < userText.length())
{
if (userText.at(i)=="LOL") {
userText.replace(i,3, "laughing out loud");
}
if (userText.at(i)=="BFF") {
userText.replace(i,3, "best friends forever");
}
if (userText.at(i)=="IMHO") {
userText.replace(i,4, "in my humble opinion");
}
if (userText.at(i)=="TMI") {
userText.replace(i,3, "too much information");
}
}
cout << "Expanded: " << userText << endl;
getline (cin, userText);

return 0;
}
Last edited on
closed account (48T7M4Gy)
if (userText.at(i)=="LOL")

userText.at(i) is a char. You are comparing it to a string LOL. Cannot!
Last edited on
Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post, highlight your code and click the <> button on the right.

while(i < userText.length()) i is not modified in the loop and will thus result in an infinite loop.

getline (cin, userText);
What is the purpose of this? cin >> userText will leave a newline character which will result in getline reading that and skipping your intended input.
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).

http://www.cplusplus.com/reference/string/string/getline/

I would recommend to make a function that expands the abbreviation, to make your code easier to read.
1
2
3
4
5
6
7
8
string expand( string abbrev )
{
    if( abbrev == "LOL" )
        return "laughing out loud";
    else if( abbrev == "BFF" )
        return "best friends forever";
    // ...
}

You can use it like so
1
2
3
4
5
6
7
8
9
int main()
{
    string userText;
    cout << "Input an abbreviation\n";
    cin >> userText;

    string expanded = expand( userText );
    cout << "Expanded: " << expanded << '\n';
}
closed account (48T7M4Gy)
@OP
Why not consider parallel arrays? One array is an array of abbreviations and the other one is a parallel array of expansions.

eg string abbreviation[] = {"LOL","BFF", "...}
string expansion[] = {"laughing out loud", "best friends forever", "...}

From there you can do the translation if you have the connecting array index. Most of OP code then becomes unnecessary.
closed account (48T7M4Gy)
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
30
31
32
33
#include <iostream>
#include <string>

using namespace std;

int main() {
    
    string abbreviation[] = {"LOL", "BFF", "IMHO", "TMI"};
    string expansion[] = { "laughing out loud","best friends forever","in my humble opinion","too much information" };
    size_t limit = sizeof(abbreviation)/ sizeof(string);
    
    size_t index = 0;
    
    size_t position = 0;
    
    string userText;
    
    cout << "Input a line of text: ";
    getline(cin, userText);
    
    while (index != limit)
    {
        position = userText.find(abbreviation[index]);
        
        if(position != std::string::npos)
            userText.replace(position, abbreviation[index].length(), expansion[index] );
        index++;
    }
    
    cout << "Expanded: " << userText << endl;
    
    return 0;
}


Input a line of text: Actually IMHO it works LOL but that's only IMHO
Expanded: Actually in my humble opinion it works laughing out loud but that's only IMHO
Program ended with exit code: 0


This will expand the first of each abbreviation detected in a sentence and can be extended easily to expand multiple instances of the same abbreviation.

Last edited on
Topic archived. No new replies allowed.