question about cin

K

Key9

Hi all

This is an simple app : read cin and print to cout
I want it's action like "cat" with out arguement.


#include <iostream>
#include <string>

using namespace std;

int main(int argc, int *argv[])
{
string str;
while( cin >> str ){
cout << str << endl;
}
}


$./test
This is key hit by keyboard input <return>
This
is
key
hit
by
keyboard
input

but if I change cout << str << endl; to cout << str;

$ ./test
This is key hit by keyboard input
Thisiskeyhitbykeyboardinput


how to solve this problem?


Thanks

key9
 
R

Ron Natalie

Key9 said:
Hi all

This is an simple app : read cin and print to cout
I want it's action like "cat" with out arguement.


#include <iostream>
#include <string>

using namespace std;

int main(int argc, int *argv[])
{
string str;
while( cin >> str ){
cout << str << endl;
}
}
endl is almost never the best idea. It puts a new line into the
stream and a flush.

The first problem is that cin >> str reads up to (but ignores)
whitespace. This is why when you output it everything gets
run togther.

You can use
getline(cin, str);
to read an entire line

But if you're trying to bulk move things there are more
efficient ways.

What exactly do you wish to accomplish?
 
R

Ron Natalie

Key9 said:
Hi all

This is an simple app : read cin and print to cout
I want it's action like "cat" with out arguement.


#include <iostream>
#include <string>

using namespace std;

int main(int argc, int *argv[])
{
string str;
while( cin >> str ){
cout << str << endl;
}
}
endl is almost never the best idea. It puts a new line into the
stream and a flush.

The first problem is that cin >> str reads up to (but ignores)
whitespace. This is why when you output it everything gets
run togther.

You can use
getline(cin, str);
to read an entire line

But if you're trying to bulk move things there are more
efficient ways.

What exactly do you wish to accomplish?
 
K

Key9

What exactly do you wish to accomplish?

For example I defined an class
The class can express stored in a line which using ascii code to express,
maybe take DNA sequence for example(just an idea- class DNA)


ATGCATGCATCG......<NL> an line stand for an instance.
ATGCATGCATCG......<NL>
EOL


I want using a single loop to read them to the class's instance
maybe we can have an overview of pesudo code , ignore the error here please.

#include "DNA.h"
#include "iostream"

.... someother code main etc ...


vector<DNA> v_dna;

while( <read to eof>){

DNA* p_dna = new DNA;
cin >> p_dna;
v_dna.pushback(*p_dna);
}

....
To here we have completed the data transform , from text 2 instance.

any ideas? sorry for my c++ experience.
 
R

Ron Natalie

Key9 said:
DNA* p_dna = new DNA;

This is NOT Java, one does not need to "new" everything in C++.
DNA dna;
cin >> p_dna;

cin >> dna; // presuming class DNA has an overload.

Your code leaks memory all over the place because it repeatedly
allocates DNA objects and discards them.
 
B

BobR

Ron Natalie wrote in message
Key9 said:
Hi all
This is an simple app : read cin and print to cout
I want it's action like "cat" with out arguement.

#include <iostream>
#include <string>
using namespace std;

int main(int argc, int *argv[]){
string str;
while( cin >> str ){
cout << str << endl;
}
}
endl is almost never the best idea. It puts a new line into the
stream and a flush.

The first problem is that cin >> str reads up to (but ignores)
whitespace. This is why when you output it everything gets
run togther.

You can use
getline(cin, str);
to read an entire line

But if you're trying to bulk move things there are more
efficient ways.

What exactly do you wish to accomplish?

Probably <G>:

std::cout << std::cin.rdbuf();
 
R

Ron Natalie

BobR said:
Probably <G>:

std::cout << std::cin.rdbuf();
Possibly, but I didn't want to suggest that (or unformatted I/O)
until I got an idea just what he was trying to accomplish.
 
K

Key9

Your code leaks memory all over the place because it repeatedly
allocates DNA objects and discards them.

yes I know the code have serious problem ,
but I have already said ignore these expression and grammar errors .
I just want the main idea how to implement.
and the rest ,I think it's just matter of debug code.

So finally we have 3 questions:

1. can I using "cin >> string;" or something alike expression to get the
same behavior as "getline(cin, str);"?

2. how I can using " string >> class; " to achieve setting instance's
member?

3. how about directly using "cin >> class;"
 
K

Key9

std::cout << std::cin.rdbuf();
OK see code

#include <iostream>
using namespace std;

int main(int argc, int *argv[])
{
while( std::cout << std::cin.rdbuf() ){
}
}


the code works right , but bad news is I have to press ctrl+d twice then I
want to end the app.
 
B

BobR

Key9 wrote in message ...
So finally we have 3 questions:

1. can I using "cin >> string;" or something alike expression to get the
same behavior as "getline(cin, str);"?

{
using std::cout; // for NG post
// --- simulate std::cin ---
std::istringstream icin("29 This is a string\n 08 12345");
int num1(0);
icin >> num1;
std::string thestring;
// icin >> thestring; // out: num1=29 thestring=This
icin.ignore(1); // skip space
getline( icin, thestring, '\n' );
// out: num1=29 thestring= This is a string
int num2(0);
icin >> num2;
cout <<"num1="<<num1<<" thestring="<<thestring
<<" num2="<<num2<<std::endl;
// out: num1=29 thestring=This is a string num2=8
}
2. how I can using " string >> class; " to achieve setting instance's
member?

void SetString( std::string const &sstr ){ MyClassString = sstr; }
3. how about directly using "cin >> class;"

You overload the 'istream operator>>'.

class MyClass{
std::string MyClassString;

friend std::istream& operator>>( std::istream &in, MyClass &mc ){
in >> mc.MyClassString;
if( mc.MyClassString.empty() )
is.setstate( ios::failbit );
return in;
} // istream&>>
}; // class MyClass

int main(){
MyClass Mc;
cin >> Mc;

std::ifstream data("mydata.txt");
data >> Mc;

std::string str("This is a string");
std::istringstream scin( str );
scin >> Mc; // inputs 'This' ( unless 'getline' used.)

return 0;
}
 
R

Ron Natalie

Key9 said:
1. can I using "cin >> string;" or something alike expression to get the
same behavior as "getline(cin, str);"?

You can not. Use getline.
2. how I can using " string >> class; " to achieve setting instance's
member?

I suppose you can define
operator>>(std::string, YourClass)
but that's kind of counter the way C++ is usually defined. You
would define operator>> for istream and your class and operator<<
for ostream. Then you can use them for any stream (cin, fstreams,
and if you needed to use a string, a stringstream).
3. how about directly using "cin >> class;"

Now your talking. Overload them like this:

std::istream& operator>>(std::istream& is, MyClass& mc) {
// define things that read into the object such as:

is >> mc.member1;
is >> mc.member2;

// Return the stream so that you can chain things
// together such as
// cin >> a >> b >> c
//
return is;
}

Now you can do
MyClass a;
cin >> a;

Similarly, for output:
std::eek:stream& operator<<(std::eek:stream& os, const MyClass& mc) {
os << mc.member1;
os << mc.member2;
return os;
}
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Key9 said:
1. can I using "cin >> string;" or something alike expression to get the
same behavior as "getline(cin, str);"?

You can use a helper class with an operator >> , like the following:

#include <iostream>
#include <string>

class GetLine
{
public:
GetLine (std::string & get_to) :
s (get_to)
{ }
private:
std::string & s;
friend std::istream & operator >>
(std::istream & is, const GetLine & gl);
};


std::istream & operator >> (std::istream & is, const GetLine & gl)
{
getline (is, gl.s);
return is;
}



int main ()
{
std::string s;
std::cout << "Enter a line of text: " << std::flush;
std::cin >> GetLine (s);
std::cout << "You entered: '" << s << "'\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top