I am facing a logical error in the function filewrite() but i cant figure it out, it all seems right. The sum of the array elements is shown on the screen but when it is written in the file, it is not in the correct format.
#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
void enter(double array[][5]);/*Getting input for array elements*/
void display(double array[][5]);/*Displaying Array Elements*/
void sum(double r[][5]);
void filewrite(double r[][5], double su[5]);
/*Main function starting here*/
main()
{
double su[5];
double r[2][5];
enter(r);
display(r);
sum(r);
filewrite(r,su);
cout<<endl;
system("pause");
}
/*Functrion for getting the input for array elements*/
void enter (double r[][5])
{
for(int i= 0;i < 1;i++)
{
for(int j=0;j<5;j++)
{
cout<<endl<<"Please enter Numbers for position "<<"["<<i<<","<<j<<"] : ";
cin>>r[i][j];
}
}
for (int i = 1;i<2;i++)
{
for(int j= 0;j<5;j++)
{
cout<<endl<<"Please enter numbers for position "<<"["<<i<<","<<j<<"] : ";
cin>>r[i][j];
}
}
}
/*function for displaying the Array Elements*/
void display(double r[][5])
{
for(int i = 0;i<2;i++)
{
for (int j=0; j<5;j++)
{
cout<<endl<<"The number saved in the position "<<"["<<i<<","<<j<<"] : "<<" is "<<r[i][j];
}
}
}
void sum(double r[][5])
{
cout<<endl;
double su[5] = {0};
for (int i = 0; i<5;i++)
{
su[i] = r[0][i] + r[1][i];
cout<<"The sum of element [0, "<<i<<"] and [1, "<<i<<"] is :"<<su[i]<<endl;
}
}
/*Function to write the array elements in the file start here*/
void filewrite(double r[][5], double su[])
{
ofstream re;
re.open("2d array.txt");
if(re.is_open())
{
cout<<"File Created"<<endl;
re<<"\t\t"<<"1 Column"<<"\t"<<"2 Column"<<"\t"<<"3 Column"<<"\t"<<"4 Column"<<"\t"<<"5 Column";
re<<"\n";
re<<"First Row";
for(int i = 0 ;i<5;i++)
{
re<<"\t"<<r[0][i]<<"\t";
}
re<<"\n";
re<<"Second Row";
for(int i=0;i<5;i++)
{
re<<"\t"<<r[1][i]<<"\t";
}
re<<"\n";
re<<"Sum";
for(int i=0;i<5;i++)
{
re<<"\t"<<su[i]<<"\t";
}
cout<<"First Row Written in the file"<<endl;
cout<<"Second Row writen in the file"<<endl;
cout<<"Sum Written in the file"<<endl;
}
re.close();
}
`