Computer Science Canada

Can't use "cout" in seperate functions

Author:  sheepo39 [ Fri Dec 11, 2009 5:15 pm ]
Post subject:  Can't use "cout" in seperate functions

I'm a beginning C++ programmer, and I'm reading a book on it and I'm at the part with declaring functions. I understand the whole concept of it and all, but I don't understand why you can't use "cout" instead of "return" to output a value.

Like

float bodyTempC()
{
float temperature = 37.0 ;
return temperature ;
}

void bodyTempF()
{
float temperature = 98.6 ;
cout << temperature ;
}

instead of

float bodyTempC()
{
float temperature = 37.0 ;
return temperature ;
}

float bodyTempF()
{
float temperature = 98.6 ;
return temperature ;
}

Doing the first will return an error, and won't compile, why is this, I'm fairly confused.

Author:  Tony [ Fri Dec 11, 2009 5:19 pm ]
Post subject:  RE:Can\'t use "cout" in seperate functions

Your compiler will tell you why it doesn't compile. Probably because there is no cout. Did you mean std::cout ?

Author:  HRI [ Wed May 05, 2010 7:50 pm ]
Post subject:  Re: Can't use "cout" in seperate functions

Be sure to put in '#include <iostream>' at the start of the file. Also, the way you're using it, 'using std::cout;' would be a logical statement after the first. That way, you need only type cout rather than std::cout.


: