Lesson 4: Adding on using IOManip

[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)


[/spoiler]

[spoiler title=”Lesson Source Code”]

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
 
 
int main(){
 
  // 1 + 3
  // i + j
  // +, -, *, /, %    -- % is remainder 10%3   10/3 then give me the remainder,  1
  //a, b ,c.   total = a+b+c;   total = 10;  total = total+a+b+c;  total += a+b+c;
  // +=, -=, *=, /=
 
  //Homework: Creating a simple interest calculator -- futureMoney = principle*(rate*time);
  //Creating a compound interest calculator
 
  int years = 0;
  double APR = 0.0, principal = 0.0, total = 0.0, total2=0.0, total3=0.0;
 
  //fixed -- Scientific notation 5 decimal places are always shown
  cout < < setiosflags(ios::showpoint) << setiosflags(ios::fixed); 
 
 
  cout << "Enter your starting capital: ";
  cin >> principal;
 
  cout < < "\nEnter your APR (ex: 8.0% = 8):  ";
  cin >> APR;
 
  cout < < "\nHow many years will your invest this money for: ";
  cin >> years;
 
  APR /= 100;
  total = principal * (pow((1 + APR), 1));
  total2 = principal * (pow((1+APR),2)  );
  total3 = principal * (pow((1 + APR), years));
 
 
  cout < < "Starting amount" << " | " << "Interest Rate" << " | " << "Years Invested" << " | " << "Final Amount" << endl;
 
  cout << setprecision(2) << "$" << principal << setw(10) << " | " << 1 + APR << "%" << setw(11) << "| " << 1 << setw(16) << "| " << total << endl;
  cout << setprecision(2) << "$" << principal << setw(10) << " | " << 1 + APR << "%" << setw(11) << "| " << 2 << setw(16) << "| " << total2 << endl;
  cout << setprecision(2) << "$" << principal << setw(10) << " | " << 1 + APR << "%" << setw(11) << "| " << years << setw(16) << "| " << total3 << endl;
 
 
  //cout << "\nThe amount in your account after: " << years << " years will be: " << total << endl;
 
  cout << "\n\n";
 
 
  system("PAUSE");
  return 0;
}

[/spoiler]



IOManip
In this lesson we covered iomanip (full listing of functions in library here: http://www.cplusplus.com/reference/iomanip/). The 4 functions that we set our sights on were

  1. showpoint (via setiosflags)
  2. fixed(via setiosflags)
  3. setprecision
  4. setw


Showpoint
Showpoint is an ios flag that forces the console to show the numbers after the decimal point. This is true even with integer numbers that do not have a decimal point


Fixed
Fixed is an ios flag that forces the console to show a certain amount of numbers after the decimal point


setprecision
Setprecision is a function that specifies the minimum precision (points after the decimal) you will allow it to print.


setw
Setw is a function used for formatting tables. It automatically adds a certain number of spaces. You can also change the fill using setfill (linked in the reference above)