Write code (C++) that inserts userItems into the output string stream itemsOSS u
ID: 3761207 • Letter: W
Question
Write code (C++) that inserts userItems into the output string stream itemsOSS until the user enters "Exit". Each item should be followed by a space. Sample output if user input is "red purple yellow Exit":red purple yellow
#include #include #include using namespace std; int main() { string userItem; ostringstream itemsOSS; cout << "Enter items (type Exit to quit):" << endl; cin >> userItem; while (userItem != "Exit") { /* Your solution goes here */ cin >> userItem; } cout << itemsOSS.str() << endl; return 0; } Write code (C++) that inserts userItems into the output string stream itemsOSS until the user enters "Exit". Each item should be followed by a space. Sample output if user input is "red purple yellow Exit": red purple yellow
#include #include #include using namespace std; int main() { string userItem; ostringstream itemsOSS; cout << "Enter items (type Exit to quit):" << endl; cin >> userItem; while (userItem != "Exit") { /* Your solution goes here */ cin >> userItem; } cout << itemsOSS.str() << endl; return 0; }
Explanation / Answer
#include <iostream>
#include <sstream>
//#include
using namespace std;
int main() {
string userItem;
ostringstream itemsOSS;
cout << "Enter items (type Exit to quit):" << endl;
cin >> userItem;
while (userItem != "Exit") {
itemsOSS<<userItem<<" "; //Write the string followed by a space to the stream.
cin >> userItem;
}
cout << itemsOSS.str() << endl;
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.