for loop!

This is a discussion on for loop! within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main () { int num=1,sqr=1,cube=1,pow; printf(" NUMBER SQUARE CUBE\n"); printf(" ====== ====== =====\n"); for(num=1; num<=10; num++) ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    for loop!

    Code:
     
    
    #include <stdio.h>
    int main ()
    {
        int num=1,sqr=1,cube=1,pow;
        printf(" NUMBER    SQUARE    CUBE\n");
        printf(" ======    ======    =====\n");
        for(num=1; num<=10; num++)
        { 
             printf("   %d          %d        %d\n",num,num*num,num*num*num);
        }
        system ("pause");
        return 0;
    }
    how can i get the output to be in a straight line from top to bottom?
    Last edited by amoeba532; 12-09-2010 at 04:04 AM. Reason: found the ans for pre 1

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    Your query is not very clear, what do you mean exactly? That is different from what should happen already? do you mean all the columns evenly 'left justified' ? Try using a \t to output a tabspace after each value in the line.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    If I understand your somewhat vague question correctly, you wish you have the output like so:

    Code:
    NUMBER    SQUARE    CUBE
    ======    ======    ====
         1         1       1
         2         4       8
         3         9      27
    etc.
    To accomplish this, look at the format specifiers for the string you feed to the printf function:

    printf reference

    You will want to look at the part that goes in front of the format specifiers to be precise, in particular the width sub-specifier.

    If you on the other hand wish to have the output left justified, this can also be done by looking it up at that particular reference.
    Last edited by JacobN; 12-09-2010 at 06:48 AM. Reason: Wanted to add additional information.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Is this supposed to be C++? ...Or C?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21