【问题标题】:input a file of words and numbers into an array c++将单词和数字的文件输入到数组c ++中
【发布时间】:2014-11-27 01:28:51
【问题描述】:

好的,我对编程不是很有经验,但我有一个任务是创建一个 c++ 程序,该程序使用数值方法根据混合物中每种物质的焓和百分比来计算三种物质的混合物的温度.它基本上是 h = a1*T + a2*T^2 + ... 直到 a6 的多项式。对于 H2O、H2 和 O2 中的每一个,这些系数 a1 到 a6 在表格中给出。我的程序需要能够从 .dat 文件中读取物质名称和系数值,以便我可以将系数用于我的方程。这就是我需要帮助的地方。如何让程序将物质名称和系数值输入到数组中,以便在我的方程式中使用它们?对这部小说感到抱歉,但我试图提供尽可能多的背景信息。 下面正是我的 .dat 文件中的内容,以及我试图放入数组中的内容。物质名称在前,后跟a1、a2等

H2O 406598.40 440.77751 -.12006604 .000015305539 ​​-.00000000072544769 -4475789700 H2 50815.714 9.9343506 -.000027849704 -.00000035332966 .000000000041898079 -14329128 O2 961091.64 199.15972 -.052736240 .00000897950410 -.00000000063609681 -318699310

到目前为止,这是我的源代码,但它不工作,我很迷茫。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
double myArray[21];

ifstream file("thermo2.dat");

if (file.is_open())
{
    for (int i = 0; i < 21; ++i)
    {
            file >> myArray[i];
    }
}
else 
{
    cout << "the file did not open";
}

for (int i = 0; i < 21; ++i)
    {
        cout << "      " << myArray[i];
    }

return 0;
}

谢谢!

编辑:开始尝试使用结构数组....我不断收到错误消息:没有匹配函数调用“getline(std::ifstream&, double&, char)”。代码如下:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct Data
{
    string species;
    double a1, a2, a3, a4, a5, a6;
};

int main()
{
ifstream fin;

fin.open("thermo2.dat");

if (fin.fail())
{
    cout << "Failed to open file" << endl;
}


Data * database = new Data[3];
string line;

for(int i = 0; i < 3; i++)
{


    getline(fin, database[i].species, '\t');
    getline(fin, database[i].a1, '\t');
    getline(fin, database[i].a2, '\t');
    getline(fin, database[i].a3, '\t');
    getline(fin, database[i].a4, '\t');
    getline(fin, database[i].a5, '\t');
    getline(fin, database[i].a6, '\t');
}


system("pause");


return 0;
}

【问题讨论】:

  • 首先决定如何存储数据。你想要一个数组还是三个?您需要文件中的物质名称吗?如果是这样,您希望如何存储它们?
  • 我正在尝试使用一组结构,但我很难让它发挥作用,如果您可以查看我发布的编辑并提供任何见解,那就太好了!

标签: c++ arrays file input


【解决方案1】:

将你的结构声明为:

struct Data
{
    string species;
    double a[6];
}

阅读如下:

for(int i = 0; i < 3; i++) {
 fin >> database[i].species;
 for (int j = 0; j < 6; j++) {
   fin >> database[i].a[j];
 }
}

【讨论】:

  • 谢谢!这很有帮助!
【解决方案2】:

我的建议:

  1. 创建一个struct 来保存每种材料的数据。

    struct Material
    {
       std::string name;
       double coeffcients[6];
    };
    
  2. 创建一个函数以从流中读取一个Material

    std::istream& operator>>(std::istream& in, Material& mat)
    {
        // Read the name.
        in >> mat.name;
    
        // If there was an error, return.
        // Let the calling function deal with errors.
        if (!in)
        {
           return in;
        }
    
        // Read the coefficients.
        for (int i = 0; i < 6; ++i )
        {
            in >> mat.coefficients[i];
            if (!in)
            {
               return in;
            }
        }
        return in;
    };
    
  3. main函数中,编写驱动代码。

    int main()
    {
        // Create a vector of materials.
        std::vector<Material> materials;
    
        // Open the input file.
        ifstream file("thermo2.dat");
    
        // Read Materials the file in a loop and
        // add them to the vector.
        Material mat;
        while (file >> mat)
        {
           materials.push_back(mat);
        }
    
        // Now use the vector of Materials anyway you like.
    
    
        // Done with main.
        return 0;
    }
    

【讨论】:

  • 在我意识到你已经回复之前,我试图让它与一个结构一起工作。有什么办法可以让它这样工作吗?我不断收到 a1 - a6 的所有 getlines 错误。我会将我当前的代码发布为编辑
  • getline 不能用于读取double。为此使用operator&gt;&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
  • 1970-01-01
  • 2015-01-25
  • 2014-06-19
  • 2014-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多