1,105,363 Community Members

Formatted output and Output text files

Member Avatar
mkaynutty
Newbie Poster
15 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am having trouble formatting my output into columns. Some of the the names are longer than others so it pushes the whole line out instead of lining up evenly. I cannot use the printf function, we haven't learned this in class yet, I'm only familiar with setw and setfill. I'm also having trouble getting my output to copy to a text file. Half of the output is sent there but the other half with the actual output of the names, averages, and grades are not. Any help would be greatly appreciated!

Here's the 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;


  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';}



  cout<<fixed<<showpoint;
  cout<<setprecision(2);

  cout<<left<<firstName<<" "<<lastName
      <<setw(12)<<" "
      <<setw(5)<<average1
      <<setw(3)<<" "
      <<setw(5)<<average2
      <<setw(5)<<" "
      <<setw(5)<<average3
      <<setw(5)<<" "
      <<setw(1)<<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;


  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';}



  fout<<fixed<<showpoint;
  fout<<setprecision(2);

  fout<<left<<firstName<<" "<<lastName
      <<setw(12)<<" "
      <<setw(5)<<average1
      <<setw(3)<<" "
      <<setw(5)<<average2
      <<setw(5)<<" "
      <<setw(5)<<average3
      <<setw(5)<<" "
      <<setw(1)<<grade<<endl;

  if(fin.peek()=='\n')fin.ignore();
}//end of while


fin.close();
fout.close();






    system ("pause");
    return 0;
}
Member Avatar
lawrenca37
Newbie Poster
1 post since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I don't see anything immediately bad. but your code lacks any error checking of the input size to the columns size or any value checking, also what is the width of the screen/printer your outputing to, is it 80, 120....how does that compare to your line length

Member Avatar
mkaynutty
Newbie Poster
15 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm not sure I understand what you mean. How would I build an error check into the program for that?

Right now it's outputting as:

Snow White               84.50         86.50        B
Sleeping Beauty                 84.50          86.50     B

And I need it to look like this:

Snow White               84.50         86.50        B
Sleeping Beauty          84.50         86.50        B

I can't adjust it to the idivdiual names because I am reading it from a file. I've played around with all types of setw lentghs and left and right alignment.

Question Answered as of 2 Years Ago by lawrenca37
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: