So in the beginning, Im sure i can ditch #include <string>...
Im sure at the end, where i have a million cout's (42-69) that could be simplified (i feel like the inverse of the distributive property in math could be applied.

I want the ouput to look the same. this seems to work for me for numbers 99,999.00 and smaller. If there is an approach that will allign all dollar signs, (and all decimal points) while keeping the percentages and integers and ends (.00) of the setprecision(2) numbers justified right.

This is in my 4th class and my proffessor is such a useless D-bag. one class he decided to read his powerpoints out loud (loudly to himself) without displaying them. I learn absoloutely nothing in class and I only have the book, (sample programs) and google to help me with the rest.

I might be confusing so let me know if i can clarify...

Again, i got it to output the way i want to, I just feel like i did it in a complicated manner. (i moved all the bricks from one side to another alright, but i did it one mollecule at a time instead of with a forklift... if you catch my analogy.)

//This program is used to calculate: Monthly Payment, Amount Paid Back, and Interest Paid
//from the inputs: Loan Amount, Monthly Interest Rate, and Number of Payments.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
float	L ;
float 	RatePercent ;
float   N ;
float	monthlyPayment=0 ;
float   amountPaid ;
float   interestPaid ;
float	Rate ;

cout << setprecision(2) <<fixed << showpoint <<setw(10);

//Prompt user to enter data
cout << "Enter the following amounts:  \n" ;
cout <<  "Loan Amount: " ;
cin >> L ;
cout << "Yearly Interest Rate: ";
cin >> RatePercent ;
cout << "Number of Payments: ";
cin >> N ;

//Calculate Monthly Payment, Amount Paid Back, and Interest Paid. 

Rate = RatePercent / 1200 ;
monthlyPayment = L * (Rate * pow((1+Rate),N))/((pow((1+Rate),N)-1));
amountPaid = monthlyPayment * N;
interestPaid = amountPaid - L; 

//Display calculated data
cout << endl << endl << endl; 

cout << "Loan Amount: " ;
cout << setw(12) ;
cout << "$ " ;
cout << setw(8);
cout <<	L  << endl ;
cout << "monthly Interest Rate: " ;
cout << setw(9);
cout << setprecision(0) << noshowpoint ;
cout << RatePercent/12 << "%\n" ;
cout << "Number of Payments: " ;
cout << setw(13);
cout << N << endl ;
cout << showpoint << setprecision(2) ; 
cout << "Monthly Payment: " ;
cout << setw(8);
cout << "$ " ;
cout << setw(8);
cout << monthlyPayment << endl;
cout << "Amount Paid Back: " ;
cout << setw(7) ;
cout << "$ " ;
cout << setw(8);
cout <<  amountPaid  << endl;
cout << "Interest Paid: " ;
cout << setw(10);
cout << "$ ";
cout << setw(8);
cout << interestPaid << endl;

return 0;


Recommended Answers

All 8 Replies

You can string multiple outputs (including stream manipulators such as setw()) together:

cout << "Loan Amount: " << setw(12) << "$ " << setw(8) << L << endl ;

ok cool. I must have messed up when i tried that the first time. I got errors. but now it works great. thanks.

Is there any further simplification after I condense all those one liners?

When I attempt to compile the condensed version of your code, the only error I get is related to the "pow()"s on Line 34.

To correct this, you need to add the <cmath> header to your #includes.

Mine compiled just fine, I guess I had a handicap with MS VS 2010?
I thought I didnt need it because it worked. But ill go ahead and include it.

So normally this code wont compile because of that error?

What about all those floats that the beginning could I do :

float L, ratePercent, N //etc...//;

Mine compiled just fine, I guess I had a handicap with MS VS 2010?
I thought I didnt need it because it worked. But ill go ahead and include it.

So normally this code wont compile because of that error?

It shouldn't. There must be a header overlap somewhere.
It does not compile for me on MS VS 2008 or on Code::Blocks. And Code::Blocks also rejects the "system()" statement on Line 71.

Ok thanks. This is my first use of MSVS since i installed it. I never put cmath in. weird. so to clarify, will i need cmath for all functions? even /,*,% etc.? that was a thought in the back of my mind, I was thinking "I should use... but it works... eh? whatever." Now i know. again, I do have the **itiest comp-sci teacher known to the whole world, so thanks for your patience.

No, you only need it for math functions such as pow(), floor(), ceil(), sin(), cos(), etc...

The symbols '+', '-', '*' , '/', and '%' are operators not functions. Because they are operators, their functionality is built-in to the core language; no headers are required to use them.

Be a part of the DaniWeb community

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