String assignment

Okay so I'm having trouble with this assignment.
-------------------
Prompt the user to enter 3 words.
Once you have all 3 words output them to the screen, in the order they were input, and
separated by a space.
Use only 2 string variables to capture all 3 words.
-------------------
Please help me with my code, I know it's wrong because it will only output one, but I can't quite figure out how to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include <string>
using namespace std;
int main ()
{
string input1;
string input2;

  cout << "Please enter three words: ";
  input2 = input1;
  input2 += " ";
  cout << "The words you entered are : " << input2 << ".\n";
  system ("PAUSE");
  return 0;
}
This makes no sense to me. Use 2 string variables to capture all 3 words?

You need 3 of them to capture 3 words. IF you use cin >>.

1
2
3
string s;
cout << "Enter words";
cin >> s" 


If the user inputs "Hey there"

and you print out "s". Only "hey" will be printed out.

Otherwise, if you use getline() you only need 1 string to capture everything.

1
2
string input1;
getline(cin, input1);


If the user enters "Hey there" It will print out the entire thing.
Last edited on
closed account (D80DSL3A)
I think kikmekings' code outputs no words because nothing has been read from cin yet.

A solution is to use one string to capture each word (one at a time), then "sum" them into the second string using += as you go.
input2 += " " + input1;
@fun2code yes. That would be a good way, but the description that @kikmekings gave us it says

Use only 2 string variables to capture all 3 words.


Thats why it kinda makes no sense to me.
closed account (D80DSL3A)
I see your point.
Hopefully kikmeking returns to clarify the intent.
Topic archived. No new replies allowed.