Hello kings0d,
I kind of figured that string stream would be ahead of where you would be at, but still a fair example to keep.
When I looked at you original code I finally figured out what you are doing.
The instructions say to replace the abbreviations directly in "tweet", but I like creating a different variable to work with in case I should need the original value of "tweet". Since it is technically wrong it will depend on what you can get by with.
Your line of code
pos = tweet.find("IMHO");
is using the wrong variable. This may work for awhile, but at some point it will cause a problem when you use the value of "pos" in
plaintexttweet.replace(pos, 3, AFK);
. Your code should look like:
1 2 3 4
|
pos = plaintexttweet.find("LOL");
if (pos >= 0)
plaintexttweet.replace(pos, 3, LOL);
|
Since you are working with "plaintexttweet". This along with some other changes I made will make for a nicer output.
I will cover some changes I made in your code:
const size_t LENGTH(160);
. The "unsigned" will work, but when you look at a function like
tweet.size()
it returns a "size_t" which is a typedef alias for "unsigned int". And there are other functions that return a "size_t" or "size_type" which is the same as "size_t". It is something I became use to working with because of how often it is used.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
string tweet{ "AFK LOL IRL" }; // <--- Used this way for testing.
string plaintexttweet;
string LOL = " laughing out loud\n";
string AFK = " away form keyboard\n";
string NVM = " never mind\n";
string BFF = " best friend forever\n";
string FTW = " for the win\n";
string IIRC = " if I recall correctly\n";
string TTYL = " talk to you later\n";
string IMHO = " in my humble opinion\n";
string IRL = " in real life\n";
//string copy3; // <--- Not used.
|
Line 1 is a little trick you can use to not have to enter something each time the program is run. Just helps speed up testing and debugging.
For now I have started each string with a space an ended with "\n". Later you will have to change this for the output to look right.
I have changed these lines to work with line 1 above:
1 2 3
|
//cout << "Enter your tweet (160 character limit) here: ";
cout << "Enter your tweet (160 character limit) here: " << tweet; // <--- Used for testing.
//getline(cin, tweet);
|
I changed this:
1 2 3
|
//size change here
if (tweet.size() > LENGTH)
tweet.resize(LENGTH);
|
If "tweet" is less than 160 there is no point adding something you do not need.
The for loop is unnecessary. All you need is
plaintexttweet = tweet;
.
The end of the program looks like this:
1 2 3 4 5 6 7 8 9 10
|
cout << "\n\nTweeted message: " << tweet;
cout << "\n\nDecoded message:\n " << plaintexttweet;
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
|
In lines 1 and 3 I start out with the "\n\n"s to spread out the output. What I have come up with produces this:
Enter your tweet (160 character limit) here: AFK LOL IRL
Tweeted message: AFK LOL IRL
Decoded message:
away form keyboard
laughing out loud
in real life
Press Enter to continue:
|
This works fine for testing right now, but will most likely need to be changed later.
When it comes to naming things the general consensus is that regular variables should start with a lower case letter, classes and structs should start with an upper case letter and variables defined as being constant are all capital letters. This is not set in stone, but more of a guide line and what is most often used. In the end it is your choice of what you do. Just be consistent with it.
For names that contain more than one work there are two methods you can use. What I see, and use my-self, is the camelCase form, i.e., "plainTextTweet". The other would be the old DOS method of showing a space the underscore, i.e., "plain_text_tweet". Either method is your choice. Just be consistent.
In this program variable names like "LOL" would be an exception for the way they are used, but you could make them a constant (
const string LOL = " laughing out loud\n";
because they should never be changed by the program just used. Probably a better choice to make them constant.
The code I have used just before the "return" is something I use with my IDE. You may not need this or want it, but worth saving for the future should you ever need to create a pause in a program. If you use the Windows or (Windoze) this is better than using "system()" commands.
Hope that helps,
Andy