我正在尝试使用fstream进行作业,但我认为ifstream不会打开我的文件或读错了
我的代码应该是从文件读取点并给出距离,直径等...
当我使用" 11 4 -3 3"运行代码时在文件中 所有我的整数x1,x2,y1和y2变为-858993460,我的输出为0.00 当我在屏幕上输入键盘时,代码在没有fstream的情况下完美运行。
我查了一下 如果(!鳍) cout>> "无法打开" 它输出"无法打开"所以我猜它没有打开..
这是我的代码:
#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);
return distance;
}
double radius (int x1, int x2, int y1, int y2)
{double radius;
radius = distance(x1,x2,y1,y2);
return radius;
}
double circumference (double radius, double pi)
{double circumference;
circumference = 2*pi*radius;
return circumference;}
double area (double radius, double pi)
{ double area;
area = pi*radius*radius;
return 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();
return 0;
}