COMP2710: Homework 1 Solved

$25.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (3 votes)

Goals:
To find someplace where you can develop, compile, and run C++ code.
To write a very simple C++ program and learn a few things about buffer management using C++
String class.
Questions (put your answers in a pdf or txt file):
The C++ string class is useful for storing and manipulating with strings (texts). Answer the
following questions that highlight some of the useful features of C++ strings. To verify if
your answers are correct, you may write a small program to test them.
1) Write a code fragment that will use the “+” operator to concatenate two strings hashtag
and topic together. The strings are defined as follows:
string firstname = “#”;
string lastname = “WarEagle”;
(Note that the above is one way to initialize strings.)
2) Write a code fragment that will use the “+” operator to append the string username to
the string message and store it into the string message_buffer where the username
is first bracketed by the strings “%(” and “)%”. The strings are defined as follows:
string username = “Kate”;
string message = “Get in the loop with our War Eagle
group!”;
string message_buffer;
After executing your code fragment, message_buffer must contain the following
string:
%(Kate)%Get in the loop and join our War Eagle group!
3) Write the code fragment that will use the above code in (2) to allow the program to
append more user name and message into message buffer created in (2). All new
messages are always appended to the message buffer.
string username2 = “Daniel”;
string message2 = “I’m in!”;
After executing your code fragment, message_buffer should contain the following
string:
%(Kate)%Get in the loop with our War Eagle
group!%(Daniel)%I’m in!
4) Write the code fragment that will insert a “$^” string between two messages message3
and message4 and store them in message_buffer.
string message3 = “War Eagle”;
string message4 = “Beat Clemson!”;
After executing your code fragment, message_buffer should contain the following
string:
War Eagle$^Beat Clemson!
2
Your code fragment must print out this string in message_buffer and whenever there
is a string “$^” string in the message buffer, it must print a newline.
5) The variables username and message in (2) are objects, which mean that they
encapsulate both functions and attributes. There are many useful functions in the
string class. What does the function string.length() return? For example, in
question (2), what will message.length() return?
6) What will be the effect of executing the code message.clear() in question (2)?
Questions 7-11 refer to the state after the code fragment in (3) is executed.
7) Write the string function call that will return the index of the last occurrence of a string
“%(” in message_buffer.
8) Write the string function call that will return the index of the second last occurrence of a
string “%(” in message_buffer.
9) Write the code fragment that will return the string containing the last user name in
message_buffer. (The user name must not contain “%(” or “)%”.)
10) Write the code fragment that will return the string containing the second last user name in
message_buffer.
11) Write the code fragment that will return the string containing the third last message in
message_buffer.
12) Assume that a string buffer is defined as follows:
string messagebuffer;
What is the difference between the following codes, e.g. when the user input the line
“Tiger Group”?
a. cin >> messagebuffer;
b. getline(cin, messagebuffer);
Program (submit your code listing and a sample script of the execution):
A very common application is virtual bulletin board, social Internet group, chatroom or group
texting on your cell phone or computer. Messages can be posted on the bulletin board and are
readable from a group of users. Messages can also be sent from a user to another user or multiple
users through the network. In some cases, conference call involving multiple participants can chat
with each other in real time almost simultaneously. The applications that handle your messages
must first buffer the messages before they are sent. There are many challenging problems that
must be addressed for multiple users to exchange messages and access shared buffers
concurrently. However, instead of solving these difficult problems, you will focus on how to
manage message buffers.
The purpose of this homework is for you to write a short program in C++. Compile and run it
using the g++ compiler on a Linux computer (either in Davies 123 or Shelby 2125, your home
Linux machine, using secureCRT to log into a remote tux Linux machine, or using a virtual
machine like VirtualBox on your Windows PC). If you test on your programs on your own Linux
computer, you must eventually ensure that your program works correctly in the College of
Engineering tux Linux computers in Davies 123 or Shelby 2125 for full credits.
Use comments to provide a heading at the top of your code containing your name, Auburn Userid,
and filename. You will lose points if you do not have a comment block on EVERY program you
hand in. Also describe any help or sources that you used (as per the syllabus). Your program
must be written in the COMP 2710 C++ programming style guide described in the handout.
3
Write a program that will continually prompt for the user name and then prompt for multiple lines
of text messages. (The user name consists of only one word.) The text message entry is
terminated when in a new line, the user enters “$$” and the enter key, i.e. a line with only the
string “$$”. The string “$$” must NOT be included in the message. When the program reads
the inputs, it will then store the user name and their message into a message buffer that is of the
C++ string class. There is only one shared message buffer to store multiple user names and
their messages. All messages must be appended to the message buffer, i.e. the latest message
must be placed after the older messages in the shared message buffer. In other words, the shared
message buffer contains messages that are in chronological order. The format for appending the
username and message into the shared message buffer is as follows. The user name must first be
enclosed by the strings “%(” and “)%”followed by the user message before appending into the
shared message buffer. Examples are shown in Questions (2) and (3) above. Messages with
multiple lines must use the “$^” string to separate the messages in the message buffer as in
Question (4) above. The “$^” string must not be printed, instead a newline is printed whenever
that string occurs.
After a user terminated their message, the program will then prompt if there is any more user
name. If the answer is “yes”, then it starts the cycle again. The name of the next user name and
message will be appended to the same shared message buffer. Note that the start of each user
name and message can be detected by the string “%(”. If the answer is “no”, it will then
proceed to the next stage of outputting the user names and messages stored in the shared message
buffer. The output will display the text, “The current messages are:” and then must
show all the user names and messages stored in the shared message buffer in reverse
chronological order, i.e. the latest message first. Each message must start on a new line, where
the user name is printed followed by the “ >>” string and a newline and then the user message.
Your program must print a blank line after each message.
Here is a sample dialog (where the user input is depicted as Bold, but you do not need to display
user input in bold.):
===========================================================
| Welcome to the Tiger Social Network! |
===========================================================
Enter user name> Kate
Enter message> Get in the loop and join our War Eagle
group!
Summer in St. John’s islands was fun.
How was your summer?
$$
Any more users? > yes
Enter user name> Daniel
Enter message> I’m in!
$$
Any more users? > yes
Enter user name> Joey
Enter the message> Count me in.
We were in Tokyo; interesting!
$$
Any more users? > yes
4
Your program’s output must match the style of the sample output.
Deliverables in .pdf or .txt format:
– Answers to the questions.
– A source code listing with instructions on how to compile your code (commands)
– A script of a sample run printed from the console.
Enter user name> Kevin
Enter the message> What’s new?
We had a great time in the Outback.
$$
Any more users? > no
The current messages are:
Kevin >>
What’s new?
We had a great time in the Outback.
Joey >>
Count me in.
We were in Tokyo; interesting!
Daniel >>
I’m in!
Kate >>
Get in the loop and join our War Eagle group!
Summer in St. John’s islands was fun.
How was your summer?