What does this mean?

I'm taking a class on C++ and I'm given this question but don't understand it. Thanks

Give the value assigned to flower or write the output to the display for each line of the following code fragment:

string flower = "rose";
flower = flower + " of Sharon";
cout << flower.at(0) << flower.at(8) << endl;
cout << flower.find("s") << " " << flower.find("S") << endl;
flower.replace(5, 2, "from");
flower.erase(0,4);
flower.insert(0, "thorn");
Complete the program and look the result of its execution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string flower = "rose"; 
   flower = flower + " of Sharon"; 
   cout << flower.at(0) << flower.at(8) << endl; 
   cout << flower.find("s") << " " << flower.find("S") << endl; 
   flower.replace(5, 2, "from"); 
   cout << flower << endl;
   flower.erase(0,4); 
   cout << flower << endl;
   flower.insert(0, "thorn"); 
   cout << flower << endl;

   return 0;
}
Last edited on
the window opens for a second and then closes?
and thanks again
i got it to stay open now but its only offering me 5 results when on the work im doing it saying theres a total of 7 answers reflecting off the original 7 functions in my first post
We aren't here to do your homework, but to help you understand it. If you're just gonna copy-paste vlad's code without even reading it then good luck...

Go over the code carefully and take note of what it's outputting. Also read your question again carefully.
ive got that, im not gonna waist my time for 1 problem, i'm trying to understand it.

but if cout is its output, i just dont understand why there is a block open for an answer that is a statement? i.e. string flower = "rose";
i just dont understand why there is a block open for an answer that is a statement?

I don't quite understand this sentence.

Your assignment is asking you to write what the code will display, or if a line doesn't display anything write the value of flower.
V = value, D = display
1
2
3
4
5
6
7
string flower = "rose";  //V
flower = flower + " of Sharon"; //V
cout << flower.at(0) << flower.at(8) << endl; //D
cout << flower.find("s") << " " << flower.find("S") << endl; //D
flower.replace(5, 2, "from"); //V
flower.erase(0,4);  //V
flower.insert(0, "thorn"); //V 

So for those lines with a V, what string does flower currently contain? And for those with a D, what will your code print to the screen?
Thank you

when i say block open, the teacher is asking to give the value assigned for the 7 individual commands. i.e theres a block next to every line for me to input the data.

if i copy and paste exactly what vlad posted and "debug it" the .exe window will appear and will show:

rS
2 8
rose from Sharon
from Sharon
thorn from Sharon
Read what I wrote. If the line doesn't output anything, you write what value is currently stored in flower.
For example:
 
string flower = "rose"; //flower now contains "rose", which would be the answer for this line. 
Topic archived. No new replies allowed.