Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Okay, I am not very experienced with programming, but I have an assignment to create a c++ program that uses numerical methods to calculate the temperature of a mixture of three substances, based on the enthalpy and percent of each substance in the mixture. its basically a polynomial of h = a1*T + a2*T^2 + ... up to a6. These coefficents a1 through a6 are given in a table, for each of H20, H2, and O2. My program needs to be able to read the substance names and the values of the coefficients from a .dat file so that I can use the coefficients for my equations. That's what I need help with. How can I get the program to input the substance names and coefficient values into an array so 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 put in an array. The substance name is first, followed by 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 source code so far, but its not working, 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;
}

thanks!

EDIT: started trying to work with an array of structs....I keep getting an error: no matching function for call to '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;
}
like image 878
t.m Avatar asked Jan 29 '26 03:01

t.m


1 Answers

Declare your structure as:

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

And read as below:

for(int i = 0; i < 3; i++) {
 fin >> database[i].species;
 for (int j = 0; j < 6; j++) {
   fin >> database[i].a[j];
 }
}
like image 184
Dr. Debasish Jana Avatar answered Jan 31 '26 19:01

Dr. Debasish Jana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!