Reputation: 33
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;
}
Upvotes: 3
Views: 1577
Reputation: 7118
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];
}
}
Upvotes: 2
Reputation: 206637
My suggestion:
Create a struct
to hold the data for each material.
struct Material
{
std::string name;
double coeffcients[6];
};
Create a function to read one Material
from a 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 main
function, 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;
}
Upvotes: 0