Enter a file of words and numbers into a C ++ array

Ok, I'm not very good at programming, but I have a task to create a C ++ program that uses numerical methods to calculate the temperature of a mixture of three substances based on the enthalpy and the percentage of each substance in the mixture, its basically a polynomial of h = a1 * T + a2 * T ^ 2 + ... to a6. These coefficients from a1 to a6 are shown in the table for each of H20, H2 and O2. My program needs to be able to read the substance names and coefficient values ​​from the .dat file so that I can use the coefficients for my equations. This is what I need help with. How can I force the program to enter the substance names and coefficient values ​​into an array so that I can use them in my equations? Sorry for the novel, but I tried to give as much context as possible. below is exactly what is in my .dat file and what I am trying to add to the array.The name of the substance first, and then a1, a2, etc.

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  

this is my original code, but it doesn't work and I'm pretty lost.

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

      

thank!

EDIT: Started working with an array of structures. I keep getting the error: No suitable function to call 'getline (std :: ifstream &, double &, char)'. heres the code:

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

      

+3


source to share


2 answers


Declare your structure as:

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

      



And read like below:

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

      

+2


source


My suggestion:



  • Create a struct

    data store for each material.

    struct Material
    {
       std::string name;
       double coeffcients[6];
    };
    
          

  • Create a function to read one Material

    of the stream.

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

  • In the function, main

    write the driving code.

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

0


source







All Articles