so all i want to so is read a txt file and store it into an array but im im having a difficulty and here is my code
and my txt file has a delimeter (',') so i put a getline function as well but it is not wrking for me please help..
the txt file is:
Substance, Molecular weight
Benzene, 78.115
Ethyl Alcohol, 46.07
Refrigerant R134a (tetraflouroethane), 102.3
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int mass = 10;
int main()
{
string substance[3] = {",",",",","};
double molWeight[3] = {0,0,0};
double numMoles[3] = {0,0,0};
int index;
ifstream infile;
infile.open("/Users/ea0409952/Documents/COSC1337home/abebe-lab13/chem.txt");
int count = 0;
while(infile.eof())
{
getline(infile,substance[index],',');
infile >> molWeight[index];
count++;
}
/*
for (index = 0; index < 3; index++)
{
{
getline(infile,substance[index],',');
infile >> molWeight[index];
}
}
*/
if ( !infile)
{
cout <<"Unable to open file, try again please!" << endl;
return 1;
}
cout <<left << setw(40) << "Substance"
<< setw(20) << "Molecular Weight"
<< setw(10) << "# of moles";
infile.close();
return 0;
}
//void getMoles(double molweight[], double numMoles[], const int mass[])