cout

Can cout alter variables somehow?

So I have a function that looks something like this: float function(){ float x = SomeValue; return x / SomeOtherValue; } At some point, this function overflows and returns a really large negative value. To try and track down exactly where this was happening, I added a cout statement so that the function looked like this: flo...

Correctly over-loading a stringbuf to replace cout in a MATLAB mex file

MathWorks currently doesn't allow you to use cout from a mex file when the MATLAB desktop is open because they have redirected stdout. Their current workaround is providing a function, mexPrintf, that they request you use instead. After googling around a bit, I think that it's possible to extend the std::stringbuf class to do what I ne...

Assigning cout to a variable name

In ANSI C++, how can I assign the cout stream to a variable name? What I want to do is, if the user has specified an output file name, I send output there, otherwise, send it to the screen. So something like: ofstream outFile; if (outFileRequested) outFile.open("foo.txt", ios::out); else outFile = cout; // Will not compile bec...

c++ cout hex values?

I want to do: int a = 255; cout << a; and have it show FF in the output, how would i do this? ...

c++ cout autocase strings?

Is it possible to do something like cout << "my string"; and have my string capitalized? from what i can tell there is no way to do it? i need to wrap it around a function ...

How do I print a double value with full precision using cout?

So I've gotten the answer to my last question (I don't know why I didn't think of that). I was printing a double using cout that got rounded when I wasn't expecting it. How can I make cout print a double using full precision? Thanks ...

c++ outputting and inputting a single character

I am writing a program in c++ which implements a doubly-linked list that holds a single character in each node. I am inserting characters via the append function: doubly_linked_list adam; adam.append('a'); This function is implemented as follows: //Append node node* append(const item c){ //If the list is not empty... ...

the buffer and output sequence of cout and printf

I know cout and printf have buffer today, and it is said that the buffer is some like a stack and get the output of cout and printf from right to left, then put them out(to the console or file)from top to bottem. Like this, a = 1; b = 2; c = 3; cout<<a<<b<<c<<endl; buffer:|3|2|1|<- (take “<-” as a poniter) output:|3|2|<- (output ...

How the buffer of cout work?

I know that cout have buffer several days ago, and when I google it, it is said that the buffer is some like a stack and get the output of cout and printf from right to left, then put them out(to the console or file)from top to bottem. Like this, a = 1; b = 2; c = 3; cout<<a<<b<<c<<endl; buffer:|3|2|1|<- (take “<-” as a poniter) outp...

Is there any difference between +-ing strings and <<-ing strings in c++?

What is the difference, if any between the effects of the following snippets: cout << "Some text" << s1 << "some more text\n"; cout << "Some text" + s1 + "some more text\n"; ...

C++: First element of vector "corrupting"

I have a class (foo) that contains a vector. If i try iterating over the elements in the vector like so: for(vector<random>::iterator it = foo.getVector().begin(); it != foo.getVector().end(); ++it) { cout << (*it) << endl; } The first element is always corrupted and returns garbage data. However, if do something like: ...

What is the reverse of cout.width? (C++)

I was trying std::cout.width(int) to see what it did, and it pushes the text right to fill a minimum width: TH becomes: TH to fill a minimum width of 10. I am wondering if A) there is a way to reverse this, have a number of spaces put AFTER the text to fill a minimum width, and B) is there a way to create a maximum width AN...

Why is the beginning of my string disappearing?

In the following C++ code, I realised that gcount() was returning a larger number than I wanted, because getline() consumes the final newline character but doesn't send it to the input stream. What I still don't understand is the program's output, though. For input "Test\n", why do I get " est\n"? How come my mistake affects the first...

How to print '\n' instead of a newline?

I am writing a program that uses prints a hex dump of its input. However, I'm running into problems when newlines, tabs, etc are passed in and destroying my output formatting. How can I use printf (or cout I guess) to print '\n' instead of printing an actual newline? Do I just need to do some manual parsing for this? EDIT: I'm receivin...

"Ch++" or "ch+1" in C++?

Hello. While reading "C++ Primer Plus 5th edition", I saw this piece of code: cin.get(ch); ++ch; cout << ch; So, this will lead to display the following character after ch. But, If I did it that way: cin.get(ch); cout << ch+1; Now, cout will think ch is an int(try typecasting). So, why cout does so? And why if I...

Creating a cout function in C?

This is a totally hypothetical question, but I have to know the answer. I assume most C++ compilers are written in assembly. Which makes them different languages entirely (I could be wrong). That being said if I were going to create a cout style function for plain old C, how would I do it? cout has some very impressive features take thi...

What is a 'Stream', relating to cin and cout?

Hi, I'm new to C++. A tutorial is talking about cin and cout: "Syntactically these streams are not used as functions: instead, data are written to streams or read from them using the operators <<, called the insertion operator and >>, called the extraction operator." What is a 'stream'? ...

C++ : Unbuffered output with cout

How can you get unbuffered output from cout, so that it instantly writes to the console without the need to flush (similar to cerr)? I thought it could be done through rdbuf()->pubsetbuf, but this doesn't seem to work. The following code snippet below is supposed to immediately output to the console, and then wait a few seconds. But i...

cancelling std::cout code lines using preprocessor

One can remove all calls to printf() using #define printf. What if I have a lot of debug prints like std::cout << x << endl; ? How can I quickly switch off cout << statements in a single file using preprocessor? ...

How can I indent cout output?

I'm trying to print binary tree void print_tree(Node * root,int level ) { if (root!=NULL) { cout<< root->value << endl; } //... } How can I indent output in order to indent each value with level '-' chars. ...