Hey everyone,
I want to print data to a txt file using a void function outside the main. The problem is that it only prints the last iteration of the loop calling these function.
it only prints: a: 3 b: 4 c: 5
and a couple more things that look like garbage underneath this line of numbers
void printfunction(int a, int b, int c)
{
fstream outfile;
outfile.open("table.txt");
outfile<<"a: "<<a<<"b :"<<b<<"c: "<<c<<endl;
}
int main()
{
fstream outfile;
for ( int i = 0; i <2; i ++)
{
int a = 0, b = 1, c = 2;
a = a + i;
b = b + i;
c= c + i;
fstream outfile;
printfunction(a,b,c);
void printfunction(int a, int b, int c)
{
fstream outfile;
outfile.open("table.txt", ios_base::app); //set file pointer to the end of the file
outfile<<"a: "<<a<<"b :"<<b<<"c: "<<c<<endl;
outfile.close();
}
Also, there's no need to instantiate the outfile in main() since it's not being used there.
that did not do do the trick , i still get the same results and if i omit the fstream outfile form main(), it does not compile, error: outfile was not declared in the scope