c++ - 文件读取错误未读取 .txt 文件中的内容
问题描述
为气象学家编写一个程序,计算以华氏度为单位的输入温度、以英里/小时为单位的风速和以华氏度为单位的露点的风寒系数和云底高度。
4个功能
- 一种用于获取输入
- 华氏温度
- 风速 mph
- 华氏露点
- 计算风寒必须等于或低于华氏 50 度
- 计算云基础
- 一个用于输出
4个更多功能
- 一种用于打开文件失败条件的文件***这是问题所在使用***
- 一种计算风寒
- 一个计算云基础
- 一个输出考虑其他你认为合适的功能
#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>
using namespace std;
//function prototypes
//void getUserInput(double& temp, double& wind_speed, double& dew_point);
//calculate the wind chill
void calculateWindChill(double& temp, double& wind_speed, double& wc);
//calculate cloud base
void calculateCloudBase(double& temp, double& dew_point, double& cb);
// show cases the data on the program that was calculated
void dataOutPut(double wc, double cb);
//opens file for data scan
void fileopn(double& temp, double& wind_speed, double& dew_point);
int main()
{
//intializing variables
double temp = 0, wind_speed = 0, dew_point = 0, wc = 0, cb = 0;
bool valid = false;
do
{
fileopn( temp, wind_speed, dew_point);
// validates if temp is greater than 50 F and wind chill must be greater than 3.0 mphs
if (temp > 50 || wind_speed < 3.0)
{
cout << "error temperature mus be less than 50 degrees and wind speed must be above 3.0 mphs" << endl;
}
} while (temp > 50 || wind_speed < 3.0);
calculateWindChill(temp, wind_speed, wc);
calculateCloudBase(temp, dew_point, cb);
dataOutPut(wc, cb);
return 0;
}
//get inputs from the user
void dataOutPut(double wc, double cb)
{
cout << " the wind chill is :" << wc << endl;
cout << "the cloud base is : " << cb << endl;
}
//calculates windChill
void calculateWindChill(double& temp, double& wind_speed, double& wc)
{
wc = 35.74 + (0.6215 * temp) - (35.37 * pow(wind_speed, 0.16)) + (0.4275 * temp * pow(wind_speed, 0.16));
}
//calculate Cloudbase
void calculateCloudBase(double& temp, double& dew_point, double& cb)
{
double temp_spread = temp - dew_point;
cb = temp_spread / (4.4 * 1000); // caclulates cloudbase (formula)
}
//opens file
void fileopn(double& temp, double& wind_speed, double& dew_point)
{
ifstream inputFile;
string filename;
double number;
//get filename from user
cout << "Enter file name " << endl;
cin >> filename;
cout << inputFile.is_open();
if (inputFile)
{
while (inputFile >> number)
{
cout << number << endl;
}
}
else
{
cout << "Error opening the file . \n";
}
}
解决方案
您要求提供文件名,但实际上并没有打开文件:
inputFile.open (filename, std::ifstream::in);
这可能会有所帮助。
推荐阅读
- c# - 如何找到在解决方案/源代码中使用的类/实体完整命名空间及其使用行号(对象创建)
- r - 错误:不能对不存在的列进行子集化。ggstatsplot 包
- c# - C# 中 JSONSchema 的运行时对象
- python - 您如何使用 Selenium Chrome 浏览器保持登录状态?
- postgresql - 如何在 Postgres 中使用多个 distinct on 语句,同时保持正确的顺序
- esper - 如何清理基础语句为“INSERT INTO”的 EPStatement#iterator()
- java - 从java中的数据库自动创建单选按钮
- sql - 自动从表中删除数据?
- android - 未生成 Sqldelight 数据库架构
- typescript - 我可以告诉 TypeScript 一个函数只有在输入为 null 时才会输出 null 吗?