Formatting output when some names are longer than others

This is a discussion on Formatting output when some names are longer than others within the C++ Programming forums, part of the General Programming Boards category; I wrote a program to read names and program scores from a text file and the output has to be ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    Formatting output when some names are longer than others

    I wrote a program to read names and program scores from a text file and the output has to be formatted into columns evenly like so:

    Code:
    Snow White            89.50       89.00      B
    Sleeping Beauty       89.50       89.00      B
    Currently it looks like this:

    Code:
    Snow White          89.50              89.00          B
    Sleeping Beauty               89.50              89.00         B
    I can't adjust the lengths to the individuals names because it's read from a file, I've played around with different setw() lengths and left and right alignment and can't get it to format properly. Here's the code:

    Code:
      cout<<left<<firstName<<" "<<lastName
          <<right
          <<setw(15)<<average1
          
          <<setw(10)<<average2
    
          <<setw(10)<<average3
      
          <<setw(6)<<grade<<endl;
    Any help would be greatly appreciated!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,780
    Quote Originally Posted by mkaynutty
    I can't adjust the lengths to the individuals names because it's read from a file
    Ah, but you can. For example, you could save all the data into a vector of records, then find the length of the longest name. This will then be used as the basis to set the field width when printing the name.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    But you haven't actually set a width for the names. Why not concatenate the first and last names (with a space between) before the cout statement and then set a width.
    Code:
    string name = firstName + " " + lastName;
    
    cout << left << setw(25) << name
         << right
         << setw(10) << average1
         << ...
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    I tried out your suggestion oogabooga and it worked! I just found I have to enter a loop in there as well and I can't figure out which one to use. The grades and averages shouldn't come up until you press any key to continue. Right now mine come up as soon as I execute the program. I was looking at trying a flag controlled while loop, do you have any suggestions?

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You'd have to post your code.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    Code:
    #include <iostream>
    #include<iomanip>
    #include<fstream>
    using namespace std;
    
    int main(){
        system("color f0");
      
      cout<<"**************************************************"<<endl;
      cout<<"*    IT210 Business Applicatins with C++         *"<<endl;  
      cout<<"*           Date: March 19, 2012                 *"<<endl;
      cout<<"*           Program Assignment 3                 *"<<endl;
      cout<<"*              Student Grades II                 *"<<endl;
      cout<<"*                                                *"<<endl;
      cout<<"*     This program reads student informantion    *"<<endl;
      cout<<"*     from an input text file and ouputs results *"<<endl; 
      cout<<"*     to the monitor and the ouput text file.    *"<<endl; 
      cout<<"**************************************************"<<endl;
      
      cout<<endl;
      cout<<"Welcome to Student Grade Calculator II"<<endl<<endl;
      
    
     
     
      ifstream fin;
      ofstream fout;
      
      double program1, program2, program3, program4, program5;
      double test1, test2;
      double average1, average2, average3;
      string firstName, lastName;
      char grade;
    
     
     
    
    
      cout<<"Examine the input text file (input3.txt) before you proceed"<<endl<<endl;
      
      fin.open("input3.txt");
      fout.open("input3_ouput.txt"); 
        
    
      if(!fin){
               cout<<"Input failure\n";
               system("pause");
               return 1;
               }
      if(!fout){
                cout<<"Output failure\n";
                system("pause");
                return 1;
                }
    
    
    
      
      
      cout<<"123456789012345678901234567890123456789012345678901234567890"<<endl;
      cout<<"=============================================================="<<endl;
      cout<<left<<setw(12)<<"Student Names"
                <<setw(7)<<" "
                <<setw(7)<<"Program"
                <<setw(3)<<" "
                <<setw(4)<<"Test"
                <<setw(6)<<" "
                <<setw(6)<<"Course"
                <<setw(4)<<" "
                <<setw(6)<<"Letter"<<endl;
      cout<<left<<setw(20)<<" "
                <<setw(6)<<"Average"
                <<setw(3)<<" "
                <<setw(6)<<"Average"
                <<setw(3)<<" "
                <<setw(6)<<"Average"
                <<setw(3)<<" "
                <<setw(5)<<"Grade"<<endl;
      cout<<"=============================================================="<<endl;
      
      
      while(fin){
      fin>> firstName>>lastName;
      
        fin>>program1
         >>program2
         >>program3
         >>program4
         >>program5;
        
        fin>>test1>>test2;
         
      average1=(program1+program2+program3+program4+program5)/5;
      average2=(test1+test2)/2;
      average3=(average1+average2)/2;
      
      if(average3>=90){grade='A';}
      else if(average3>=80 && average3<=89){grade='B';}
      else if(average3>=70 && average3<=79){grade='C';}
      else {grade='F';}
      
      
     string name;
     
     name=firstName+" "+lastName;
    
      cout<<fixed<<showpoint;
      cout<<setprecision(2);
        
      cout<<left<<setw(20)<<name
    
          <<setw(10)<<average1
          
          <<setw(10)<<average2
    
          <<setw(10)<<average3
      
          <<setw(6)<<grade<<endl;
      
      if(fin.peek()=='\n')fin.ignore();
    }
       
    
      //Output to text file
      
      fout<<"**************************************************"<<endl;
      fout<<"*    IT210 Business Applicatins with C++         *"<<endl;  
      fout<<"*           Date: March 19, 2012                 *"<<endl;
      fout<<"*           Program Assignment 3                 *"<<endl;
      fout<<"*              Student Grades II                 *"<<endl;
      fout<<"*                                                *"<<endl;
      fout<<"*     This program reads student informantion    *"<<endl;
      fout<<"*     from an input text file and ouputs results *"<<endl; 
      fout<<"*     to the monitor and the ouput text file.    *"<<endl; 
      fout<<"**************************************************"<<endl;
      
      fout<<endl;
    
        fout<<"Welcome to Student Grade Calculator II"<<endl<<endl;
      
      fout<<"123456789012345678901234567890123456789012345678901234567890"<<endl;
      fout<<"=============================================================="<<endl;
      fout<<left<<setw(12)<<"Student Names"
                <<setw(7)<<" "
                <<setw(7)<<"Program"
                <<setw(3)<<" "
                <<setw(4)<<"Test"
                <<setw(6)<<" "
                <<setw(6)<<"Course"
                <<setw(4)<<" "
                <<setw(6)<<"Letter"<<endl;
      fout<<left<<setw(20)<<" "
                <<setw(6)<<"Average"
                <<setw(3)<<" "
                <<setw(6)<<"Average"
                <<setw(3)<<" "
                <<setw(6)<<"Average"
                <<setw(3)<<" "
                <<setw(5)<<"Grade"<<endl;
      fout<<"=============================================================="<<endl;
      
    
    
      
      
    fin.close();
    fout.close();
      
      
     
        
        
        
        system ("pause");
        return 0;
    }//end of main

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Maybe you need to read the data into an array of structs.
    Code:
    struct Student {
      double program1, program2, program3, program4, program5;
      double test1, test2;
      double average1, average2, average3;
      string firstName, lastName;
      char grade;
    };
    const int MAX_STUDENTS = 100;  // set to an appropriate value
    Student students[MAX_STUDENTS];
    
    int i = 0;
    while (fin) {
        fin >> students[i].firstName;
        if (!fin)
            break;
        fin >> students[i].lastName;
    
        ...
    
        i++;
    }
    int nStudents = i;
    
    // Wait for a keypress, if you want.
    
    // Then do your printing in another loop here
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Note, this does not address your question but rather another observed flaw:
    Code:
    while(fin){
        fin>> firstName>>lastName;
       
        fin>>program1
         >>program2
         >>program3
         >>program4
         >>program5;
         
        fin>>test1>>test2;
    
        ...
    This suffers from the same flaw as testing EOF in your loop conditional. By itself, while(fin) tests the state of the ifstream object fin. You are using this to tell yourself when to stop the loop but like testing EOF, the fin object test will only return false after an attempt to read has failed and at that point you will already be inside of the loop and processing data. The solution, as with the recommended fix for the use of EOF in such a conditional test, is to instead test the result of the read operation directly:
    Code:
    while(fin>> firstName>>lastName>>program1>>program2>>program3>>program4>>program5>>test1>>test2)
    {
        ...
    [edit]oogabooga's post above avoids this issue with a second test of the fin object after the first read whereby you break out of the loop if the test is false. This is also acceptable.[/edit]
    Last edited by hk_mp5kpdw; 03-21-2012 at 01:01 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting Output
    By m0nster in forum C Programming
    Replies: 5
    Last Post: 03-07-2011, 12:37 PM
  2. formatting output
    By kisiellll in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 03:53 PM
  3. Output formatting
    By C+/- in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2008, 07:29 AM
  4. formatting output
    By z.tron in forum C++ Programming
    Replies: 5
    Last Post: 11-22-2002, 05:11 PM
  5. Output formatting
    By Beginner2002 in forum C Programming
    Replies: 3
    Last Post: 09-05-2002, 06:59 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21