Write a program to test the results of printing the integer value 12345 and the floating-point value 1.2345 in various-sized fields. What happens when the values are printed in fields containing fewer digits than the values?

What will be an ideal response?

```
#include
#include
using namespace std;

int main()
{
// values used for testing output in various field lengths
int x = 12345;
double y = 1.2345;

// display values in fields the size of loop counter
for ( int loop = 0; loop <= 10; loop++ )
cout << x << " printed in a field of size " << setw( 2 )
<< loop << " is " << setw( loop ) << x << '\n' << y
<< " printed in a field of size " << setw( 2 )
<< loop << " is " << setw( loop ) << y << '\n';
} // end main
```
12345 printed in a field of size 0 is 12345
1.2345 printed in a field of size 0 is 1.2345
12345 printed in a field of size 1 is 12345
1.2345 printed in a field of size 1 is 1.2345
12345 printed in a field of size 2 is 12345
1.2345 printed in a field of size 2 is 1.2345
12345 printed in a field of size 3 is 12345
1.2345 printed in a field of size 3 is 1.2345
12345 printed in a field of size 4 is 12345
1.2345 printed in a field of size 4 is 1.2345
12345 printed in a field of size 5 is 12345
1.2345 printed in a field of size 5 is 1.2345
12345 printed in a field of size 6 is 12345
1.2345 printed in a field of size 6 is 1.2345
12345 printed in a field of size 7 is 12345
1.2345 printed in a field of size 7 is 1.2345
12345 printed in a field of size 8 is 12345
1.2345 printed in a field of size 8 is 1.2345
12345 printed in a field of size 9 is 12345
1.2345 printed in a field of size 9 is 1.2345
12345 printed in a field of size 10 is 12345
1.2345 printed in a field of size 10 is 1.2345

Computer Science & Information Technology

You might also like to view...

Which of the following protocols is used for ping commands that use hostnames instead of IP addresses?

A) EIGRP B) IGP C) EGP D) DNS

Computer Science & Information Technology

Which of the following is false?

a. If a string is converted to a C-style array using data, modifying the string could cause the pointer previously returned by data to become invalid. b. Converting a string containing one or more null characters to a C-style string can cause logic errors. c. c_str returns a character array of the same length as the string object it is called on. d. copy implicitly converts a string to a non-null terminated character array.

Computer Science & Information Technology