Branching Assignment Tweet Decoder

I am having trouble with this 3 step assignment. I have some starter code that is given but I am very confused on how to go about it.


Here is the details of the assignment

Branching - Tweet decoder
In this assignment, you’ll decode Twitter messages that include Internet abbreviations, such as
LOL and IRL. The starter program decodes two abbreviations.


1. Expand the number of abbreviations that can be decoded to include the following:
AFK = away from keyboard
NVM = never mind
BFF = best friends forever
FTW = for the win
IIRC = if I recall correctly
TTYL = talk to you later
IMHO = in my humble opinion
Save your solution for this step in a file named “step1.cpp”. You’ll need to submit each step as a
separate file.


2. Allow the user to enter a complete tweet (160 characters or less) as a single line of text. Use
getline to get the single line of text then resize (or truncate) to 160 characters.
Search the resulting string (using string’s find function) for those common abbreviations and
print a list of each abbreviation along with its decoded meaning.
Save your solution for this step in a file named “step2.cpp”. You’ll need to submit each step as a
separate file.



3. 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.
Save your solution for this step in a file named “step3.cpp”. You’ll need to submit each step as a
separate file.
Here is an example program execution for step 3 (user input is highlighted here for clarity):
Enter tweet:
I'm going to hang out with my BFF IRL tomorrow.
Decoded tweet: I'm going to hang out with my best friends forever in real life
tomorrow.
Another example execution for step 3 (user input is highlighted here for clarity):
Enter tweet:
So, IMHO he was going FTW, but I was so LOL that my BFF thought I
was going to start crying IRL! Anyway, gotta go, TTYL... I'm going AFK.
Decoded tweet: So, in my humble opinion he was going for the win, but I was so
laughing out loud that my best friends forever thought I was going to start
crying in real life! Anyway, gotta go, talk to you later... I'm going away
from keyboard.
This tweet has over 160 characters and gets truncated (user input is highlighted here for clarity):
Enter tweet:
Any people anywhere, being inclined and having the power, have
the right to rise up, and shake off the existing government, and form a new
one that suits them better. This is a most valuable - a most sacred right - a
right, which we hope and believe, is to liberate the world. - Abraham Lincoln.
Decoded tweet: Any people anywhere, being inclined and having the power, have
the right to rise up, and shake off the existing government, and form a new
one that suits them b


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include <iostream>
#include <string>
using namespace std;

int main() {
   string tweet;
   
   cout << "Enter abbreviation from tweet: ";
   cin >> tweet;
   
   // Output decoded abbreviation from tweet
   if (tweet == "LOL") {
      cout << "LOL = laughing out loud" << endl;
   }
   else if (tweet == "IRL") {
      cout << "IRL = in real life" << endl;
   }
   else {
      cout << "Sorry, don't know that one." << endl;
   }
   
   return 0;
}
Last edited on
So start with step 1. Add other else if statements to decode the extra abbreviations.

Step 2 gets a bit trickier. Easiest way would probably be to have them input the string then pop_back anything after 160 characters. Then use the find function to find any abbreviations and print out their meanings.

Step 3 is pretty similar to step 2., but instead of printing out the meaning, you need to replace the abbreviation in the "tweet".
Hello thexfiles,

You have starter code to work with step 1. Do notwork on the whole program at one time finish step 1 first. The two parts you will need to do is add "else if" statements for the other abbreviations and you will need to put all of that in a separate function and file.

I would also suggest using the variable name "abbreviation" in stead of "tweet" because it is more descriptive. Then you can use "tweet" in step 2 where it would make more sense.

Once you have step 1 working move on to step 2 with its own file and function.

Hope that helps,

Andy
Thank you for your response. I started step 1 but I am not sure if this makes sense.

Does C++ have a way to consolidate the IF end statements instead of just adding a bunch of them. I am working on Step 1. I tried watching Youtube videos but I do not understand how to link the IF end statements. Am I really suppose to do what I am doing?


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
#include <iostream>
#include <string>
using namespace std;

int main() {
   string tweet;
   
   cout << "Enter abbreviation from tweet: ";
   cin >> tweet;
   
   // Output decoded abbreviation from tweet
   if (tweet == "LOL") {
      cout << "LOL = laughing out loud" << endl;
   }
   else if (tweet == "IRL") {
      cout << "IRL = in real life" << endl;
      
   }
   else {
      cout << "Sorry, don't know that one." << endl;
   }
   cout << "Enter abbreviation from tweet: ";
   cin >> tweet;
   
   
   if (tweet == "AFK") {
      cout << "AFK = away from keyboard" << endl;
   }
   else if (tweet == "NVM") {
      cout << "Never Mind" << endl;
   }
   else {
      cout << "Sorry, don't know that one." <<
      cout << "BFF = best friends fo endl;
   }
   
    if (tweet == "BFF") {
      cout << "BFF = best friends forever" << endl;
   }
   else if (tweet == "FTW") {
      cout << "for the win" << endl;
   }
   else {
      cout << "Sorry, don't know that one." << endl;
   }
   if (tweet == "IIRC") {
      cout << "IIRC = if i recall correctly" << endl;
   }
   else if (tweet == "TTYL") {
      cout << "talk to you later" << endl;
   }
   else {
      cout << "Sorry, don't know that one." << endl;
    
     if (tweet == "IMHO") {
      cout << "IMHO = "in my humble opinion" << endl;
   }
   else if (tweet == "TTYL") {
      cout << "talk to you later" << endl;
   }
   else {
      cout << "Sorry, don't know that one." << endl;
   
  
 
   
   
  
   
   return 0;
[ 

Last edited on
You can keep adding else if statements, you're not just limited to one, for example:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int num;
    cin >> num;
    if (num == 1) cout << 1;
    else if(num == 2) cout << 2;
    else if (num ==3) cout << 3;
    else if(num == 4) cout << 4;
    else if (num == 5) cout << 5;
    else cout << "number is greater than 5";
}
Looks much cleaner thank you.

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
#include <iostream>
#include <string>
using namespace std;

int main() {
   string tweet;
   
   cout << "Enter abbreviation from tweet: ";
   cin >> tweet;
   
   // Output decoded abbreviation from tweet
   if (tweet == "LOL") {
      cout << "LOL = laughing out loud" << endl;
   }
   else if (tweet == "IRL") {
      cout << "IRL = in real life" << endl;
       }
   else if (tweet == "AFK") {
      cout << "away from keyboard" << endl;
       }
   else if (tweet == "BFF") {
      cout << "best friends forever" << endl;
    }
   else if (tweet == "FTW") {
      cout << "for the win" << endl;
   }
     
   else if (tweet == "IIRC") {
      cout << "if i recall correctly" << endl;
   }
   
   else if (tweet == "TTYL") {
      cout << "talk to you later" << endl;
   }
    
   else if (tweet == "IMHO") {
      cout << "in my humble opinion" << endl;
   }
   
   
   else { cout << "Sorry, don't know that one." << endl;
   }


I am working on step 2 but I am not sure why my code is not working.


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
 #include <iostream>
#include <string>
using namespace std;

int main() {

    string tweet;
    std::string n;
    double s;

    cout << "Enter tweet:";

    cin >> tweet;

    std::getline(std::cin,tweet);
    
    int LOLpos = MyTweet.find("LOL");

if (LOLpos != -1) {

    MyTweet.replace(LOLpos, 3, LOL2);

}

    cout << "NVM I forgot what I was going to say TTYL:";

    cin >> s; }

   
    cout << tweet << " " << n << " " << s << endl;

    return 0;
}
   
Hello thexfiles,

Does C++ have a way to consolidate the IF end statements instead of just adding a bunch of them.

Two thoughts I had; one you could "enum" (enumeration) of the abbreviations and use a "switch" in stead of the if else if statements. Two you could use "map" (include header file map) to hold the abbreviation and the description. The "enum" and "switch" might be better for what you have learned so far.

As I understand step 2; you find and print a list of the abbreviations with the description not replace as you have tried. Nice idea though, but that would change the length of 160 characters.

You have a good start for step 2, but it needs to be in its own file and its own function. Putting step 2 in main would work for testing, but you can do the same thing with a function.

Hope that helps,

Andy
It looks like step 2 should just print out a list of abbreviations found and their meanings. Part 3 is where you replace the text.
thank you for all your help. I will try these reccomendations
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
 #include <iostream>
#include <string>
#include <cstdlib>
	using namespace std;

	//Define structure
	typedef struct AbbreviationType
	{
	string abbreviation;
	string abbreviationName;
	} AbbreviationType;
	//Allow the user to enter a complete tweet (160 characters or less)
	//as a single line of text.
	AbbreviationType abb[160] = { { "AFK", "away from keyboard"},
	{ "FTW", "for the win" },{ "BFF", "best friends forever" },
	{ "NVM", " never mind" },{ "IIRC", "if I recall correctly" },
	{ "TTYL", "talk to you later" },{ "IMHO", "in my humble opinion" },
	};
	//Search the resulting string (using string’s find function)
	//for those common abbreviations and print a list of each
	//abbreviation along with its decoded meaning
	bool Search(const std::string& Selection)
	{
	for ( int x = 0; x < 50; x++ )
	{
  if ( abb[x].abbreviation.compare(Selection) == 0 )
  {
   cout << "Abbreviation of "<<abb[x].abbreviation<<" is: " << abb[x].abbreviationName << endl;
   return true;
  }
	}
return false;
	}
	int main()
	{
//Declare variable
	string abb;
	//Prompt and read input from the user
	cout<<"Enter the abbreviation:";
	cin>>abb;
	//Check input is valid or not
	if ( !Search(abb) )
	cout << "Failed to locate Abbreviation.\n";
	//Pause the system for a while
	system ("PAUSE");
return 0;
}



What do you guys think? Am I doing this correctly?
Last edited on
Topic archived. No new replies allowed.