You are not logged in.
Probably a stupid question with a simple answer but here goes;
when i compile the following code
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
string buffer;
while (1 == 1)
{
cout<<" ## ";
cin>>buffer;
}
}
it compiles fine, when run it does the following
prints " ## " minus quotes
then pauses for input
however the strange thing is that if i enter a string with a space in, quotes or no, it repeats the 'cout' statement, see below for output
## this is a string
## ## ## ##
then pauses for input, as it should, i ask you why it does this as i don't call it "expected behaviour", not with that code anyway.
EDIT: fixed some typos
EDIT_2: boxed of output&code, thx ewaller
Last edited by Iknow (2011-06-25 23:39:13)
Offline
...when i compile the following code (sorry i don't know how to box it off)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
cin reads input til the first white space (which is just a space here) and... that's it I think.
If you want to read the whole line, use getline function.
Oh, btw. you can just do while(1) .
Last edited by KenjiTakahashi (2011-06-25 23:32:00)
Offline
but why would that cause 'cout' to repeat?
'cin' only taking part of the string shouldn't cause an unintentional loop that isn't in the code, should it?
Offline
Nah, there is a loop, right?
You type "this is a string".
cin reads "this" and gets out.
while(1 == 1) is tested and gets in (of course).
cout prints " ## ". (*)
cin reads "is" and gets out.
etc.
(*) The thing why it gets "mixed" (I mean you type the whole string and then ## * 4 appears) is probably because terminal is flushing (and thus refreshing) stdout on Enter hit only.
Offline
thank you KenjiTakahashi, marking as solved, sorry for wasting your time people!
Offline