I have written a program that work as supposed to, except the output needs to be in 2 cols. instead of 1 col. in console. Could someone help me to format this?

Below is the program and the output I need to change. 14.14.cpp : Defines the entry point for the console application.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using std::cout;
using std::endl;
using std::cerr;
using std::setw;
using std::ofstream;
using std::ifstream;
 
int main()
{
 
ofstream outFile("datasize.dat");
 
cout << "Data type" << setw(16) << "Size\nchar"
<< setw(21) << sizeof(char)
<< "\nunsigned char" << setw( 12 ) << sizeof(unsigned char)
<< "\nshort int" << setw( 16 ) << sizeof(short int)
<< "\nunsigned short int" << setw( 7 ) << sizeof(unsigned short)
<< "\nint" << setw( 22 ) << sizeof(int) << '\n';
 
cout << "unsigned int" << setw( 13 ) << sizeof(unsigned)
<< "\nlong int" << setw( 17 ) << sizeof( long )
<< "\nunsigned long int" << setw( 8 ) << sizeof(unsigned long int)
<< "\ndouble" << setw ( 20 ) << sizeof (double)
<< "\ndouble" << setw ( 19 ) << sizeof (double)
<< "\nlong double" << setw (14) << sizeof (long double)<< endl;
 
return 0;
}

Data type Size
char 1
unsignedchar 1
shortint 2
unsignedshortint 2
//need to split col. here.:cheesy: :cheesy: :cheesy: :cheesy:
int 4
unsignedint 4
longint 4
unsignedlongint 4
double 8
double 8
longdouble 8
Press any key to continue . . .

Member Avatar for iamthwee

Erm,

cout << hello

cout << hello << "     " << hello

Negating the obvious syntax errors can you see how line three has two columns.

How weird you say. :lol:

Erm,

cout << hello

cout << hello << "     " << hello

Negating the obvious syntax errors can you see how line three has two columns.

How weird you say. :lol:

Did you read his code? It's done that way. :rolleyes:

Here's the output I get running it:

Data type       Size
char                    1
unsigned char           1
short int               2
unsigned short int      2
int                     4
unsigned int            4
long int                4
unsigned long int       4
double                   8
double                  8
long double            12

As you can see there's a couple which don't line up quite right. What you should probably do is use setw twice per line, one for the width of the data-type column, one for the width of the size column. That way you don't have to count the length of each string and adjust the setw for size.

Another stylistic note: if I were writing this, I'd keep it to one line per cout, and end the line with a << "\n". I'm specifically thinking about line 18 and starting everything else with a '\n'. Mainly a readability issue...

[edit:] Just realized I'd misunderstood the need for 2 columns. You actually want 4 (you already have 2), and you'll need to manually change it, so it'd be something like this:

cout << setw(dataTypeColWidth) << "Int" << setw(sizeColWidth << sizeof(int) << " some spacer" << setw(dataTypeColWidth) << "double" << setw(sizeColWidth) << sizeof(double) << "\n";

and this would get a result of (using dataTypeColWidth=15, sizeColWidth=1, spacer=5 spaces since I'm doing it by hand):

Int            4     double         8

Ugly, eh?

commented: Nice formatting. ;) - joeprogrammer +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.