Thread: writing to output file

  1. #1
    Unregistered
    Guest

    writing to output file

    here is my code

    for (x=0; x< NOSTUD; x++){
    oFile.setf(ios::left);
    oFile<<setw(9)<<(string)ssn[x]<<" "<<setw(19);
    oFile.setf(ios::left);
    oFile<<(string)name[x];
    oFile.setf(ios::right);
    oFile<<" "<<setw(7)<<setprecision(2)<<(double)average[x]<<setw(7)<<setprecision(2)<<z_score[x]<<setw(12)<<percentile[x]<<endl;

    }


    here is my output

    Michael smith
    Student Scores

    -------------------+----+----+----+----+----+----+----+----+----+----+
    SSN Name Average z-score Percentile
    -------------------+----+----+----+----+----+----+----+----+----+----+
    120555555 smith,elaine 98.00 1.00 100
    222222222 smith,michael 93.00 -1.00 0
    -------------------+----+----+----+----+----+----+----+----+----+----+
    Average Std Dev
    95.50 2.50


    How can I get the second name to left justify

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Your code works on my compiler. The name array outputs left-justified. Maybe try:
    oFile.setf(ios::left,ios::adjustfield);
    Last edited by swoopy; 01-08-2002 at 02:14 PM.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194

    Re: writing to output file

    Originally posted by Unregistered
    here is my code

    for (x=0; x< NOSTUD; x++){
    oFile.setf(ios::left);
    oFile<<setw(9)<<(string)ssn[x]<<" "<<setw(19);
    oFile.setf(ios::left);
    oFile<<(string)name[x];
    oFile.setf(ios::right);
    oFile<<" "<<setw(7)<<setprecision(2)<<(double)average[x]<<setw(7)<<setprecision(2)<<z_score[x]<<setw(12)<<percentile[x]<<endl;

    }
    I believe that second call to oFile.setf(ios::left); is unnecessary. Once you set a field to left or right, it will stay that way until you change if. Also i believe you should output (string)name[x] right after you setw(19). So just get rid of that 2nd ios::left call, and see if that fixes it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM