#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    float annualIntRate, principle, monthlyPayment, monthlyIntRate, totalAmt, intPaid;
    int yearsBorrowed, numPayments;

    cout << "This program computes information associated with a loan" << endl;
    cout << "Please input the Annual Interest Rate:" << endl;
    cin >> annualIntRate;
    cout << "Pleast input the number of years for which you have to pay back the loan:" << endl;
    cin >> yearsBorrowed;
    cout << "Please enter the amount that has been borrowed:" << endl;
    cin >> principle;
    monthlyIntRate = annualIntRate / 12;
    numPayments = yearsBorrowed * 12;
    monthlyPayment = principle * (monthlyIntRate/100.0 * pow(1 + monthlyIntRate/100.0, numPayments))/(pow(1 + monthlyIntRate/100.0, numPayments) - 1);
    totalAmt = monthlyPayment * numPayments;
    intPaid = totalAmt - principle;

    cout << setprecision(2) << fixed;
    cout << "Loan amount:" << setw(10) << "$" << principle << endl;
    cout << "Monthly Interest Rate:" << setw(10) << monthlyIntRate << "%" << endl;
    cout << "Number of Payments:" << setw(10) << numPayments << endl;
    cout << "Monthly Payment:" << setw(10) << "$" << monthlyPayment << endl;
    cout << "Total Amount Paid:" << setw(10) << "$" <<totalAmt << endl;
    cout << "Interest Paid:" << setw(10) << "$" << intPaid << endl;

    return 0;
}

Why is my code not aligned using the setw manipulator and instead it is just created a space of 10 spaces?

setw() is an absolute field width, not something based on tab stop boundaries, so you'd need to calculate the correct field widths for even alignment. You can also use the '\t' escape character to print a tab that will adhere to tab stop boundaries:

cout << setw(23) << left << "Loan amount:" << "\t$" << principle << endl;
cout << setw(23) << left << "Monthly Interest Rate:" << "\t" << monthlyIntRate << "%" << endl;
cout << setw(23) << left << "Number of Payments:" << "\t" << numPayments << endl;
cout << setw(23) << left << "Monthly Payment:" << "\t$" << monthlyPayment << endl;
cout << setw(23) << left << "Total Amount Paid:" << "\t$" <<totalAmt << endl;
cout << setw(23) << left << "Interest Paid:" << "\t$" << intPaid << endl;

And, if you want the numbers to be aligned to the right, you have to put the setw immediately before you print the number and you can also use setprecision to have only two numbers after the dot (for floating-point numbers), as in:

cout << setprecision(2);
cout << setw(23) << left << "Loan amount:" << "\t$" << setw(5) << principle << endl;
cout << setw(23) << left << "Monthly Interest Rate:" << "\t" << setw(6) << monthlyIntRate << "%" << endl;
cout << setw(23) << left << "Number of Payments:" << "\t" << setw(6) << numPayments << endl;
cout << setw(23) << left << "Monthly Payment:" << "\t$" << setw(5) << monthlyPayment << endl;
cout << setw(23) << left << "Total Amount Paid:" << "\t$" << setw(5) <<totalAmt << endl;
cout << setw(23) << left << "Interest Paid:" << "\t$" << setw(5) << intPaid << endl;

That should be pretty nice looking!

Thank you very much for your help. This was starting to give me a headache. In the book the example doesn't have the tabs and the "example display" shows it just fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.