473,768 Members | 7,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3508

"Gurikar" <ms*******@gmai l.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(othe r_buffer);

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

"ben" <be******@hotma il.com> wrote in message
news:42******** **************@ news.optusnet.c om.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******@hotma il.com> wrote in message
news:42******** **************@ news.optusnet.c om.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_trait s<char_t> >
class null_outbuf
: public std::basic_stre ambuf<char_t, traits>
{
protected:
virtual int_type overflow(int_ty pe c)
{
// noop
return traits::not_eof (c);
}
};

int main()
{
null_outbuf<cha r> 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(con sole 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_trait s<char_t> >
class null_outbuf
: public std::basic_stre ambuf<char_t, traits>
{
protected:
virtual int_type overflow(int_ty pe c)
{
// noop
return traits::not_eof (c);
}
};

int main()
{
null_outbuf<cha r> 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*******@gmai l.com> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.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<typena me 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.d k...
"Gurikar" <ms*******@gmai l.com> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.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<typena me T>
logger& operator << (T t)


That should probably be:

template<typena me 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
1501
by: qazmlp | last post by:
I hope comp.lang.c will not find the following question as a complete off-topic. I would like to remove ie.comment out the 'cout' statements during compilation(actually preprocessing) time. The statements like this: cout<<"something\n" ; should be made as
6
21067
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use. What the code does: * First writes "This is sent to prompt" to prompt.
5
4006
by: Mastupristi | last post by:
I want to obtain the c++ equivalent of: unsigned short us = 347; printf("0x%04hX",us); that outputs "0x015B" I ried with: cout.setf(ios_base::hex,ios_base::basefield);
7
7573
by: Joe C | last post by:
I'd like to have better control of text output to the console. I'm using Windows and have a little (non-standard) function to help me out: #include <windows.h> void locate(int x, int y ) { COORD cur = { x, y }; SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur ); }
16
3993
by: mrDumbass | last post by:
#include <iostream> #include <algorithm> #include <string> #include <fstream> using namespace std; /* i would like to keep the numbers of the output : Power , step etc, on the same place in the console. But this isnt going to work with
2
8865
by: Generic Usenet Account | last post by:
What exactly is the difference between the hex manipulator and the following statement: cout.setf(ios_base::hex)? According to Stroustrup, Third Edition, Section 21.4.4, "once set, a base is used until reset". For some reason, I interpreted this to mean that "<< hex" manipulator would cause the base to be set for only the current statement, while the cout.setf(ios_base::hex) statement would cause the base to be set for multiple...
5
3038
by: Peteroid | last post by:
I'm doing a managed C++ application. Is there a way to re-direct 'cout' to output destinations other than the 'black console' that comes up? Specifically, re-direction to a text file? Thanks in advance! : )
3
2551
by: shk253 | last post by:
Hi all - I'm trying to generate a payroll. I'm having trouble with the cout statements overwriting the previous inputs, e.g. enter info for employe 1: blah blah blah. Enter info for employee 2: blah blah blah... When I generate the report, only information for employee 2 appears. I can see why this would happen from looking at my code... I would appreciate it if someone could let me know how to modify the code so that I could print out the...
58
4850
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better" style and what the pros and cons are (I'm using cout as example, but it really applies to any similar construct): 1) using std::cout;
0
9577
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9412
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10185
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10022
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9969
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7390
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6659
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5289
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3938
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.