Jordan Molina
Jordan Molina

Reputation: 27

Cout works but ofstream doesnt

I am trying to return pairs of coordinates whos values are being read from an input file and displayed on an output file outFile. However, nothing is placed on to the outFile stream.

ignore the area and distance values for now I'm not done fixing those

The distance between  and  is 6.9
The distance between  and  is 3.0
The distance between  and  is 6.9
The area of this triangle is: 10.1

When using cout the values are displayed in console.

void getPoint(ifstream &inFile, double x1, double y1){
    ofstream outFile;
    inFile >> x1 >> y1;
    cout << fixed << setprecision(1) << "(" << round_off(x1,1) << "," << round_off(y1,1) <<")" << endl;
}   
//Prints coordinates appropriately in console(1.0,1.2)
                                             (6.0,6.0)
                                             (6.0,6.0)
                                             (3.0,6.5)
                                             (1.0,1.2)
                                             (3.0,6.5)

When using outFile, the coordinates are not placed on to the output file and nothing returns. (see in first pic)

void getPoint(ifstream &inFile, double x1, double y1){
    ofstream outFile;
    inFile >> x1 >> y1;
    outFile << fixed << setprecision(1) << "(" << round_off(x1,1) << "," << round_off(y1,1) <<")" << endl;
} //No values displayed in outFile

Without using the output file as a reference parameter in getPoint (if that even solves it idk), how can I display/return a coordinate (x,y) from getPoint() onto the output file? Note:

outFile << fixed << setprecision(1) << "(" << round_off(x1,1) << "," << round_off(y1,1) <<")" << endl;

works in function main but I want it to work in respect to getPoint's arguments.

Upvotes: 1

Views: 157

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

The declarations

    ofstream outFile;

in the functions main and getPoint are shadowing (hiding) the global

ofstream outFile;

and preventing it from printing things to output file. You should remove them to have the functions use the global outFile if you hate using reference parameter for some reason.

Upvotes: 2

Related Questions