Garbled output from branching.

Good day. For a assignment,I needed to write a program that could decode twitter acronyms via branching. However,when I ran the program,I got output like this.

1
2
3
4
5
Enter your tweet (160 character limit) here: AFK IRL
Tweeted message: AFK IRL                                                                                                
Decoded message: awayin real liferm keyboard IRL                                                                        
Process returned 0 (0x0)   execution time : 4.498 s
Press any key to continue.


The source code is below. Thanks.



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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
using namespace std;

int main() {
    const unsigned length(160);
    string tweet;
    string plaintexttweet;
    int pos;
    string LOL = "laughing out loud";
    string AFK = "away form keyboard";
    string NVM = "never mind";
    string BFF = "best friend forever";
    string FTW = "for the win";
    string IIRC = "if I recall correctly";
    string TTYL = "talk to you later";
    string IMHO = "in my humble opinion";
    string IRL = "in real life";


    string copy3;
    //IO is here.
    cout << "Enter your tweet (160 character limit) here: ";
    getline(cin,tweet);
    //size change here
    tweet.resize(length);
    //Copying value to other variables
    for(int i = 0 ; i < tweet.size() ; i++){
	plaintexttweet.push_back(tweet[i]), copy3 += tweet[i];
    }


    //Find and replace in plaintext tweet
    pos = tweet.find("LOL");
    if (pos>=0){

    plaintexttweet.replace(pos, 3, LOL);
}

    pos = tweet.find("AFK");
    if (pos>=0){

    plaintexttweet.replace(pos, 3, AFK);
}

    pos = tweet.find("NVM");
    if (pos>=0){

    plaintexttweet.replace(pos, 3, NVM);
}

    pos = tweet.find("BFF");
    if (pos>=0){

    plaintexttweet.replace(pos, 3, BFF);
}

    pos = tweet.find("FTW");
    if (pos>=0){

    plaintexttweet.replace(pos, 3, FTW);
}

    pos = tweet.find("IIRC");
    if (pos>=0){

    plaintexttweet.replace(pos, 3, IIRC);
}

    pos = tweet.find("IMHO");
    if (pos>=0){

    plaintexttweet.replace(pos, 3, IMHO);
}

    pos = tweet.find("TTYL");
    if (pos>=0){

    plaintexttweet.replace(pos, 3, TTYL);
}



    pos = tweet.find("IRL");
    if (pos>=0){

    plaintexttweet.replace(pos, 3, IRL);
}




    cout << "Tweeted message: "<<tweet;

    cout << "\nDecoded message: "<<plaintexttweet;




    return 0;
}
Hello kings0d,

When I first looked at your program I wondered if there was a more difficult way to go about this.

Your instructions are a bit sparse. It is best to post the complete instructions as someone may see something that you have missed, but mostly it gives a better idea of what the program needs to do.

As I reread the directions I have the question as to what you mean and need for "branching".

This may not be exactly what you need, but can give you some ideas while I do some more work.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <sstream>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

int main()
{
	const unsigned LENGTH(160);
	constexpr size_t MAXSIZE{ 9 }, MAXTWEETSIZE{ 160 };

	std::string tweet{ "LOL IRL AFK" }; // <--- Used this way for testing.
	std::string plaintexttweet;
	//int pos; // <--- Not needed.

	std::string abv[MAXSIZE]
	{
		"LOL", "AFK", "NVM", "BFF",
		"FTE", "IIRC", "TTYL", "IMHO",
		"IRL"
	};

	std::string abvToString[MAXSIZE]
	{
		"laughing out loud", "away form keyboard",
		"never mind", "best friend forever",
		"for the win", "if I recall correctly",
		"talk to you later", "in my humble opinion",
		"in real life"
	};

	std::string copy3, word;

	//IO is here.
	//cout << "Enter your tweet (160 character limit) here: ";
	//getline(cin, tweet);
	std::cout << "\n Enter your tweet (160 character limit) here: " << tweet << "\n"; // <--- Used for testing.

	//size change here
	if (tweet.size() > MAXTWEETSIZE)
		tweet.resize(LENGTH);

	std::istringstream ss(tweet);

	while (ss >> word)
	{
		//std::cout << "\n " << word; // <--- Used for testing.

		for (size_t lc = 0; lc < MAXSIZE; lc++)
		{
			if (abv[lc] == word)
			{
				plaintexttweet += "   " + abvToString[lc] + '\n';
				break;
			}
		}
	}

	std::cout << "\n\n Tweeted message: " << tweet;

	std::cout << "\n\n Decoded message:\n" << plaintexttweet;

	return 0;
}

If you are not able to use string streams yet let me know. There are other ways to do this.

Hope that helps,

Andy
Hi. Thank you for your help. Unfortunately,I'm still in the beginning portion of this class,so I'm unable to use string stream,or anything more advanced then what I've covered so far,(currently on branching) as a solution for this assignment. Here are the specifics of the assignment I'm working through.
" Convert the user's tweet to a decoded tweet, replacing the abbreviations directly within the
tweet. You only need to replace the first instance of a particular abbreviation".

Again,thanks for the help.

You are searching in tweet, but replacing in plaintexttweet. So, if you replace more than one thing you will be wrong.
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
This helps a lot. Thanks for the help and the advice on naming conventions,Andy.
Topic archived. No new replies allowed.