Output is nt showing even though the file is open

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;

//Mean calculation

double avg (double x) {
double count=0,avg,i,stud[20],sum=0;
string inname="input.txt";
ifstream infile (inname);
ofstream outfile ("output.txt");
for (int k=0;k<20;k++) {
while (infile>>i) {
count++;
stud[k]=i;
sum=sum+stud[k];
}
}
x=sum/count;
return x;
}

//Std.Deviation Calculation

double stdviation (double x) {
double i,count=0,var,stud[20],add,sum=0,pwr_sum;
double dviation,power;
string inname="input.txt";
ifstream infile (inname);
ofstream outfile ("output.txt");
for (int k=0;k<20;k++) {
while (infile>>i) {
count++;
stud[k]=i;
stud[k]=pow(stud[k],2);
sum=sum+stud[k];
}
}
power=pow(avg(x),2);
var=sum/count;
var=var-power; // This will be the variance
dviation=sqrt(var);
return dviation;
}

//Barchart Drawing

void barchart () {
string inname="input.txt";
ifstream infile (inname);
ofstream outfile ("output.txt");
double stud[20],countA=0,countB=0,countC=0,countD=0;
double countF=0,i;
if (outfile.is_open()) {
for (int x=0;x<30;x++) {
while (infile>>i) {
stud[x]=i;
if(stud[x]<40){
countA++;
}else if(stud[x]<50){
countB++;
}else if(stud[x]<70){
countC++;
}else if(stud[x]<80){
countD++;
}else if(stud[x]<=100){
countF++;
}
outfile.close();
}
}

}
cout<<endl;
outfile<<"Grade"<<setw(15)<<"Number"<<endl;
cout<<"A"<<setw(15);;
outfile<<"A"<<setw(15);
for(int numA=0;numA < countA;numA++){
outfile<<"*";
cout<<"*";
}
cout<<endl;
outfile<<endl;
cout<<"B"<<setw(15);
outfile<<"B"<<setw(15);
for(int numB=0;numB < countB;numB++){
outfile<<"*";
cout<<"*";
}
cout<<endl;
outfile<<endl;
cout<<"C"<<setw(15);
outfile<<"C"<<setw(15);
for(int numC=0;numC < countC;numC++){
outfile<<"*";
cout<<"*";
}
cout<<endl;
outfile<<endl;
cout<<"D"<<setw(15);
outfile<<"D"<<setw(15);
for(int numD=0;numD < countD;numD++){
outfile<<"*";
cout<<"*";
}
outfile<<endl;
cout<<endl;
outfile<<"F"<<setw(15);
cout<<"F"<<setw(15);
for(int numF=0;numF < countF;numF++){
outfile<<"*";
cout<<"*";
}
cout<<endl;
cout<<endl;
cout<<"Take note that Grade A is for 80>, Grade B 70>, Grade C 50>,"<<endl;
cout<<" Grade D 40>, Grade F 0>.";
cout<<endl;
}

//Main function
void main () {
double count=0,i,stud[20],sum=0,x=0;
string inname="input.txt";
ifstream infile (inname);
ofstream outfile ("output.txt");
if (!infile && outfile) {
cout<< "There was a problem loading file "<<inname
<<"for reading."<<endl;
}
for (int k=0;k<20;k++) {
while (infile>>i) {
count++;
stud[k]=i;
cout<<"Mark of student "<<count<<" : "
<<stud[k]<<endl;
}
}
cout<<endl;
cout<<"Your mean is "<<avg(x)<<endl;
outfile<<"Your mean is "<<avg(x)<<endl;
cout<<"Your std deviation is "<<stdviation(x)<<endl;
barchart ();
cout<<endl;
system ("pause");
}


Above is my program where i need to read a sequence of marks from txt.file and calculate its mean and std deviation and then write on another txt file. I'm having problem in writing the data onto txt.files now. It's showing blank page. Can anyone tell me what gone wrong in the code above ??
Please use code tags.
EDIT: You are pening your file several times in different piints in your program. Default settings will rewrite file everytime it opens.
Last edited on
Topic archived. No new replies allowed.