Need help understanding formatting output with printf

This is a discussion on Need help understanding formatting output with printf within the C Programming forums, part of the General Programming Boards category; Hi folks, what i'm trying to do is put in two spaces and then ask the user to enter something. ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Need help understanding formatting output with printf

    Hi folks,

    what i'm trying to do is put in two spaces and then ask the user to enter something. this is what my code looks like so far:

    Code:
    #include <cmath> 
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
        double payment;
        double loan;
        double rate;
        double nPayments;
        bool valid;  
        
        printf("%50s","William's Loan Service\n");  
        do {
            valid=true; 
            printf("%4s", "\nPlease enter the loan amount.");
            scanf("%lf",&loan);
            if (loan < 100 || loan > 999999.99)
            {
                printf("\nLoan amount must be between $100.00 and $999,999.99");
                       valid=false;
            }
        }while (!valid);   
        do {
            valid=true; 
            printf("\n%2sPlease enter annual interest rate.  ");
            scanf("%lf",&rate);
            if (rate < 1 || rate > 20)
            {
                printf("\nThe interest rate must be between 1 and 20");
                       valid=false;
            }
        }while (!valid);    
         do {
            valid=true; 
            printf("\n%2sPlease enter the number of payments.");
            scanf("%lf",&nPayments);
            if (nPayments < 5 || nPayments > 360)
            {
                printf("\nThe number of payments must be between 5 and 360");
                       valid=false;
            }
        }while (!valid);    
        rate = rate/1200;
        payment = (rate*pow((1+rate),nPayments)/(pow((1+rate),nPayments)-1))*loan;
        printf("\n%20s %15s %10.2lf","Loan Amount","$", loan);
    
        
        system("pause");
    The odd part is that if you look at the second do/while loop inside the printf printf("\n%2sPlease enter annual interest rate. "); works just fine, and as I thought it would; it puts two spaces and then displays text. In fact from this point down it works fine but if I try using it in my first do/while loop or in the very first printf it causes a fatal error (took me an hour to pinpoint that).

    So instead, I tried what you see, and if I put any number from 1-50 after the % and before the s it does seemingly nothing but then if i put higher number it jumps half away across the screen.

    Also, I know that the number between % and letter specifies a width but exactly how much width? In other words, is 1= to one spacebar?

    Any help is much appreciated

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This:
    Code:
    printf("\n%2sPlease enter annual interest rate.  ");
    is an illegal printf statement. %s tells printf to insert the string pointed to by the pointer-to-char argument that you gave it and put it there. But you didn't supply a pointer-to-char, so the compiler is taking four bytes (probably) of your program and pretending that it is a pointer to char.

    If you want spaces in your output, put them there. For instance, if you want two spaces before your text, then you should print:
    Code:
    printf("\n  Please enter annual interest rate.");

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Thanks for the info. I have recoded my program to this
    Code:
    #include <cmath> 
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
        double payment;
        double loan;
        double rate;
        double nPayments;
        double mathRate;
        bool valid;  
        
        printf("%50s","William's Loan Service\n");  
        do {
            valid=true; 
            printf("\n  Please enter the loan amount.  ");
            scanf("%lf",&loan);
            if (loan < 100 || loan > 999999.99)
            {
                printf("\n Loan amount must be between $100.00 and $999,999.99\n");
                       valid=false;
            }
        }while (!valid);   
        do {
            valid=true; 
            printf("\n  Please enter annual interest rate.  ");
            scanf("%lf",&rate);
            if (rate < 1 || rate > 20)
            {
                printf("\n The interest rate must be between 1 and 20\n");
                       valid=false;
            }
        }while (!valid);    
         do {
            valid=true; 
            printf("\n  Please enter the number of payments.  ");
            scanf("%lf",&nPayments);
            if (nPayments < 5 || nPayments > 360)
            {
                printf("\n The number of payments must be between 5 and 360\n");
                       valid=false;
            }
        }while (!valid);    
        mathRate = rate/1200;
        payment = (mathRate*pow((1+mathRate),nPayments)/(pow((1+mathRate),nPayments)-1))*loan;
        printf("\n        Loan Amount:                  $     %9.2lf\
               \n        Annual Interest rate:               %9.lf\
               \n        Number of Payments:                 %9.lf\
               \n        Monthly Payments:             $     %9.2lf\
               \n        Amount Paid Back:             $     %9.2lf\
               \n        Interest Paid:                $     %9.2lf\
               \n", loan,rate,nPayments,payment,(payment*nPayments),((payment*nPayments)-loan));
    
        
        system("pause");
    }
    Asthetically my program looks just like the example. Is this the standard way to format the display? The issue is that I am new to C and I have an instructor who is, to put it nicely, very cryptic. his instructions were literally:

    "• Validate all user entry. If no range is specified then the entry must be greater than 0.
    • You must use the proper kind of loops for validation
    • Make sure that your display is formatted correctly
    Validation:
    Loan amount should be between $100 and $999,999.99
    Interest rate should be between 1 % and 20%
    Number of payments should be between 5 and 360 months


    The report should be formatted and aligned according to the above"

    I am most concerned about the final printf. is that considered "proper"?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You could do it that way. If you put \n in your format string, you don't need to actually put a new line in your source code. I'm not sure what you think those \ at the end of your lines are actually doing, to be honest. (They don't do anything.) Really you just want
    Code:
    printf("\n      Loan Amount:             $     %9.2lf\n      Annual Interest Rate:           ...etc", stuff);
    And do you really want all those spaces after the dollar sign?

    Actually most people would write that as
    Code:
    printf("\n\tLoan Amount:\t\t$%9.2lf", loan);
    printf("\n\tAnnual Interest Rate:\t\t%9.lf\%", interest);
    etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with two two dimensional arrays
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 12-06-2009, 05:57 PM
  2. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  3. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  4. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 07:35 PM
  5. menu problem!!! need U R G E N T help!!!
    By catcat28 in forum C Programming
    Replies: 16
    Last Post: 11-19-2007, 12:32 PM

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