Answer to Question #350969 in C++ for BScInf

Question #350969

Write a program that initialises a vector with the following string values: “what” “book” “is” “that” “you” “are” “reading”. Display the contents of the vector on the screen to the user as a question and read in the name of the book the user is reading (you can decide what it will be). Have the program add the name of the book to the vector, word by word. For example, if I am reading “How to learn C++”, the program should add the words, “How” “to” “learn” “C++”, one by one to the vector. Display the new vector. 


1
Expert's answer
2022-06-15T17:24:07-0400
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
	vector<string>vs{ "what", "book", "is" ,"that", "you", "are" ,"reading" };
	vector<string>::iterator it;
	for (it = vs.begin(); it != vs.end(); it++)
	{
		cout << *it << " ";
	}
	string tmp;
	while (cin.get()!='\n')
	{
		cin >> tmp;
		vs.push_back(tmp);
	}
	for (it = vs.begin(); it != vs.end(); it++)
	{
		cout << *it << " ";
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS