C++ Formatting cout Primer Tutorial

C++ Formatting cout Primer Tutorial

C++ Formatting cout Primer Tutorial

C++ uses iostream.h methods to format command line output. Programmers with a background in C may think the iostream.h “cout” stream output methods are a little harder to use (compared to C’s printf and sprintf and fprintf (which, of course, you can still use if you wish)), and this may be true. However if you add iomanip.h into the equation it gets easier.

Today, inspired by a Yahoo Answers question (thanks) we take an introductory look at “cout” and the use of:

  • cout.precision([numberSignificantNumbers])
  • setw([colWidth]) // via iomanip.h
  • endl

Here is the C++ code (with comments) below within the Xcode IDE … project called “cout_formatting” … source code called “main.cpp” … show logarithmic table …


// cout_formatting.cpp
// Format a logarithmic table to standard output via cout and ...
// Also use:
// cout.precision([numberSignificantNumbers])
// setw([colWidth])
// endl

using namespace std;
#include "iostream.h" // needed for cout and cout.precision (and cin)
#include "iomanip.h" // needed for setw()
#include "math.h" // needed for log() method

int main() {
int rows, columns, n, i, j; // rows=number of base values in table, columns=number of log values in table
double base1 = 7.0, base2; // base1 is log value, base2 is base value ... double values
cout << "Log values program" << endl << endl; // Introduce ...
cout << "Enter start logarithm value: "; // Prompt and accept base2 ...
cin >> base2;
cout << "Enter how many logarithm values : "; // Prompt and accept rows ...
cin >> rows;
cout << "Enter start base value : "; // Prompt and accept base1 ...
cin >> base1;
cout << "Enter how many base values : " ; // Prompt and accept columns ...
cin >> columns;
cout << "Enter precision : "; // Prompt and accept precision (number of significant numbers) ...
cin >> n;
cout.precision(n); // set cout's precision for double values

cout << endl << endl << "Table starts at logarithm of b = " << base2 << " with base a = " << base1 << endl; // First output line on top of table ...
cout << "Log values "; // ... start of second line at top of Logarithmic Table ...
for( j=0; j < rows; j++) cout << setw(8) << " b + " << j; // ... rest of second line ...
cout << endl; // ... with end of line ...
for( i=0; i < columns; i++) { // ... for each base value ...
cout << " a + " << i << " ";
for( j=0; j < rows; j++) { // ... for each log value ...
cout << setw(8) << (log(base2+j) / log(base1+i)) << " "; // ... output (to standard output) a logarithmic table calculation ...
}
cout << endl; // ... end of a table row ...
}
return 0; // Finish ... bye.
}

Here is a link with fewer comments here you could rename to main.cpp ( and, for Xcode C++, at least, place it as the main.cpp source code for a C++ new Command Line Tool project of type C++ ).

Good link that helped with answer is here.

Today’s tutorial is based on a Yahoo Answers question here.

If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials, Xcode and tagged , , , , . Bookmark the permalink.

21 Responses to C++ Formatting cout Primer Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *