472,139 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

cout

How to make cout not printing on the console. i mean

void main()
{

cout<<"Hello world"<<endl;
}
Is there any way where i can block printing on console even when iam
cout in my code..I mean even using cout<<"Hello world"<<endl;.
This is shouldnt get printed on console..

regards

Jul 23 '05 #1
10 3316

"Gurikar" <ms*******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
How to make cout not printing on the console. i mean

void main()
int main()
{

cout<<"Hello world"<<endl;
}
Is there any way where i can block printing on console even when iam
cout in my code..I mean even using cout<<"Hello world"<<endl;.
This is shouldnt get printed on console..

regards


If you prefer not to print to the console, then don't print to the console.
The string "Hello world" is a literal. Its not a variable. To store the
literal string, use it to initialize a std::string variable.

#include <iostream>
#include <string>

int main()
{
std::string s("Just a string.\n");
s += "Just another string.\n";

// std::cout<< s << std::endl;

return 0;
}

Homework:
Whats a variable?
Whats a literal string?
Why is void main() not allowed?

Jul 23 '05 #2
ben
What's wrong of printing straight from string literal? The user just wants
to ask how to redirect cout to somewhere else.

So the answer is to use std::ios::rdbuf() function. A google search would
give plausible many examples :)

ben
If you prefer not to print to the console, then don't print to the console. The string "Hello world" is a literal. Its not a variable. To store the
literal string, use it to initialize a std::string variable.

Jul 23 '05 #3
Gurikar on 2005-05-12 09:10 spammed:
How to make cout not printing on the console. i mean

cout.rdbuf(other_buffer);

--
SirMike
the code is my strength
http://www.sirmike.grudziadz.com
Jul 23 '05 #4

"ben" <be******@hotmail.com> wrote in message
news:42**********************@news.optusnet.com.au ...
What's wrong of printing straight from string literal? The user just wants
to ask how to redirect cout to somewhere else.

So the answer is to use std::ios::rdbuf() function. A google search would
give plausible many examples :)

ben


Ben,

please place your responses either at the bottom of the post, or
interspersed with the text you're commenting on. Putting your comments at
the top is referred to as "top posting", and is frowned upon in usenet,
because it disrupts the normal order of reading (from top to bottom).

Thanks,
Howard


Jul 23 '05 #5

"ben" <be******@hotmail.com> wrote in message
news:42**********************@news.optusnet.com.au ...
What's wrong of printing straight from string literal? The user just wants
to ask how to redirect cout to somewhere else.

So the answer is to use std::ios::rdbuf() function. A google search would
give plausible many examples :)

ben


Nothing wrong with printing from a literal and you have graciously offered
ios::rdbuf() in the case the OP is indeed trying to redirect std::cout.
In my opinion, thats not the case here. But thats just a guess.
Lets see what the feedback says...

Thanks for the input.
Jul 23 '05 #6
>How to make cout not printing on the console.

Where do you want it to output character?
i mean

void main()
main() returns an int, always.

int main(){
cout<<"Hello world"<<endl;
}
I'll just rewrite that example to make sure you
start correctly.

# include <iostream>

int main()
{
std::cout << "Hello world" << std::endl;
}
Is there any way where i can block printing on
console even when iam cout in my code..I mean even
using cout<<"Hello world"<<endl;. This is
shouldnt get printed on console..


Where should it get printed? If it's another
stream, many have pointed the std::ios::rdbuf()
solution. If it's to another graphical mean such a a
graphical window or whatever, you will have to use
operating-system-dependent functions.

If you want to completely "block printing on
console", do something like

# include <iostream>

template <
class char_t,
class traits = std::char_traits<char_t> >
class null_outbuf
: public std::basic_streambuf<char_t, traits>
{
protected:
virtual int_type overflow(int_type c)
{
// noop
return traits::not_eof(c);
}
};

int main()
{
null_outbuf<char> no;

std::cout << "hello";

std::streambuf *old = std::cout.rdbuf(&no);
std::cout << "this is not printed";
std::cout.rdbuf(old);

std::cout << "I'm back";
}
--
Jonathan
[FAQ] - http://www.parashift.com/c++-faq-lite/

Jul 23 '05 #7
HI,
Actually i want print log information on console. So i use cout,
incase if i dont want log, i dont want to change the code, so i just
want cout not print on the console without changing any code. Its
something like this:

void log(char* str)
{
cout<<str<<endl;
}
int main()
{

bool b = true;

if(b)
{
log("Log is on");
}
log("hello");
log("world");
....
....
...
....
log("End");
}

So when bool b = true, it prints on console, now when bool b = false, i
dont want print it on console, how do i do this? I dont want change the
code of calling log eveywhere. something like i want make cout = NULL
when bool b = false. Is this possible.
One more question, what happends i use cout many times in windows based
application(console not present). Does it affect performance.
Regards
Jonathan Mcdougall wrote:
How to make cout not printing on the console.


Where do you want it to output character?
i mean

void main()


main() returns an int, always.

int main()
{
cout<<"Hello world"<<endl;
}


I'll just rewrite that example to make sure you
start correctly.

# include <iostream>

int main()
{
std::cout << "Hello world" << std::endl;
}
Is there any way where i can block printing on
console even when iam cout in my code..I mean even
using cout<<"Hello world"<<endl;. This is
shouldnt get printed on console..


Where should it get printed? If it's another
stream, many have pointed the std::ios::rdbuf()
solution. If it's to another graphical mean such a a
graphical window or whatever, you will have to use
operating-system-dependent functions.

If you want to completely "block printing on
console", do something like

# include <iostream>

template <
class char_t,
class traits = std::char_traits<char_t> >
class null_outbuf
: public std::basic_streambuf<char_t, traits>
{
protected:
virtual int_type overflow(int_type c)
{
// noop
return traits::not_eof(c);
}
};

int main()
{
null_outbuf<char> no;

std::cout << "hello";

std::streambuf *old = std::cout.rdbuf(&no);
std::cout << "this is not printed";
std::cout.rdbuf(old);

std::cout << "I'm back";
}
--
Jonathan
[FAQ] - http://www.parashift.com/c++-faq-lite/


Jul 23 '05 #8
"Gurikar" <ms*******@gmail.com> wrote in message news:11**********************@f14g2000cwb.googlegr oups.com...
HI,
Actually i want print log information on console. So i use cout,
incase if i dont want log, i dont want to change the code, so i just
want cout not print on the console without changing any code. Its
something like this:

[...]


#include <iostream>

class logger
{
public:
bool on;

template<typename T>
logger& operator << (T t)
{
if (on) std::cout << t; // else do nothing
return *this;
}
};

int main()
{
logger log;

log.on = true;
log << "should display to cout";

log.on = false;
log << "should not display";

return 0;
}

HTH,

--
Lionel B

Jul 23 '05 #9
"Lionel B" <me@privacy.net> wrote in message news:42***********************@news.sunsite.dk...
"Gurikar" <ms*******@gmail.com> wrote in message news:11**********************@f14g2000cwb.googlegr oups.com...
HI,
Actually i want print log information on console. So i use cout,
incase if i dont want log, i dont want to change the code, so i just
want cout not print on the console without changing any code. Its
something like this:

[...]
#include <iostream>

class logger
{
public:
bool on;

template<typename T>
logger& operator << (T t)


That should probably be:

template<typename T>
logger& operator << (const T& t)
{
if (on) std::cout << t; // else do nothing
return *this;
}
};

int main()
{
logger log;

log.on = true;
log << "should display to cout";

log.on = false;
log << "should not display";

return 0;
}

HTH,

--
Lionel B

Jul 23 '05 #10
Hi,

The answer to second part of the question...about cout's performance
issue...
...yes...writing too much on console causes a performace degradation.
You can check this by having a mechanism of enabling and disabling all
the couts in your program....oooops another problem!!....suggest to use
compiler macros for doing this.

-vs_p...

Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Omid | last post: by
5 posts views Thread by Mastupristi | last post: by
7 posts views Thread by Joe C | last post: by
16 posts views Thread by mrDumbass | last post: by
2 posts views Thread by Generic Usenet Account | last post: by
5 posts views Thread by Peteroid | last post: by
58 posts views Thread by Mark Casternoff | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.