COSC 1315 Fundamentals of Programming
  1. iostream
    Working class derived from both istream and ostream. you include this class with <iostream> definition.

    1. Objects
      1. cin
        Standard input of class istream.
      2. cout
        Standard output of class ostream.
      3. cerr and clog
        Standard error output of class ostream.

    2. Overloaded Operators
      1. <<
        Shift left is overload and called the stream insertion operator. This operator was chosen because it associates left to right, low order of precedence and just the way it look denoting the flow of data. Which means that statements like:
        	int i=47;
        	cout << i << " " << i + 3 << endl;
         
        Gives the correct output of:
        47 50
        
        This is accomplished thru the use of multiple overloaded operator definitions.

        There are defined manipulators for cout like:

        • endl -print an end of line the same as sending '\n' to cout.
        • flush - flush the output stream buffer.
      2. >>
        The shift right has been overloaded for the istream class and called the stream extractor.

    3. Common manipulators and member functions to use these include <iomanip>.

      manipulatorMember functionDescription
      decsetf(ios::dec)Set radix to 10
      hexsetf(ios::hex)Set radix to 16
      octsetf(ios::oct)Set radix to 8
      setfill(c)fill(c)Set the fill character to c.
      setprecision(n)precision(n)Set display precsion to n
      setw(n)width(n)Set output field width to n.
      setiosflags(p)setf(p)Turn on IOS icontrol bits
      resetiosflags(p)unsetf(p)Turn off IOS control bits

    4. Format state flag bits

      IOS::FlagDescription
      skipws Skip whitespace characters on an input stream
      left Left justify ouput
      right Right justify the output
      internal Left justify the sign, right justify the number
      dec Display the number in base 10
      oct Display the number in base 8
      hex Display the number in base 16
      showbase Specify base ahead of the number
      showpoint Display floating point numbers with a decimal point
      uppercase Use upper case in displaying numbers
      showpos Show positive or negative signs
      scientific Output floating point in scientific notation
      fixed Specify the number of output to right of decimal point

    5. Examples


      1. //
        //
        //
        #include <iostream>
        #include <iomanip>
        using namespace std;
        void main()
        {
            long ioflags;
            ioflags=cout.flags();
        
            int i=47;
            cout << "Integer " << i << endl;
        
            char a='a';
            cout << "Character output " << a << endl;
        
            float f=3.14159;
            cout << "Floating Point " << f << endl;
            
            cout << "Integer in hex  "  << hex << i << endl;
            cout << "Integer in Octal " << oct << i << endl;
            cout << dec;
            
            cout << "Integer in width " << setw(5) << i << endl;
            cout << "Integer in width with fill " << setfill('-') << setw(5) << i << endl;
            
            cout.flags(ioflags);
            cout.setf(ios::left);
            cout << "Justify left " << setw(5) << i << endl;
            
            cout.flags(ioflags);
            cout.fill('-');
            cout.setf(ios::right);
            cout << "Justify Right " << setw(5) << i << endl;
            
            i=-47;
            cout.fill(' ');
            cout.flags(ioflags);
            cout.setf(ios::internal);
            cout << "Internal " << setw(5) << i << endl;
            }
        

        Integer 47
        Character output a
        Floating Point 3.14159
        Integer in hex  2f
        Integer in Octal 57
        Integer in width    47
        Integer in width with fill ---47
        Justify left 47---
        Justify Right ---47
        Internal   -47
        

      2. #include <iomanip>
        #include <iostream>
        using namespace std;
        main()
        {
          char a='a';
          int  i=34,j=-34;
          float f=3.14159;
          
          cout << a << endl;
          cout << (int)a << endl;
          cout << i << " " << j << endl;
          cout << (unsigned)i << " " << (unsigned)j << endl;
          cout << oct << i << endl;
          cout << hex << i << endl;
          cout << dec;
        
          cout << f << endl;
        
          cout << setw(3) << i << " " << setw(3) << j << endl;
          cout << setiosflags( ios:: showpoint | ios::fixed);
          cout << setw(6) << setprecision(2) << f << endl;
        
           i=8;
           j=3;
           cout << setw(i) << setprecision(j) << f << endl;
          return(0);
        }
        

        a
        97
        34 -34
        34 4294967262
        42
        22
        3.14159
         34 -34
          3.14
           3.142
        

  2. More complex example
    Value of an Annuity.
    An Annuity, or installment plan, is a series of payments made at equal intervals of time. Examples are pensions, IRAs and premiums on life insurance. More often than not, the interest conversion period is unequal to the payment interval. The Following formula determines the eventual cash cash value, s, of an annuity of r dollars paid per year in p installments for n years at an interest rate of j percent converted m times per year.

    x = 1 + j/m

    s = r [ (xmn-1)/ p(xm/p-1)]

    Where s = eventual cash value 
    r = payment per year 
    p = installments per year 
    n = duration of the annuity in years 
    j = nominal interest rate 
    m = conversions per year 
    

    Write a program to determine the eventual cash value after requesting the necessary information. To test use r=2000 p=12 n=20 j=13% m=2, you should get 180,330.40



    //
    // value of an annuity
    //
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    
    int main()
    {
        float r;  //  payment per year 
        float p;  //  installments per year 
        float n;  //  duration of the annuity in yea
        float j;  //  nominal interest rate 
        float m;  //  conversions per year
        float x;  //  working variable
        float numerator, denominator;
        float s;  // eventual value
        
        cout << "Enter the payment per year : ";
        cin >> r;
        cout << "Enter the installments per year : ";
        cin >> p;
        cout << "Enter the duration of the annuity in years :";
        cin >> n;
        cout << "Enter the nominal interest rate :";
        cin >> j;  
        cout << "Enter the conversions per year : ";
        cin >> m;
        
        if(j > 1.0) j/=100.0;
        
        x=1 + j/m;
        
        numerator=pow(x,m*n)-1;
        denominator=p * (pow(x,m/p)-1);
        
        s=r*(numerator/denominator);
        
        cout << setiosflags( ios:: showpoint | ios::fixed);
        cout << "The cash value of this annuity is " 
             << setprecision(2) << s << endl;
             
        system("pause");
        return(0);
     
    }
    
    


    Enter the payment per year : 2000
    Enter the installments per year : 12
    Enter the duration of the annuity in years :20
    Enter the nominal interest rate :13
    Enter the conversions per year : 2
    The cash value of this annuity is 180330.44
    Press any key to continue . . .