0

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.

user3097575
  • 5
  • 1
  • 1
  • 4
  • What exactly do you mean? I can't tell if you are asking if you want every word to be displayed on a new line or if you just want the same sentence. If you want the same sentence then doesn't your code already do this? – Andrew Dec 19 '13 at 23:51
  • 1
    By the way, one '=' means assignment, two '==' means equivalence. – Thomas Matthews Dec 19 '13 at 23:52
  • @Lemony-Andrew The program needs to output one word for every line, as the example shows. The program at the moment outputs jsut the string but I want it to output one word per line. – user3097575 Dec 19 '13 at 23:55
  • @ThomasMatthews Thanks, I actually had that, just didn't copy it down correctly. Edited the code to resemble what I have. – user3097575 Dec 19 '13 at 23:56
  • @user3097575 Have you thought about, that your program is outputting the right answer but its all outputting onto one line. Change cout << sentence[i]; into cout << sentence[i] << "\n"; Hopefully that will help you debug. – Chris Condy Dec 19 '13 at 23:56
  • According to your example, your program is working correctly. – Thomas Matthews Dec 19 '13 at 23:56
  • @ChrisCondy I tried that and what that did was output a letter on each line, however it did skip a line for each space which was weird. – user3097575 Dec 20 '13 at 00:00
  • @user3097575 Ive added an answer below hopefully the dot points I added will help – Chris Condy Dec 20 '13 at 00:08
  • I typed in your question's title into Google, "stackoverflow c++ one word per line" and got this list: https://www.google.com/search?q=stackoverflow+c%2B%2B+one+word+per+line&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a – Thomas Matthews Dec 20 '13 at 00:09

3 Answers3

1

According to this the \0 does not represent a whitespace. It seems you want something more like:

[...]
if(sentence[i] == ' ') cout << endl; // check for whitespace
else cout << sentence[i];
[...]

By the way, due to the way markdown formats text, the 'one word per line' thing was not clear, I had to fake-edit your post to see what exactly you meant. I think using a code tag would solve that.

Community
  • 1
  • 1
Futile
  • 133
  • 1
  • 6
  • Ahhh, damn, as soon as I read "whitespace" I realized I was using the null terminator at the end of the sentence instead of the actual whitespace. Many thanks, it's been a long semester and my logic is down the drain. – user3097575 Dec 20 '13 at 00:05
  • This will not satisfy the requirements because a word at the end of a sentence does not use a space as a separator. – Thomas Matthews Dec 20 '13 at 00:05
0

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.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • I've searched several times however I have only found things regarding C or Java, both of which I have no experience in. The '=' was an '==', however I miscorrectly copied my code to the question. It still outputs the regular sentence. – user3097575 Dec 19 '13 at 23:58
  • Sorry, but my memory differs: http://stackoverflow.com/questions/7621727/split-a-string-into-words-by-multiple-delimiters-in-c – Thomas Matthews Dec 20 '13 at 00:07
0

Ok first of all with the code feel free to add in the input stuff you have in before!

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string sentence = "Hello World";
  int length;
  string temp = "";

  for(int i = 0; i < sentence.length(); i++)
  {
    temp += sentence[i];
    if(sentence[i] == ' ')
    {
        cout << temp << "\n";
        temp = "";
    }
  }
  std::cout << temp;

  return 0; //*
}
  1. Your orignal code does the following logic. If it not the end of the file output the current character. Which it does so make sure you add in \n to the end of every word (Just to make debugging easier for you! You can always fix that later)
  2. The logic of the above code does this. Create a temp variable that we will just keep adding letters to until we find a space character. When we find a space character output the temp variable. And reset it do this until the end of the sentence then at the end of the program output temp again since we hit the end of line character!
Chris Condy
  • 626
  • 2
  • 9
  • 25