{"id":6645,"date":"2014-03-06T05:03:47","date_gmt":"2014-03-05T18:03:47","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=6645"},"modified":"2014-03-06T05:03:47","modified_gmt":"2014-03-05T18:03:47","slug":"c-formatting-cout-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-formatting-cout-primer-tutorial\/","title":{"rendered":"C++ Formatting cout Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/cout_formatting\/cout_formatting.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"C++ Formatting cout Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/cout_formatting\/cout_formatting.jpg\" title=\"C++ Formatting cout Primer Tutorial\" \/><\/a><p class=\"wp-caption-text\">C++ Formatting cout Primer Tutorial<\/p><\/div>\n<p>C++ uses iostream.h methods to format command line output.   Programmers with a background in C may think the iostream.h &#8220;<a target=_blank title='cout' href='http:\/\/www.cplusplus.com\/reference\/iostream\/cout'>cout<\/a>&#8221; stream output methods are a little harder to use (compared to C&#8217;s <a target=_blank title='printf' href='http:\/\/www.cplusplus.com\/reference\/cstdio\/printf'>printf<\/a> 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.<\/p>\n<p>Today, inspired by a Yahoo Answers <a target=_blank title='Yahoo Answers question' href='http:\/\/au.answers.yahoo.com\/question\/index?qid=20140304120518AAaNR7L'>question<\/a> (thanks) we take an introductory look at &#8220;cout&#8221; and the use of:<\/p>\n<ul>\n<li>cout.precision([numberSignificantNumbers])<\/li>\n<li>setw([colWidth]) \/\/ via iomanip.h<\/li>\n<li>endl<\/li>\n<\/ul>\n<p>Here is the C++ code (with comments) below within the Xcode IDE &#8230; project called &#8220;cout_formatting&#8221; &#8230; source code called &#8220;main.cpp&#8221;  &#8230; show logarithmic table &#8230;<\/p>\n<p><code><br \/>\n\/\/ cout_formatting.cpp<br \/>\n\/\/ Format a logarithmic table to standard output via cout and ...<br \/>\n\/\/   Also use:<br \/>\n\/\/         cout.precision([numberSignificantNumbers])<br \/>\n\/\/         setw([colWidth])<br \/>\n\/\/         endl<\/p>\n<p>using namespace std;<br \/>\n#include \"iostream.h\"       \/\/ needed for cout and cout.precision (and cin)<br \/>\n#include \"iomanip.h\"        \/\/ needed for setw()<br \/>\n#include \"math.h\"           \/\/ needed for log() method<\/p>\n<p>int main() {<br \/>\n    int rows, columns, n, i, j;          \/\/ rows=number of base values in table, columns=number of log values in table<br \/>\n    double base1 = 7.0, base2;           \/\/ base1 is log value, base2 is base value ... double values<br \/>\n    cout << \"Log values program\" << endl << endl;    \/\/ Introduce ...<br \/>\n    cout << \"Enter start logarithm value: \";         \/\/ Prompt and accept base2 ...<br \/>\n    cin >> base2;<br \/>\n    cout << \"Enter how many logarithm values : \";    \/\/ Prompt and accept rows ...<br \/>\n    cin >> rows;<br \/>\n    cout << \"Enter start base value : \";              \/\/ Prompt and accept base1 ...<br \/>\n    cin >> base1;<br \/>\n    cout << \"Enter how many base values : \" ;        \/\/ Prompt and accept columns ...<br \/>\n    cin >> columns;<br \/>\n    cout << \"Enter precision : \";                     \/\/ Prompt and accept precision (number of significant numbers) ...<br \/>\n    cin >> n;<br \/>\n    cout.precision(n);                    \/\/ set cout's precision for double values<\/p>\n<p>    cout << endl << endl << \"Table starts at logarithm of b = \" << base2 << \" with base a = \" << base1 << endl; \/\/ First output line on top of table ...<br \/>\n    cout << \"Log values \";     \/\/ ... start of second line at top of Logarithmic Table ...<br \/>\n    for( j=0; j < rows; j++) cout << setw(8) << \" b + \" << j;    \/\/ ... rest of second line ...<br \/>\n    cout << endl;                                              \/\/ ... with end of line ...<br \/>\n    for( i=0; i < columns; i++) {                 \/\/ ... for each base value ...<br \/>\n        cout << \"     a + \" << i << \" \";<br \/>\n        for( j=0; j < rows; j++) {                \/\/ ... for each log value ...<br \/>\n            cout << setw(8) << (log(base2+j) \/ log(base1+i)) << \" \";   \/\/ ... output (to standard output) a logarithmic table calculation ...<br \/>\n        }<br \/>\n        cout << endl;    \/\/ ... end of a table row ...<br \/>\n    }<br \/>\n    return 0;   \/\/ Finish ... bye.<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Here is a link with fewer comments here you could rename to <a target=_blank title='main.cpp' href='http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/cout_formatting\/main.cpp_GETME'>main.cpp<\/a> ( 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++ ).<\/p>\n<p>Good link that helped with answer is <a target=_blank title='setw usage' href='http:\/\/georgiakeays.me\/Cplus\/20120822\/1372033545199587.html'>here<\/a>.<\/p>\n<p>Today&#8217;s tutorial is based on a Yahoo Answers question <a target=_blank title='Yahoo Answers question' href='http:\/\/au.answers.yahoo.com\/question\/index?qid=20140304120518AAaNR7L'>here<\/a>.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d6632' onclick='var dv=document.getElementById(\"d6632\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/??tag=c\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d6632' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>C++ uses iostream.h methods to format command line output. Programmers with a background in C may think the iostream.h &#8220;cout&#8221; stream output methods are a little harder to use (compared to C&#8217;s printf and sprintf and fprintf (which, of course, &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-formatting-cout-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37,40],"tags":[176,997,1319,1473,1488],"class_list":["post-6645","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","category-xcode","tag-c","tag-programming","tag-tutorial","tag-xcode","tag-yahoo-answers"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/6645"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=6645"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/6645\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=6645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=6645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=6645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}