I've been working on a program my professor gave us awhile back and I've run into a logic issue, as in I can't figure out how to exactly do this. I need to output one word on each line of a sentence input by the user. For example, the user inputs "Hello World I'm Chris" and the program needs to output: Hello World I'm Chris
This is what I have so far:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string sentence;
int length;
cout << "Enter the sentence now." << endl;
getline(cin, sentence);
for(int i = 0; i < sentence.length(); i++)
{
if(sentence[i] != '\0')
{
cout << sentence[i];
}
else if(sentence[i] == '\0')
{
cout << endl;
}
}
system("pause");
}
However, when I run it, the program basically just outputs the same sentence. Is there another way I can do this? Many thanks.