You are not logged in.

#1 2011-06-25 23:11:19

Iknow
Member
Registered: 2010-11-26
Posts: 18
Website

Question about 'cin' & 'cout' (C++) (SOLVED)

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

#2 2011-06-25 23:20:10

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,324

Re: Question about 'cin' & 'cout' (C++) (SOLVED)

Iknow wrote:

...when i compile the following code (sorry i don't know how to box it off)

BBCode


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

#3 2011-06-25 23:28:10

KenjiTakahashi
Member
From: outside the universe
Registered: 2011-06-22
Posts: 24
Website

Re: Question about 'cin' & 'cout' (C++) (SOLVED)

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) smile.

Last edited by KenjiTakahashi (2011-06-25 23:32:00)

Offline

#4 2011-06-25 23:31:50

Iknow
Member
Registered: 2010-11-26
Posts: 18
Website

Re: Question about 'cin' & 'cout' (C++) (SOLVED)

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

#5 2011-06-25 23:37:23

KenjiTakahashi
Member
From: outside the universe
Registered: 2011-06-22
Posts: 24
Website

Re: Question about 'cin' & 'cout' (C++) (SOLVED)

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

#6 2011-06-25 23:38:50

Iknow
Member
Registered: 2010-11-26
Posts: 18
Website

Re: Question about 'cin' & 'cout' (C++) (SOLVED)

thank you KenjiTakahashi, marking as solved, sorry for wasting your time people!

Offline

#7 2011-06-25 23:40:48

KenjiTakahashi
Member
From: outside the universe
Registered: 2011-06-22
Posts: 24
Website

Re: Question about 'cin' & 'cout' (C++) (SOLVED)

Np., have to learn for abstract algebra exam next week, so I have a lot of time big_smile.

Offline

Board footer

Powered by FluxBB