Need help redirecting my output to a file , cant seem to get the print to redire
ID: 3806113 • Letter: N
Question
Need help redirecting my output to a file , cant seem to get the print to redirect to a file.
#include<iostream>
#include<fstream>
#include <algorithm>
using namespace std;
int subject[100];
double average[100];
double daysexercise[100];
double est_maximum[100];
double ratio1[100];
double ratio2[100];
int c=0;
void getdata(ifstream &in)
{
int maximum;
int age;
double sum=0;
// Read file
while(!in.eof())
{
double aver[5];
double maxi[5];
double exe[5];
// First line
in>>subject[c];
in>>maximum;
in>>age;
// Next 5 lines read to array
for(int i=0;i<5;i++)
{
in>>aver[i];
in>>maxi[i];
in>>exe[i];
}
int cou=0;
// Average calculation
for(int i=0;i<5;i++)
{
if(aver[i]==-1)
sum=sum+0;
else
{
sum=sum+aver[i];
cou++;
}
}
double avg=sum/cou;
average[c]=avg;
cou=0;
// Number of days exercised
for(int i=0;i<5;i++)
{
if(exe[i]!=0)cou++;
}
daysexercise[c]=cou;
// Estimated maximum
est_maximum[c]=220-age;
// Ratio Calculation
ratio1[c]=(maximum/est_maximum[c])*100;
double max=0;
// Find maximum value in the array
for(int i=0;i<5;i++)
{
if(maxi[i]>max)
max=maxi[i];
}
// Ratio Calculation
ratio2[c]=(max/maximum)*100;
c++;
}
}
void print(int subjectSorted[] )
{
cout<< "COMMUTING AND EXERCISE HEART RATE SUMMARY"<<endl<<endl;
cout<< "Subject Number Average Commutting HR Day Exercise Estimated MAX HR % Measure To Estimated MAX HR % Max Commutting HR TO Measured "<<endl<<endl;
for(int i=0;i<c;i++)
{
for(int j=0;j<c;j++)
{
if(subjectSorted[i]==subject[j])
{
cout<<subject[j]<<" "<<average[j]<<" "<<daysexercise[j]<<" "<<est_maximum[j]<<" "<<ratio1[j]<<" "<<ratio2[j]<<endl;
}
}
}
}
int main()
{
ifstream in;
// Open the file
in.open("HR.txt");
// read data from the file and commute
getdata(in);
int subjectSorted[100];
// Copy array subject to subjectSorted
for(int i=0;i<c;i++)
{
subjectSorted[i]=subject[i];
}
// Sort the array
sort(subjectSorted,subjectSorted+c);
// Print the output
print(subjectSorted);
system("pause");
return 0;
}
Explanation / Answer
******If you want to show output then please give input file.*******
Program: redirecting output to a file. which parts are bold.
#include<iostream>
#include<fstream>
#include <algorithm>
using namespace std;
int subject[100];
double average[100];
double daysexercise[100];
double est_maximum[100];
double ratio1[100];
double ratio2[100];
int c=0;
void getdata(ifstream &in)
{
int maximum;
int age;
double sum=0;
// Read file
while(!in.eof())
{
double aver[5];
double maxi[5];
double exe[5];
// First line
in>>subject[c];
in>>maximum;
in>>age;
// Next 5 lines read to array
for(int i=0;i<5;i++)
{
in>>aver[i];
in>>maxi[i];
in>>exe[i];
}
int cou=0;
// Average calculation
for(int i=0;i<5;i++)
{
if(aver[i]==-1)
sum=sum+0;
else
{
sum=sum+aver[i];
cou++;
}
}
double avg=sum/cou;
average[c]=avg;
cou=0;
// Number of days exercised
for(int i=0;i<5;i++)
{
if(exe[i]!=0)cou++;
}
daysexercise[c]=cou;
// Estimated maximum
est_maximum[c]=220-age;
// Ratio Calculation
ratio1[c]=(maximum/est_maximum[c])*100;
double max=0;
// Find maximum value in the array
for(int i=0;i<5;i++)
{
if(maxi[i]>max)
max=maxi[i];
}
// Ratio Calculation
ratio2[c]=(max/maximum)*100;
c++;
}
}
void print(int subjectSorted[] ,ofstream &out)
{
out<< "COMMUTING AND EXERCISE HEART RATE SUMMARY"<<endl<<endl;
out<< "Subject Number Average Commutting HR Day Exercise Estimated MAX HR % Measure To Estimated MAX HR % Max Commutting HR TO Measured "<<endl<<endl;
for(int i=0;i<c;i++)
{
for(int j=0;j<c;j++)
{
if(subjectSorted[i]==subject[j])
{
out<<subject[j]<<" "<<average[j]<<" "<<daysexercise[j]<<" "<<est_maximum[j]<<" "<<ratio1[j]<<" "<<ratio2[j]<<endl;
}
}
}
}
int main()
{
ifstream in;
ofstream out;//out put file
// Open the file
in.open("HR.txt");
// read data from the file and commute
getdata(in);
int subjectSorted[100];
// Copy array subject to subjectSorted
for(int i=0;i<c;i++)
{
subjectSorted[i]=subject[i];
}
// Sort the array
sort(subjectSorted,subjectSorted+c);
out.open("Output.txt");//create and open output file
// Print the output on file
print(subjectSorted,out);
system("pause");
return 0;
}