5 Replies - 547 Views - Last Post: 01 October 2010 - 10:21 PM Rate Topic: -----

#1 ThriceSenses  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-March 10

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.
Is This A Good Question/Topic? 0
  • +

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

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

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.
Was This Post Helpful? 1
  • +
  • -

#3 ThriceSenses  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-March 10

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

Posted 01 October 2010 - 09:29 AM

Yes, console programming is exactly what I am doing now. Thank you for bringing up framework and target control, I will have to research those as well as cerr. Again, thank you, this helps a lot.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

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++.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>

using namespace std;

int piFunctionAprox(int n) { return n / log((double)n); }

void outputPrimes(std::ostream& out, int max) {
  std::vector<int> primes;
  primes.reserve(piFunctionAprox(max));
  primes.push_back(2);
  for (int num = 3; num < max; num+=2) {
    int count = 0;
    for(int i = 0; i < primes.size(); ++i) {
      if (num % primes[i] == 0) { count++;  break; }
    }
    if (count == 0) { primes.push_back(num); }
  }

  //output the list of primes... 
  for(int i = 0; i < primes.size(); ++i) {
    out << setw(6) << primes[i] << ", ";
    if(i%10 == 9) { out << '\n'; }
  }
  out.flush();
}

int main() {
  ofstream outputFile("PrimesList100000.txt");
  if (outputFile) {
    outputPrimes(outputFile, 100000);
    outputPrimes(cout, 1000);
  } else {
    cerr << "Error opening file: PrimesList100000.txt" << endl;
  }

  return 0;
}


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.
Was This Post Helpful? 1
  • +
  • -

#6 ThriceSenses  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-March 10

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

Posted 01 October 2010 - 10:21 PM

@NickDMax

Thanks for the information. Some of it is over my head but I will be there eventually. Thank you for the sample code. Some of it makes senses. I have much to learn and look forward to it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1