簡體   English   中英

將單詞和數字文件輸入到數組c ++中

[英]input a file of words and numbers into an array c++

好吧,我對編程不是很有經驗,但我有一個創建c ++程序的任務,該程序使用數值方法根據混合物中每種物質的焓和百分比計算三種物質混合物的溫度。 它基本上是多項式h = a1 * T + a2 * T ^ 2 + ...直到a6。 對於H 2 O,H 2和O 2中的每一個,這些系數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;
}

將您的結構聲明為:

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];
 }
}

我的建議:

  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; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM