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;
}

Recommended Answers

All 2 Replies

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

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.