Formatting output and Output text file issue

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

#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;
}//end of main
first:
1
2
system("color f0");
system ("pause");
please do not use this, system functions are bad and do not work the same way for different systems, for example, my comp(macOS) does not find a "pause" command and does not run the program.
second:
while(fin) does not work for me, so the content in the while loops is not actually done. if you want it to go until end of file do something like fin.eof() but i am not sure what you are trying to accomplish there.
instead of
1
2
3
4
5
6
7
8
9
10
  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;

use
1
2
3
4
5
6
7
8
9
10
  cout<<"**************************************************"<<endl
      <<"*    IT210 Business Applicatins with C++         *"<<endl  
      <<"*           Date: March 19, 2012                 *"<<endl
      <<"*           Program Assignment 3                 *"<<endl
      <<"*              Student Grades II                 *"<<endl
      <<"*                                                *"<<endl
      <<"*     This program reads student informantion    *"<<endl
      <<"*     from an input text file and ouputs results *"<<endl 
      <<"*     to the monitor and the ouput text file.    *"<<endl 
      <<"**************************************************"<<endl;


I know it doesn't help you with your actual problem but you did say ANY help would be greatly appreciated
I have to use the system functions because that's how I was taught and if not I'd probably get points taken off on my assignment. Where would I put the fin.eof() and what does exactly mean?

@Jadax Thank you, that would make my life much easier, specially when it comes to test time.
umm, does your teacher say "use system functions" because they are HUGE holes in your program that are OS specific. and .eof() is a boolean function that determines if the end of the file (eof) has been reached, and then returns a true value. so you would do while(!fin.eof()) // while not end of fin
No those aren't his exact words but regardless I am in a beginner course and I'm sure as I continue on to different classes I'll learn about these holes and different ways to accomplish the same thing. Thanks for explaining the .eof() I'll add it into my code.
.eof() is a boolean function that determines if the end of the file (eof) has been reached, and then returns a true value. so you would do while(!fin.eof()) // while not end of fin


You will hardly ever do this, because it is very rarely the right thing to do. It certainly isn't here. What he did in the control expression is the correct thing to do.
Last edited on
qsort ← {1≥⍴⍵:⍵⋄e←⍵[?⍴⍵]⋄ (∇(⍵<e)/⍵) , ((⍵=e)/⍵) , ∇(⍵>e)/⍵}
qsort 1 3 5 7 9 8 6 4 2
1 2 3 4 5 6 7 8 9
Of course, in real APL applications, one would use ⍋ to sort (which will pick a sorting algorithm suited to the argument).

- Sometimes I just want to say, that one should pick the right tools for a job.
In this example of a merge sort program the one line comment is longer than the program.
mKay, would you please post a copy of the input & output files;
fin.open("input3.txt");
fout.open("input3_ouput.txt");


I see, and this may only be on my machine, that I have trouble with fin and fout with STRING vars. In particular, 'lastname' and 'firstname'.
fin & fout work on DOUBLES just fine.



mKay, since all of your averages are of fixed length ( nn.nn) then the spaces between those averages can also be fixed in size.
However because your names (firstname, lastname) are variable , then by starting your first average at a fixed distance from the end of the name, then the averages seem to move to the left, and cause the line, in full, to look jagged.

A solution for this is to determine, or set an arbitrary max size for the vars firstname & lastname , for example firstname(8), lastname (11)- since you currently only have 20-21 spaces before the "PROGRAM" header,

Create a variable for the space - rather than a fixed size of 12
For each name (first & last) - subtract that size (or length) from 21
and assign that value to the space-variable

for example: "Robert Johnsen" contains 14 chars
" Bob Able" contains 8 chars
In each case you want to begin the first AVG in position 22

so for Robert, subtract 14 from 22 and set the SpaceVar to 8.
In Bob's case subtract 8 from 22 and set the SpaceVar to 14.

int SpaceVar 0 ;

SpaceVar = 8;

SpaceVar = 14;


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;



cout<<left<<firstName<<" "<<lastName
<<setw(SpaceVar)<<" "
<<setw(5)<<average1
<<setw(3)<<" "
<<setw(5)<<average2
<<setw(5)<<" "
<<setw(5)<<average3
<<setw(5)<<" "
<<setw(1)<<grade<<endl;
Topic archived. No new replies allowed.