Text message decoder (loops)

Get a line of text from the user. Output that line. Then output the line again,this time expanding all of the found common text message abbreviations. You can do this by using a loop.
Ex:
Enter text: My BFF is David. IDK who your BFF is.
You entered: My BFF is David. IDK who your BFF is.
Expanded: My best friend forever is David. I don't know who your best friend forever is.
Support these abbreviations:

BFF -- best friend forever
IDK -- I don't know
JK -- just kidding
TMI -- too much information
TTYL -- talk to you later
Note: If an abbreviation appears more than once, every instance gets expanded.
-----------------------------------------------------------------------
Attempt at solution:
#include <iostream>
#include <string>
using namespace std;

int main() {

string userText;
unsigned int i=0;

cout << "Enter text: "<< endl;
getline (cin, userText);
cout << "You entered: " << userText << endl;
getline (cin, userText);

while(i < userText.length())
{
if(userText.at(i)== IDK) {
userText.replace(i,3, "I don't know");
}
if (userText.at(i)== TTYL) {
userText.replace(i,4,"talk to you later");
}
if (userText.at(i)== BFF){
userText.replace(i,3,"best friend forever");
}
if (userText.at(i)== JK){
userText.replace(i,2,"just kidding");
}
if (userText.at(i)== TMI){
userText.replace(i,3,"too much information");
}
}
cout << "Expanded: "<< userText << endl;
getline (cin, userText);


return 0;
}


---------------------------------------------------------------------------
I know something is wrong with my if statements but I'm not quite sure how to fix them.
Your problem description is very vague. Please be as specific as you can be. If you're getting compile errors, say so and include the list of compile errors.

A few of problems I see are:

1) Line 17,20,23,26,29: Your abbreviations are not quoted. The compiler thinks they are undefined identifiers.

2) If the abbreviation occurs in the middle of a word, you're going to do the replacement in the middle word. Better to use the cin >> operator to read one word at a time and then check the word read.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
i obtain this error when using quotes:
1
2
 main.cpp:39:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
 if (userText.at(i)== "TMI"){

I've also tried using the .find() method but this results in a runtime error.

do you know another way around these errors?
Did you find a way past your errors? I'm doing the same assignment and I'm totally stuck right now.
no I haven't. I'm really confused.
std::basic_string::at returns a reference to the character at the specified location.
"TMI" is a string. You're trying to compare a single character to a string.
Topic archived. No new replies allowed.