Assigning cout() to a variable

Mar 27, 2014 at 10:50pm

I am attempting to assign a cout statement to a (class member)variable with great difficulty. I would be grateful for any assistance available.

I am using the Dev C++ 4.9.9.2 compiler

1
2
3

  rank.First = cout <<weight << ("  pounds" )<< barSize << ("mm  Diameter Bars.");

Mar 27, 2014 at 10:59pm
Sorry but what are you trying to do? If you was able to assign it to a variable what would the variable contain or be able to do?
Mar 27, 2014 at 10:59pm
What is the type of rank.First?
cout is an object of type iostream.
What is it that you expect to be assigned?

You might try using stringstream:
http://www.cplusplus.com/reference/sstream/stringstream/

1
2
3
4
5
6
7
#include <sstream>

stringstream ss;
string temp;

ss <<weight << ("  pounds" )<< barSize << ("mm  Diameter Bars.");
temp = ss.str();





Mar 27, 2014 at 11:05pm
cout is an object of type iostream.

ostream

If it was iostream there would be no need for cin since it would be input and output.
Last edited on Mar 27, 2014 at 11:06pm
Mar 27, 2014 at 11:06pm
std::cout is of type std::ostream.
http://www.cplusplus.com/reference/iostream/cout/

Also, std::ostream is a move-only type, so any assigning will either use a reference or a pointer to std::cout.
Mar 27, 2014 at 11:11pm
giblit wrote:
If it was iostream there would be no need for cin since it would be input and output.
http://ideone.com/t9BGIS if only it worked...
Mar 27, 2014 at 11:24pm
That didn't prove anything. You used cout(ostream) for output and cin(istream) for input with an iostream object.

What I was saying anyways is that you can't do this:
1
2
std::cout << "enter age: ";
std::cout >> age;
Last edited on Mar 27, 2014 at 11:29pm
Mar 28, 2014 at 12:36am
giblit wrote:
That didn't prove anything. You used cout(ostream) for output and cin(istream) for input with an iostream object.
I wasn't trying to prove anything. And no, I used the underlying stream buffers from cout and cin, I did not use cout and cin.
Mar 28, 2014 at 4:30pm
giblit wrote:

cout is an object of type iostream.

ostream

That was a typo on my part. I had stringstream on the brain.
Last edited on Mar 28, 2014 at 4:52pm
Topic archived. No new replies allowed.