首页 > 解决方案 > 文件读取错误未读取 .txt 文件中的内容

问题描述

为气象学家编写一个程序,计算以华氏度为单位的输入温度、以英里/小时为单位的风速和以华氏度为单位的露点的风寒系数和云底高度。

4个功能

  1. 一种用于获取输入
  1. 计算风寒必须等于或低于华氏 50 度
  2. 计算云基础
  3. 一个用于输出

4个更多功能

  1. 一种用于打开文件失败条件的文件***这是问题所在使用***
  2. 一种计算风寒
  3. 一个计算云基础
  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";
        }

    }

标签: c++

解决方案


您要求提供文件名,但实际上并没有打开文件:

 inputFile.open (filename, std::ifstream::in);

这可能会有所帮助。


推荐阅读