cout

How can I print a string to the console at specific coordinates in C++?

Hello, I'm trying to print characters in the console at specified coordinates. Up to now I have been using the very ugly printf("\033[%d;%dH%s\n", 2, 2, "str"); But I just had to ask whether C++ had any other way of doing this. The problem is not even that it's ugly, the problem comes up when I try to make myself a prettier function lik...

C++ ofstream vs. C++ cout piped to file

I'm writing a set of unit tests that write calculated values out to files. Each test produces a square matrix that holds anywhere from 50,000 to 500,000 doubles, and I have a total of 128 combinations of test cases. Is there any significant overhead involved in writing cout statements and then piping that output to files, or would I be...

How can I pad an int with leading zeros when using cout << operator?

I want cout to output an int with leading zeros, so the value 1 would be printed as 001 and the value 25 printed as 025 .. you get the idea .. how can I do this? Thanks. ...

Display message in windows dialogue box using "cout" - C++

Can a windows message box be display using the cout syntax? I also need the command prompt window to be suppressed / hidden. There are ways to call the messagebox function and display text through its usage, but the main constraint here is that cout syntax must be used. cout << "message"; I was thinking of invoking the VB msgbox co...

Limit the precision on std::cout of default values in boost::options_description

When I construct a boost::options_description instance like options.add_options() ("double_val", value(&config.my_double)->default_value(0.2), "it's a double"); and later want to have the automated output of the options that are available for my program, and put std::cout << options << std::endl; the default value 0.2 is shown wi...

C++ cout cin string manipulation

I'm trying to get a line as input from the command line. My problem is that I'm not getting the whole line, but it's being tokenized by space. So if I entered something such as "I like Math a lot" instead of getting "you enterend: I like Math a lot" I get the follwoing: EDITING MODE: Enter a command i like Math a lot you entered i ...

C++ cout printing slowly

I noticed if I print out a long string(char*) using cout it seems to print 1 character at a time to the screen in Windows 7, Vista, and Linux(using putty) using Visual C++ 2008 on Windows and G++ on Linux. Printf is so much faster I actually switched from cout to printf for most printing in a project of mine. This is confusing me because...

Redirect std::cout to newly created console

When you create a C++ console application under Windows you automatically get the console window created for you and std::cout outputs to the console window. I have a GUI application for which I also want to create a console window. I can create the console window using the AllocConsole() function, but how do I redirect / attach std::c...

redirected cout -> std::stringstream, not seeing EOL

Hi all, I've read a bunch of posts regarding redirecting std::cout to stringstreams, but I'm having problem reading the redirected string. std::stringstream redirectStream; std::cout.rdbuf( redirectStream.rdbuf() ); std::cout << "Hello1\n"; std::cout << "Hello2\n"; while(std::getline(redirectStream, str)) { // This does not work - ...

mixing cout and printf for faster output

After performing some tests I noticed that printf is much faster than cout. I know that it's implementation dependent, but on my Linux box printf is 8x faster. So my idea is to mix the two printing methods: I want to use cout for simple prints, and I plan to use printf for producing huge outputs (typically in a loop). I think it's safe t...

Is it possible to refresh two lines of text at once using something like a CR? (C++)

Right now, I have a console application I'm working on, which is supposed to display and update information to the console at a given interval. The problem I'm having is that with a carriage return, I can only update one line of text at a time. If I use a newline, the old line can no longer be updated using a carriage return. What can I...

cout << order of call to functions it prints?

the following code: myQueue.enqueue('a'); myQueue.enqueue('b'); cout << myQueue.dequeue() << myQueue.dequeue(); prints "ba" to the console while: myQueue.enqueue('a'); myQueue.enqueue('b'); cout << myQueue.dequeue(); cout << myQueue.dequeue(); prints "ab" why is this? It seems as though cout is calling the outermost (closest to t...

Is it possible to cout to terminal while redirecting cout to outfile?

I'm running a program and redirecting cout to an outfile, like so: ./program < infile.in > outfile.o I want to be able to read in an option ('-h' or '--help') from the command line and output a help message to the terminal. Is there a way I can do this but still have the regular cout from the rest of the program go to the outfile?...

Printing an uninitialized bool using cout (C++)

I have a class with a bool data member that is not initialized by the constructor. If I do cout << x.myBoolDataMember; where x is an object of this class in which the bool has not been initialized, I sometimes get a random number rather than 0 or 1. (I'm using gcc.) Is this behavior compliant with the Standard? ...

Absolute positioning in printout with cout in C++ ?

How do you get "absolutely positioned" columns with cout, that leftaligns text and right-aligns numbers? #include <iostream> #include <iomanip> using namespace std; struct Human { char name[20]; char name2[20]; char name3[20]; double pts; }; int main() { int i; Human myHumen[3] = { {"Mr", "Alan", "Turin...

how do I write a logger class with cout style interface (logger << "Error: " << val << endl;)

I want to create a logger class such that with a functionality like this: Logger log; log << "Error: " << value << "seen" << endl; This should print me a custom formatted message. E.g. "12-09-2009 11:22:33 Error 5 seen" My simple class currently looks like this: class Logger { private: ostringstream oss; public: ...

How to determine the size of an array of strings in C++?

I'm trying to simply print out the values contained in an array. I have an array of strings called 'result'. I don't know exactly how big it is because it was automatically generated. From what I've read, you can determine the size of an array by doing this: sizeof(result)/sizeof(result[0]) Is this correct? Because for my program, ...

Unsticky a cout modifier?

cout << hex << 11 << endl; cout << 12 << endl; will print : a b If I cout 13, it will be printed as 'c'. How do I remove the hex modifier from now on so it would just print 13? This is probably simple but I tried looking for the answer elsewhere. Thanks. ...

Overloading << operator and recursion

Hi, I tried the following code: #include <iostream> using std::cout; using std::ostream; class X { public: friend ostream& operator<<(ostream &os, const X& obj) { cout << "hehe"; // comment this and infinite loop is gone return (os << obj); } }; int main() { X x; cout << x; return 0; }...

one question about iostream cout in C++

Hi In such code, what it is called, \\n like this? cout<<"Hello\\n \'world\'!"; What's the basic rule about such characters? ...