Program fails to display output
Dec 9, 2013 at 3:48am UTC
how do i display the output ? This program only saves the output to "gpa.txt" I want it to also display the output to cmd.
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
const int SIZE = 10;
const string line = "=======================================================================================================================" ;
int sid[SIZE];
string first[SIZE];
string last[SIZE];
float cch[SIZE];
float cgpa[SIZE];
float class1[SIZE];
float class2[SIZE];
float class3[SIZE];
float class4[SIZE];
float class5[SIZE];
float sgpa[SIZE];
float ngpa[SIZE];
float means[9];
float c1[SIZE];
float c2[SIZE];
float c3[SIZE];
float c4[SIZE];
float c5[SIZE];
string header;
float sum = 0;
float credit = 15;
float points1,points2,points3,points4,points5,points6,points7,points8,points9,points10;
//Function prototypes
void readdata();
float mean(float []);
void savemean();
void point(float []);
void assignPoints();
void sgpaCalc();
void ngpaCalc();
void output();
ifstream fin;
ofstream fout;
int main()
{
readdata();
assignPoints();
sgpaCalc();
ngpaCalc();
output();
return 0;
}
//-----------------------------------------------------------------------------------------------------
// Function reads the data from a file and saves them to arrays.
//-----------------------------------------------------------------------------------------------------
void readdata()
{
fin.open("student.txt" );
getline(fin,header);
for (int i = 0; i < SIZE; i++)
fin >> sid[i] >> first[i] >> last[i] >> cch[i] >> cgpa[i] >> class1[i] >> class2[i] >> class3[i] >> class4[i] >> class5[i];
}
//-----------------------------------------------------------------------------------------------------
// Function calculates the sum and returns the average.
//-----------------------------------------------------------------------------------------------------
float mean(float mean[])
{
sum = 0;
for (int i = 0; i < SIZE; i++)
{
sum += mean[i];
}
return sum/SIZE;
}
//----------------------------------------------------------------------------------------------------
// Function stores the returned averages into the means array
//----------------------------------------------------------------------------------------------------
void savemean()
{
means[0] = mean(cch);
means[1] = mean(cgpa);
means[2] = mean(class1);
means[3] = mean(class2);
means[4] = mean(class3);
means[5] = mean(class4);
means[6] = mean(class5);
means[7] = mean(sgpa);
means[8] = mean(ngpa);
}
//---------------------------------------------------------------------------------------------------
// Function determines the number of grade points for each element in the array
//---------------------------------------------------------------------------------------------------
void point(float a[])
{
if (a[0] >= 90) points1 = 12;
else if (a[0] >= 80) points1 = 9;
else if (a[0] >= 70) points1 = 6;
else if (a[0] >= 60) points1 = 3;
else if (a[0] < 60) points1 = 0;
if (a[1] >= 90) points2 = 12;
else if (a[1] >= 80) points2 = 9;
else if (a[1] >= 70) points2 = 6;
else if (a[1] >= 60) points2 = 3;
else if (a[1] < 60) points2 = 0;
if (a[2] >= 90) points3 = 12;
else if (a[2] >= 80) points3 = 9;
else if (a[2] >= 70) points3 = 6;
else if (a[2] >= 60) points3 = 3;
else if (a[2] < 60) points3 = 0;
if (a[3] >= 90) points4 = 12;
else if (a[3] >= 80) points4 = 9;
else if (a[3] >= 70) points4 = 6;
else if (a[3] >= 60) points4 = 3;
else if (a[3] < 60) points4 = 0;
if (a[4] >= 90) points5 = 12;
else if (a[4] >= 80) points5 = 9;
else if (a[4] >= 70) points5 = 6;
else if (a[4] >= 60) points5 = 3;
else if (a[4] < 60) points5 = 0;
if (a[5] >= 90) points6 = 12;
else if (a[5] >= 80) points6 = 9;
else if (a[5] >= 70) points6 = 6;
else if (a[5] >= 60) points6 = 3;
else if (a[5] < 60) points6 = 0;
if (a[6] >= 90) points7 = 12;
else if (a[6] >= 80) points7 = 9;
else if (a[6] >= 70) points7 = 6;
else if (a[6] >= 60) points7 = 3;
else if (a[6] < 60) points7 = 0;
if (a[7] >= 90) points8 = 12;
else if (a[7] >= 80) points8 = 9;
else if (a[7] >= 70) points8 = 6;
else if (a[7] >= 60) points8 = 3;
else if (a[7] < 60) points8 = 0;
if (a[8] >= 90) points9 = 12;
else if (a[8] >= 80) points9 = 9;
else if (a[8] >= 70) points9 = 6;
else if (a[8] >= 60) points9 = 3;
else if (a[8] < 60) points9 = 0;
if (a[9] >= 90) points10 = 12;
else if (a[9] >= 80) points10 = 9;
else if (a[9] >= 70) points10 = 6;
else if (a[9] >= 60) points10 = 3;
else if (a[9] < 60) points10 = 0;
}
//----------------------------------------------------------------------------------------------------
// Function assigns the grade points to each element of a grade point array for each class
//----------------------------------------------------------------------------------------------------
void assignPoints()
{
point(class1);
c1[0] = points1;
c1[1] = points2;
c1[2] = points3;
c1[3] = points4;
c1[4] = points5;
c1[5] = points6;
c1[6] = points7;
c1[7] = points8;
c1[8] = points9;
c1[9] = points10;
point(class2);
c2[0] = points1;
c2[1] = points2;
c2[2] = points3;
c2[3] = points4;
c2[4] = points5;
c2[5] = points6;
c2[6] = points7;
c2[7] = points8;
c2[8] = points9;
c2[9] = points10;
point(class3);
c3[0] = points1;
c3[1] = points2;
c3[2] = points3;
c3[3] = points4;
c3[4] = points5;
c3[5] = points6;
c3[6] = points7;
c3[7] = points8;
c3[8] = points9;
c3[9] = points10;
point(class4);
c4[0] = points1;
c4[1] = points2;
c4[2] = points3;
c4[3] = points4;
c4[4] = points5;
c4[5] = points6;
c4[6] = points7;
c4[7] = points8;
c4[8] = points9;
c4[9] = points10;
point(class5);
c5[0] = points1;
c5[1] = points2;
c5[2] = points3;
c5[3] = points4;
c5[4] = points5;
c5[5] = points6;
c5[6] = points7;
c5[7] = points8;
c5[8] = points9;
c5[9] = points10;
}
//-------------------------------------------------------------------------------------------------------
// Function calculates and stores the semester GPA
//-------------------------------------------------------------------------------------------------------
void sgpaCalc()
{
for (int i = 0; i < SIZE; i++)
sgpa[i] = (c1[i] + c2[i] + c3[i] + c4[i] + c5[i]) / 15;
}
//-------------------------------------------------------------------------------------------------------
// Function calculates and stores the cumulative GPA
//-------------------------------------------------------------------------------------------------------
void ngpaCalc()
{
for (int i = 0; i < SIZE; i++)
ngpa[i] = (cgpa[i] * cch[i] + sgpa[i] * 15) / (cch[i] + 15);
}
//-------------------------------------------------------------------------------------------------------
// Function outputs all the data to a file
//-------------------------------------------------------------------------------------------------------
void output()
{
fout.open("gpa.txt" );
fout << setw(5) << "NO." << setw(4) << "SID" << setw(15) << "NAME" << setw(14) << "CCH" << setw(9) << "CGPA" << setw(10) << "CLASS1" << setw(9) << "CLASS2" << setw(9) << "CLASS3" << setw(9) << "CLASS4" << setw(9) << "CLASS5" << setw(16) << "SGPA" << setw(11) << "CGPA" << endl;
fout << line << endl;
for (int i = 0; i < SIZE; i++)
{
fout << setprecision(0) << fixed;
fout << setw(4) << (i+1) << setw(6) << sid[i]
<< setw(10) << first[i]
<< setw(9) << last[i]
<< setw(9) << setprecision(0) << fixed <<cch[i]
<< setw(9) << setprecision(2) << fixed <<cgpa[i]
<< setw(9) << setprecision(1) << fixed <<class1[i]
<< setw(9) << setprecision(1) << fixed <<class2[i]
<< setw(9) << setprecision(1) << fixed <<class3[i]
<< setw(9) << setprecision(1) << fixed <<class4[i]
<< setw(9) << setprecision(1) << fixed <<class5[i]
<< setw(17) << setprecision(2) << fixed << sgpa[i]
<< setw(11) << setprecision(2) << fixed << ngpa[i]
<< endl;
}
fout << line << endl << endl;
fout << setw(18) << "Averages" << setw(13);
savemean();
fout << setw(21) << means[0] << setw(8) << means[1] << setw(9) << means[2] << setw(9) << means[3] << setw(9) << means[4] << setw(9) << means[5] << setw(9) << means[6] << setw(17) << means[7] << setw(11) << means[8];
}
Dec 9, 2013 at 6:00am UTC
Hello,
You would need to call a command that writes to stdout (the terminal displays what was written to stdout as well as stderr. The most simple way to do this would be to just call
cout with the same data as what you have written to fout.
Here is a post that I ran into that may help. The following thread is discussing writing to both stdout and a file simultaneously.
http://www.cplusplus.com/forum/general/19489/
At the heart of all this is the concept of streams:
http://en.wikipedia.org/wiki/Standard_streams
Topic archived. No new replies allowed.