Should I be making a habit of using cout?

Posted 01 October 2010 - 08:36 AM

Hi,

I'm going to school for programming and have very basic programming classes. I am learning on my own as well.

So the question is: "Is using cout for outputs standard in the real world, should I be making a habit of using it and if not what is the standard used mostly? If you could link me to information on what else to use or should use that would be greatful. Thanks.

Replies To: Should I be making a habit of using cout?

Re: Should I be making a habit of using cout?

Posted 01 October 2010 - 09:22 AM

cout writes to the console, i.e. standard output. If you're using a GUI, output methods will differ basd on the framework and target control.

You will likely start with console programming, so there's nothing wrong with cout. If you want to get "fancy", you can use cerr, i.e., standard error, to accomplish the same thing for printing errors. Standard error is (I believe) not buffered by default, which means data is immediately sent to the stream, rather the held until flushed either by you or the operating system.

Re: Should I be making a habit of using cout?

Posted 01 October 2010 - 09:36 AM

What I mean by target control, by the way, is the control in the graphic interface. Writing to a textbox is different to writing to a listbox; in the latter case it usually involves adding the data you want to output to a list, and the listbox itself renders the text. With a textbox you usually just set the textbox's value to the string you want to display.

Example frameworks include Qt, wxWidgets, and Windows Forms, the latter using Microsoft's C++/CLI, the .NET implementation of C++, which is somewhat different in some ways from standard C++.

Re: Should I be making a habit of using cout?

Posted 01 October 2010 - 10:08 AM

It is most definitely not a "bad habit" to get into as the standard IO streams are considered "standard" for a reason. cin/cout are not just used for console IO either. For example they are often mapped to sockets in web programming, they are pretty general ways to feed data into and get data from a program.

As a C++ programmer it would be a "bad habit" to use scanf/printf over cin/cout but I really can't think of many reasons why you would not want to be very familiar with cin/cout usage.

This is kind of a base idea in C++ -- all of the C++ standard library IOStreams work like cin/cout. So for example you can develop a function that is supposed to write output to a file, but you can do all of your testing using cout, and then only have to change which output stream you give it (i.e. you don't have to change any of the internal structure.)

Here is an example that writes a list of prime numbers to both a file and cout. Note two things:
#1 this is not a wonderful example of how to generate a list of primes, there are much faster methods.
#2 The line 24 is not a cout line, but it has the same syntax and used the same techniques.

So the program is a little more general than just cout, I could also easily send the output to a socket or over a serial port etc, and I would never have to change the function "outputPrimes" to do so.

So learning to use cout is teaching you how to use C++ i/o streams, which later can be used for all kinds of things.