DUE TONIGHT!! Formatting table out to screen for money
Jan 21, 2012 at 1:10am UTC
Here is the code I have now, it should compile. The commented out portion explains HOW it should appear and also shows that I should be accomplishing this using setprecision, setw, and setiosflags.
1. Ive already read through the documentation/references on cplusplus.com
2. Ive already googled extensively
3. Ive asked 3 tutors at my school, each wasted an hour of my time with no solution
4. My assignment is due tonight with nobody else to ask for help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>; //Input Output
#include <iomanip>; //Decimal precision / set width
using namespace std;
void main(){
//Set starting values
double balance = 10050; double payment = 500; double interestRate = 0.01;
/*use the setprecision, setw and setiosflags defined in the <iomanip> to create the table and format it so the pennies align...
example:
Month Balance Interest Payment
$10050.00 $100.50 $500.00
$ 9650.50 $ 96.51 $500.00
$ 9247.01 $ 92.47 $500.00
$ 8839.48 $ 88.39 $500.00
*/
cout << setw(13) << "Month Balance" << setw(13) << "Interest" << setw(13) << "Payment" << endl; //Header
for (int i = 0; i < 4; i++){
interest = balance * interestRate; //Set interest amount
cout //<< setiosflags(std::ios_base::left)
<< fixed << setprecision(2) << "$" << balance
<< setw(12) << setprecision(2) << "$" << interest
<< setw(12) << fixed << setprecision(2) << "$" << payment
<< endl;
balance = balance + interest - payment; //Increment amounts
}}//end
Jan 21, 2012 at 2:09am UTC
Try this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>; //Input Output
#include <iomanip>; //Decimal precision / set width
using namespace std;
void main(){
double balance = 10050;
double payment = 500;
double interestRate = 0.01;
double interest;
cout << "Month balance\tInterest\tPayment" << endl; // Header
cout << setiosflags(ios::right);
for (int i=0; i < 4; i++) {
interest = balance * interestRate; // Set interest amount
cout << fixed << "$" << setw(8) << setprecision(2) << balance
<< "\t$" << setw(6) << setprecision(2) << interest
<< "\t\t$" << setw(6) << setprecision(2) << payment
<< endl;
balance = balance + interest - payment;
}
}
Last edited on Jan 21, 2012 at 2:10am UTC
Jan 21, 2012 at 3:02am UTC
That worked great, thanks!
Topic archived. No new replies allowed.