GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-May-2007, 08:48
jtrink jtrink is offline
New Member
 
Join Date: Jan 2007
Posts: 20
jtrink is on a distinguished road
Talking

How to make columns when reading in files???


Hey how would i make columns when im reading in a text document? My prorgam outputs everything correctly, but my numbers dont line up when my program runs b/c all my "players" names are different sizes...

here is my code:

CPP / C++ / C Code:
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>

void main()
{
 
 int ab;
 float average;
 char players[256];
 
 cout << setprecision(3)<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint);
   
 ifstream Open;
 
 Open.open("Yanks.txt");
 cout << "***2006 New York Yankees Batting Statistics***\n\n";
 cout << "PLAYER"<< setw(10) << "AB\n";
 
 while(!Open.eof())
 {
 Open >> players >> ab;
 cout <<" "<<players << setw(5) <<ab<< endl;
 }
 
 Open.close();

}

output looks something like this:

D.Jeter 623
A.Rodriguez 572
J.Damon 593
J.Posada 465
J.Giambi 446
B.Williams 420
M.Cabrera 460
R.Cano 482
A.Pillips 246
M.Cairo 222
B.Crosby 87
B.Abreu 209
H.Matsui 172


..not exactly like that, but you get the idea....any help would be great, thanks.
  #2  
Old 02-May-2007, 14:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,641
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to make columns when reading in files???


Quote:
Originally Posted by jtrink
Hey how would i make columns when im reading in a text document?

I think you mean when you are writing the document, right?

Well, I usually set up titles and column headers visually. Just diddle around with a text editor until it looks right and then put the literal strings in output statements.

To line up names, I would probably left-justify them, and numbers are usually right-justified.

CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{

    int ab;
    float average; // not used yet
    char players[256];
    char *inname = "Yanks.txt";

    // get output ready to print floating point numbers with three
    // decimal places. Will be used for batting averages.

    cout << setprecision(3) 
         << setiosflags(ios::fixed) 
         << setiosflags(ios::showpoint);

    ifstream infile;

    infile.open(inname);
    if (!infile) {
        cout << "There was a problem opening " << inname << " for reading."
             << endl;
        return 0;
    }
    cout << "***2006 New York Yankees Batting Statistics***" << endl << endl;
    cout << "         PLAYER                 AB" << endl;
    cout << "       ----------------------------" << endl;

    while (infile >> players >> ab) {
        cout << "         "                  // leading space to line things up
             << left << setw(20) << players  // names are left-justified
             << right << setw(5) << ab       // "at bat" numbers are right-justified
             << endl;
    }

    infile.close();

    return 0;

}

Output for a couple of names:
Code:
***2006 New York Yankees Batting Statistics*** PLAYER AB ---------------------------- D.Jeter 623 A.Rodriguez 572


Of course you will adjust the widths to accommodate longest possible names and make it "look good" to you --- or your boss or teacher or whoever the heck is the target market (audience) for this product.

By putting each thing on its own line, it's easier (for me, anyhow) to keep track of what changes will affect which items.


Notes:
1. You should always test to see that files are opened properly and probably print some kind of message to indicate problems.

2. You should always test results of read operations to make sure the input stream is still OK. Note that the way you had set up your loop it reads the last line twice (since the eof flag isn't set until after you have tried to read past the end of the file). The value of the expression in the while(...) statement is a boolean that is false if input stream has failed (including the case where the program has tried to read past the end of the file), and the value is true if it's OK so far.

Regards,

Dave
Last edited by davekw7x : 02-May-2007 at 15:00.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bloodshed Dev C++ Project Options JdS CPP / C++ Forum 6 11-Nov-2005 17:23
Need help reading data from files blitzy C Programming Language 5 15-Mar-2005 19:07
GIMcontacts - a gim fltk fluid venture cable_guy_67 FLTK Forum 0 14-Feb-2005 14:18
Reading and writing binary files in certain format Dream86 CPP / C++ Forum 10 06-Aug-2004 10:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.