My instructions are to write a program that assigns a five-word phrase with a single space between words to a user-defined string variable. It then outputs each word of the phrase separately. It must use the phrase "This is a test phrase" in the output, but also function properly when using any other five-word phrase. My program does this, but I don't know how to use string manipulation functions like getline to do this any other way than the simple way that I've done it below. If somehow would show me, I'd greatly appreciate it. This site and its tutorials have been very helpful so far!

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main ()
	string firstWord, secondWord, thirdWord, fourthWord, fifthWord;

	cout << "Enter a five-word phrase." << endl;
	cin >> firstWord >> secondWord >> thirdWord >> fourthWord >> fifthWord;

	cout << "Your first word is: " << firstWord << endl
		<< "Your second word is: " << secondWord << endl
		<< "Your third word is: " << thirdWord << endl
		<< "Your fourth word is: " << fourthWord << endl
		<< "Your fifth word is: " <<fifthWord << endl;

	return 0;
8 Years
Discussion Span
Last Post by Freaky_Chris

If you want to use getline() then strtok() would work perfectly for you. To split the line of text into indervidual words


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.