Using ofstream in function and in main

Hi all, I am trying to write a function that writes data to a txt file but my main program also needs to write to that file. The way i have tried to do it is highlighted with double slashes, but having two ofstreams doesnt work. Can anyone suggest anything?

The purpose of the function is to align the data in the txt file so it is more readable.

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
  #include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<vector>

using namespace std;

//void align( int i, int a, double k, int b, float lfloat, double ldouble);

int main(){

ofstream silver("silver.txt");

int N;                         // Value of the indice used for silver ratio
vector<float>  lfloat(51);     // For the float data type calculation of recurrence relation (51 as it will be up to N=50)
vector<double> ldouble(51);    // For the double data type calculation of recurrence relation (51 as it will be up to N=50)
 
lfloat[0]=1.0;                 // Silver ratio to the power of zero, needs to be intialised for recurrence relation
ldouble[0]=1.0;  
lfloat[1]=(-1+sqrt(5))/2;      // Silver ratio to the power of one, needs to be intialised for recurrence relation
ldouble[1]=(-1+sqrt(5))/2;

cout<< "Please enter your indice (non-negative and no greater than 50)" << endl;
cin>> N ;

 while (!cin || N<0 || N>50){  // Checks user input was correct
  cin.clear();
  cin.ignore();
  cout<< "Incorrect input, please enter a non negative indice of value no greater than 50" << endl;
  cin>> N;
 }

silver<< "Indice Value" << setw(15) << "Actual Value" << setw(37) << "Recurrence Value (Float)" << setw(31) << "Recurrence Value (Double)" << endl;

 for (int i=0 ; i<=N; i++){    // Calculates exact value of silver ratio and recurrence relation for given indice

   double k= pow(((-1+sqrt(5))/2),i) ;   // The silver ratio to ith power
  
    if (i>=2){

     lfloat[i]=lfloat[i-2]-lfloat[i-1] ;     // The recurrence relation for float data type
     ldouble[i]= ldouble[i-2]-ldouble[i-1];  // The recurrence relation for double data type  
    }
   
    if (lfloat[i]<0 && ldouble[i]>0){
     //int a=24;
     //int b=31;
     //align(i, a, k, b, lfloat[i], ldouble[i]);
     silver<< setw(15) << left << i << setprecision(15) << setw(24) << left << k << setw(31) << left << lfloat[i] << setw(35)
     << left << ldouble[i] << endl;
    }

    else if (lfloat[i]>0 && ldouble[i]<0){
     
     //int a=24;
     //int b=31;
     //align(i, a, k, b, lfloat[i], ldouble[i]);
     silver<< setw(15) << left << i << setprecision(15) << setw(25) << left << k << setw(29) << left << lfloat[i] << setw(35)
     << left << ldouble[i] << endl;
    }

    else if (lfloat[i]<0 && ldouble[i]<0){

     //int a=24;
     //int b=31;
     //align(i, a, k, b, lfloat[i], ldouble[i]);
     silver<< setw(15) << left << i << setprecision(15) << setw(24) << left << k << setw(30) << left << lfloat[i] << setw(35)
     << left << ldouble[i] << endl;
    }

    else{
     
     //int a=24;
     //int b=31;
     //align(i, a, k, b, lfloat[i], ldouble[i]);
     silver<< setw(15) << left << i << setprecision(15) << setw(25) << left << k << setw(30) << left << lfloat[i] << setw(35)
     << left << ldouble[i] << endl;
    }
 }
 
silver.close();

cout<< "The data has been written to 'silver.txt'" << endl;

return 0;
}

//void align(int i, int a, double k, int b, float lfloat, double ldouble){

//ofstream silver("silver.txt");

//      cout<< setw(15) << left << i << setprecision(15) << setw(a) << left << k << setw(b) << left << lfloat << setw(35)
      //<< left << ldouble << endl;
//}


Your function should accept a std::ostream by reference, and main should pass the file stream as that parameter.
Topic archived. No new replies allowed.