List of Words

Apr 12, 2011 at 1:18am
I am trying to write a program where the user inputs a sentence and the program outputs each word on a separate line. I know I need a loop but I don't know how to set it up?
Apr 12, 2011 at 1:31am
Show us what you have so far and we'd be happy to help.
Apr 12, 2011 at 1:35am
you try to this code !
1
2
3
4
5
6
7
8
 cout<<" Enter the number which you want to chosse : ";
 cin>>chosse;
 do {
	 case 1: cout<< " doing something 1  which you want to do " ;
	 case 2 : cout<< " doing something 2 .....";
	 ........
	 default : break;
 while ( chosse < 1 || chosse > n );
Last edited on Apr 12, 2011 at 2:17am
Apr 12, 2011 at 1:35am
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
string sentence;
int length;

cout << "Enter a sentence:\n";
getline(cin, sentence);

int x = sentence.find(" ");
int y = sentence.find(" ", x);

cout << sentence.substr(0, x) << endl;
while(??????)
{
cout << sentence.substr(x+1, y-1) << endl;
int x = sentence.find(" ");
}

system("PAUSE");
return EXIT_SUCCESS;
}
Apr 12, 2011 at 1:45am
That's okay. How about writing a replace function. Just replace all of the spaces with '\n' and then output your string.
Apr 12, 2011 at 1:50am
That sounds great, but this is for a class and we haven't learned about replace functions yet so I wouldn't be able to use one. I like the idea of it though.
Apr 12, 2011 at 1:53am
"haven't learned replace functions"

Why would you let your class hold you back?!?!?! Just learn man, and grow and gain the skills and power to do everything on your own and create!!
Topic archived. No new replies allowed.