Your program outputs the same sentence because you told it to.
for(int i = 0; i < sentence.length(); i++)
{
if(sentence[i] != '\0') // If the current char is not the end,
{
cout << sentence[i]; // print the character.
}
else if(sentence[i] = '\0') // This should be "=="
{
cout << endl;
}
}
Basically, you are printing each letter in the sentence back to std::cout
.
Please search StackOverflow for "C++ print word sentence", as many people have posted questions about this assignment.
Edit 1: The fundamentals of the assignment
The assignment requires you to extract letters from the input string to form a word. There are many ways to do this. Search your text book or reference manual for the std::basic_string
class and see what functions could help you.
Some people start from the first letter in the sentence and search for the next character that is not a letter:
const char valid_letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
sentence.find_first_not_of(valid_letters);
And they use the returned position to get a substring (std::string::substr) between the two positions.
Another approach is to use a loop. If the present character is a letter, append to the word
string.
Again search and see what examples you can find.