int i=47; cout << i << " " << i + 3 << endl; Gives the correct output of: 47 50This is accomplished thru the use of multiple overloaded operator definitions.
There are defined manipulators for cout like:
| manipulator | Member function | Description |
|---|---|---|
| dec | setf(ios::dec) | Set radix to 10 |
| hex | setf(ios::hex) | Set radix to 16 |
| oct | setf(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 |
| IOS::Flag | Description |
|---|---|
| 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 |
//
//
//
#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
#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
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 . . .