UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » something odd about input/output

#1 2004-02-24 01:51 AM

capacman
Member
Registered: 2004-02-24
Posts: 2

Re: something odd about input/output

hi everyone.This the first time that i encountered such thing.The code that i sent is just an example to show what it is.
code:
#include <iostream>
#include <string>
using namespace std;

int main()
{

        string a;
        while(1)
        {
                cin>>a;
                cout<<a<<a.length()<<endl;
        }
        return 0;
}

when i compile and run this i get such an output
output:
hello world   //this is the input i write
hello5
world5
h e l l o
h1
e1
l1
l1
o1

every word taken as if it has an eol.Can someone  explain what is wrong.
Also same thing happened when write it withc functions scanf and printf.thank you for your helps in advance.

Offline

#2 2004-02-24 04:53 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: something odd about input/output

The main problem with your code is that you specify a string as target. A string can be all, from 1 character up to a whole sentence. If you intent to read all chars until the end of the line with it you'd have to say that cin too. For example using cin.getline(buffer, buffersize,'\n'). The same goes for scanf, you have to include the new line character in the input format string like "%s\n" to really force it to wait until the end of line is reached.

Offline

#3 2004-02-24 08:12 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: something odd about input/output

Offline

  • Index
  • » C++
  • » something odd about input/output

Board footer

Powered by FluxBB