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