Saturday, 15 August 2015

c++ - fstream function doesn't read or open the file -



c++ - fstream function doesn't read or open the file -

i trying utilize fstream assignment think ifstream doesn't open file or reads wrong

my code supposed read points file , give distance, diameter etc...

when run code "11 4 -3 3" in file integer x1,x2,y1 , y2 becomes -858993460 , output 0.00 code works without fstream when input keyboard on screen.

i checked if(!fin) cout >> "cannot open" , output "cannot open" guess doesn't opens..

here code :

#include "stdafx.h" #include <iostream> #include <cmath> #include <iomanip> #include <fstream> using namespace std; double distance (int x1, int x2, int y1, int y2) {double distance; distance = ((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); distance = sqrt(distance); homecoming distance; } double radius (int x1, int x2, int y1, int y2) {double radius; radius = distance(x1,x2,y1,y2); homecoming radius; } double circumference (double radius, double pi) {double circumference; circumference = 2*pi*radius; homecoming circumference;} double area (double radius, double pi) { double area; area = pi*radius*radius; homecoming area;} int main() { ifstream fin; fin.open("textfile1.txt"); int x1; int x2; int y1; int y2; double pi = 3.1416; double radius; double circumference; double area; double diameter; fin >> x1 >> y1 >> x2 >> y2; radius = radius(x1,x2,y1,y2); circumference = circumference(radius, pi); area = area(radius,pi); diameter = radius(x1,x2,y1,y2)*2; cout << setprecision(2) << fixed << radius << endl; cout << setprecision(2) << fixed << diameter << endl; cout << setprecision(2) << fixed << circumference << endl; cout << setprecision(2) << fixed << area; fin.close(); homecoming 0; }

c++

No comments:

Post a Comment