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

  1. It’s an remarkable piece of writing for all the internet people; they will take benefit from it I am sure.|

  2. There are not enough comments on this post that are really relevent to this topic. C’mon people at least read the post before you start making these comments.

  3. Required to compose you a tiny note to lastly thank you very significantly however once again for your private splendid strategies you might have discussed above. It truly is strangely open-handed with people like you to offer publicly all that quite a few folks would have marketed as an electronic book to produce some bucks for their very own finish, primarily now which you could possibly have tried it in the event you ever wanted. These inspiring tips likewise acted like a great way to know that the rest have exactly the same dreams actually like my individual own to see a complete lot a lot more concerning this problem. IÒ€ℒm positive you can find thousands of far more enjoyable times inside the future for a lot of who check out your weblog.

  4. After study some with the blog posts within your website now, and i genuinely such as your technique for blogging. I bookmarked it to my bookmark website list and are checking back soon. Pls appear into my internet internet site likewise and make me aware what you consider.

  5. excellent points altogether, you just received a new reader. What may you suggest in regards to your submit that you simply made a few days in the past? Any sure?

  6. Definitely, what a magnificent blog and educative posts, I surely will bookmark your website.Best Regards!

  7. Dr. Grossman says:

    This internet internet site is often a walk-through for all of the expertise you wanted concerning this and didnÒ€ℒt know who should. Glimpse here, and you will absolutely discover it.

  8. health says:

    I’m absolutely concur with your declaration or the things that you have actually shared here in this site.

  9. I’m entirely concur with your statement or things that you have actually shared right here in this website.

  10. I needed to compose you one very little word in order to say thanks the moment again for the nice tactics you have provided on this page. It is unbelievably open-handed of you to offer unreservedly what a lot of people could possibly have supplied for an ebook in making some profit on their own, most importantly given that you might have tried it if you desired. The strategies in addition served to provide a easy way to understand that the rest have the identical keenness much like my personal own to figure out somewhat more when it comes to this condition. I know there are some more pleasant opportunities up front for individuals that scan your blog post.

  11. Wow! Thank you! I permanently wanted to write on my blog something like that. Can I implement a portion of your post to my site?

  12. Girl Games says:

    You can definitely see your skills within the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. All the time go after your heart.

  13. I have to show my appreciation to the writer just for rescuing me from such a circumstance. Because of researching throughout the online world and finding thoughts which were not helpful, I believed my life was gone. Existing without the solutions to the issues you have sorted out as a result of your main website is a serious case, as well as the kind that would have in a negative way damaged my career if I had not discovered your website. The competence and kindness in touching all things was priceless. I’m not sure what I would have done if I hadn’t come upon such a subject like this. It’s possible to at this moment relish my future. Thanks very much for your reliable and amazing help. I won’t be reluctant to recommend your web blog to any individual who should receive guide on this issue.

  14. webpage says:

    How to become a tutor in creative writing for Further education and or teach English as a second language?

  15. I truly wanted to write a quick message to be able to express gratitude to you for those great information you are posting on this website. My long internet look up has finally been compensated with pleasant ideas to share with my colleagues. I ‘d assert that we readers actually are definitely fortunate to dwell in a superb network with many perfect people with very helpful tricks. I feel pretty fortunate to have seen your entire web site and look forward to plenty of more enjoyable minutes reading here. Thanks again for everything.

  16. I have fun with, lead to I found just what I used to be looking for. You have ended my 4 day long hunt! God Bless you man. Have a nice day. Bye

  17. heaven says:

    My husband and i felt so thankful John could conclude his survey through your ideas he discovered using your web site. It’s not at all simplistic to simply find yourself giving out tips and tricks which often other people may have been trying to sell. And we also keep in mind we’ve got the blog owner to give thanks to because of that. The most important illustrations you made, the straightforward web site navigation, the friendships you will give support to foster – it’s all remarkable, and it’s assisting our son in addition to us believe that that matter is amusing, and that is pretty pressing. Thanks for everything!

  18. I would like to show appreciation to the writer for rescuing me from such a dilemma. After checking throughout the the net and coming across ways which are not productive, I thought my life was done. Being alive without the approaches to the problems you’ve fixed all through your entire guide is a critical case, and ones which may have in a negative way affected my entire career if I had not noticed your blog post. The expertise and kindness in touching the whole thing was very useful. I am not sure what I would have done if I hadn’t come across such a step like this. I am able to at this moment look forward to my future. Thanks very much for your reliable and result oriented guide. I won’t be reluctant to endorse your site to any person who should get tips about this problem.

  19. Very good written story. It will be supportive to anyone who utilizes it, as well as me. Keep up the good work – i will definitely read more posts.

  20. Dante Neice says:

    Youre so amazing! I don’t expect Ive read anything similar to this prior to. So wonderful to find someone with some initial ideas on this subject. realy thank you for beginning this up. this website is something that is needed on the internet, a person with a little originality. beneficial job for bringing something new to the internet!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>