0

I would like to point I am not good in C++, I usually use C but i thought that get to know some C++ basic will be good so I need to send data to server and normal std::cin >> variable can't do it because it only reads input to space so I read about getline and it's working great, but when I do something liek this in infinite loop:

for (;;)
{
std::cout << "Hello" << std::endl;;
std::getline(std::cin,darek.wiadomosc);
}

Durning first itteration it shows on screen double Hello like: Hello Hello String that is being entered

But after one loop it shows everything good. It's problem with getline I'm sure because when I changed it to std::cin >> just for test it worked. Can anybody answer to my simple question?

Karel Petranek
  • 15,005
  • 4
  • 44
  • 68
Darek
  • 1
  • 1
  • What are you trying to do? You shouldn't be surprised there's an infinite loop: you used `for(;;)` and have no breaks from it. In any case, you might want to check out an [introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) so you can learn the language properly; your C knowledge will only prove useful for a small subset of C++ *syntax*, and probably not useful at all for idiomatic and correct C++ coding. – GManNickG Mar 14 '11 at 21:07
  • I know that thre is no break... I just wanted to use it for test I know how to handle break from loop, it's not the topic. – Darek Mar 14 '11 at 21:08
  • What compiler and OS are you using? This works fine (printing only one Hello at the beginning) for me on Windows and Visual C++. The problem is probably that your OS/running environment doesn't clear standard input when running the program thus making the first getline return immediately. Without more info I can't say that for sure though. – Karel Petranek Mar 14 '11 at 21:08
  • OS Win XP SP2 Compiler: Visual C++ 2008 EE – Darek Mar 14 '11 at 21:09
  • @Darek: Sorry, I guess I'm just confused with what you were expecting and what you got. Can you state what your high-level goal is? – GManNickG Mar 14 '11 at 21:09
  • It's just beggining of multi-thread server and I can send one message to server and get respond to client but I added this infinite loop so I could write more than just one line before program exits. It's probably related to clearing input like dark_charlie mentioned. – Darek Mar 14 '11 at 21:11

1 Answers1

3

std::cin leaves the terminator in the stream (be it a space or a newline). So if you do:

std::string foo, bar;
std::cin >> foo;
std::getline(std::cin, bar);

with input as:

Hello
World

bar will end up with the empty string, not "World", because getline will stop at the first newline, which is the one after "Hello".

To bring this back to your question, if you have instances of std::cin >> before the start of your loop, you may have a stray newline, making the first iteration behave in unexpected ways. You can use std::cin.ignore to ignore any newlines left in the stream.

Eugene Talagrand
  • 2,214
  • 1
  • 14
  • 16