0

So I feel that I am close to solving a programming assignment that takes the most used word of each line and prints it on a line. So for example:

I am a man, am right? I don't know if i like that. It's okay to not feel okay. 

Would print: "am i okay" (punctuations and case are ignored for the assignment)

This is what I have done so far, but the problem is that the while loop that scans the lines never terminates and thus never prints the output in the external for loop. Anybody see where I went wrong?

string line; vector<string> result; while(getline(cin,line)){ //on each line } 
1

2 Answers 2

2

Your loop is correct as written; you just don't know how to signify the end of input. You're sitting there waiting for the program to progress, but the program is sitting there waiting for you to give it more input.

Press Ctrl+D (Linux) or Ctrl+Z (Windows) to send the EOF character/signal/potion to end the loop.

This way, all the common shell techniques like file redirection and pipes will also work.

Introducing artificial means like a double-newline or some magic command is non-conventional and makes your program harder to automate. (And making your program magically know that one of the newlines came from a keyboard hit rather than copy/pasting, is just not possible. Nor do you want it to be! That breaks a ton of valuable abstractions.) When writing a command-line tool, stick to standard practices as much as possible.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to code this in so i don't need to do that? The paragraph is inputted at once, and then enter is pressed and the result is printed.
@Team.Coco: The program has no way (and should have no way) of knowing that you want the third enter to behave differently from the first two enters, unless you always want to accept exactly three lines. What I've described is the correct way to do it, and it's really easy! You seem to suggest it's a hardship; I'm not sure why.
There is only one 'enter' by the user. The input paragraph is given at once
@Team.Coco: "There is only one 'enter' by the user" There is no distinction between an "enter" typed on your keyboard and an "enter" that was found in the input by other means, be that a copy/paste from clipboard or whatever (how are you doing it?). A newline is a newline is a newline.
1

Currently your program is waiting for an EOF character which indicates the input has ended. If you are running this and entering the input from the command line, you can manually insert an EOF by pressing Ctrl+D on *nix, or Ctrl+Z on windows. This will cause your program to break out of your getline loop.

If you would rather not do that, you need a way to break out of the getline loop, otherwise it will continue to run in that loop.

A nice idea might be detecting an empty line, so pressing enter twice ends the loop:

while(getline(cin,line)){ //on each line if(line == "") break; ... } 

15 Comments

Is there any way to do this so i just put in the input, press enter once and get the result?
I assume you're entering this from the command line (to stdin), so each line is ended with you pressing enter? If so, you need to decide some way to distinguish between a new line meaning your line is complete and you will type a new one, and a new line meaning you have finished.
The entire paragraph is inputted, and then i press enter. I need a way so that enter is only pressed once. I feel i am so close!
No, @Team. As I've said several times now, you are trying to make your program magically guess that when you hit "Enter" on your keyboard after the input, that you hit it on your keyboard and that you did so when no more input would be forthcoming. That is fundamentally and logically impossible. There are several ways in which you are trying to hammer a square peg through a round hole, right now.
@Team.Coco: Sounds like the teacher under-specified the problem! For class, a blank line to signify termination might be okay. I would definitely advise remembering this conversation though and using proper EOF in the future when you're in a job :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.