| C++ Stream Input/Output Solved Program (Source Code) |
| Write a program to test in putting integer values in
decimal, octal and hexadecimal format. Output each integer read by the program
in all three formats. Test the program with the following input data: 10, 010,
0x10 |
|
Write a program to test the results of printing the integer value 12345 and the
floating-point value 1.2345 in varioussize fields. What happens when the values
are printed in fields containing fewer digits than the values?
#include < iostream.h >
#include < iomanip.h >
int main()
{
int x = 12345;
double y = 1.2345;
for ( int loop = 0; loop <= 10; ++loop )
cout << x << " printed in a field of size " << loop <<
" is " << setw( loop ) << x << '\n' << y << "
printed in a field " << "of size " << loop << " is " <<
setw( loop ) << y << '\n';
return 0;
}
|
Write a program that prints the value 100.453627 rounded to the nearest digit,
tenth, hundredth, thousandth and ten thousandth.
#include < iostream.h >
#include < iomanip.h >
int main()
{
double x = 100.453627;
cout << setiosflags( ios::fixed );
for ( int loop = 0; loop <= 5; ++loop ) cout << setprecision( loop )
<< "Rounded to " << loop << " digit(s) is " << x
<< endl;
return 0;
} |
Write a program that inputs a string from the keyboard and determines the
length of the string. Print the string using twice the length as the field
width
#include < iostream.h >
#include < iomanip.h >
#include < string.h >
const int SIZE = 80;
int main()
{
char string[ SIZE ];
int stringLength; cout << "Enter a string: ";
cin >> string; stringLength = strlen( string );
cout << "the length of the string is " << strlen( string ) <<
endl;
// print string using twice the length as field with
cout << setw( 2 * stringLength ) << string << endl;
return 0;
} |
Write a program that uses a for structure to print a table of ASCII values for
the characters in the ASCII character set from 33 to 126. The program should
print the decimal value, octal value, hexadecimal value and character value for
each character. Use the stream manipulators dec, oct and hex to print the
integer values.
#include < iostream.h >
#include < iomanip.h >
int main()
{
cout << setw( 7 ) << "Decimal" << setw( 9 ) << "Octal "
<< setw( 15 ) << "Hexadecimal " << setw( 13 ) <<
"Character" << setiosflags( ios::showbase ) << '\n';
for ( int loop = 33; loop < = 126; ++loop )
cout << setw( 7 ) << dec << loop << setw( 9 ) <<
oct << loop << setw( 15 ) << hex << loop <<
setw(13) << (char)< CHAR >( loop ) << endl; return 0;
} |
|
Write a program to show that the getline and three-argument get istream member
functions each end the input string with a string-terminating null character.
Also, show that get leaves the delimiter character on the input stream while
getline extracts the delimiter character and discards it. What happens to the
unread characters in the stream?
#include < iostream.h >
#include < ctype.h >
const int SIZE = 80;
int main()
{
char array[ SIZE ], array2[ SIZE ], c;
cout << "Enter a sentence to test getline() and get():\n";
cin.getline( array, SIZE, '*' );
cout << array << '\n';
cin >> c;
// read next character in input
cout << "The next character in the input is: " << c << '\n';
cin.get( array2, SIZE, '*' );
cout << array2 << '\n';
cin >> c;
// read next character in input
cout << "The next character in the input is: " << c << '\n';
return 0;
}
|
|
|
|
|
|
|
|