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[])

From an examination of your code, a part of your problem appears to be using index to fill the array, but your loop is incrementing count. Therefore the array is always using the same index.

Also using eof() to terminate the loop is bad practise. Instead use the return value of getline. When getline can't read any more it will return false.:

while(getline(infile,substance[index],','))

Checking if infile is valid after you've read to eof wil always rerturn 1. Check before the while loop.

You haven't got a loop to print out the data in the arrays.

first of all thank you for your reply so i changed the line you suggested instead of using eof, that makes sense, also
i took out the count part, now the getline will read the line all the way, include the delimiter but its not in my case, im not sure whats the problem..i havent changed anything but this code below only

while(getline(infile,substance[index],','))
{
    getline(infile,substance[index],',');
    infile >> molWeight[index];

}

Maybe you want this?

while(getline(infile,substance[index],','))
{
    // getline(infile,substance[index],','); // done above //
    infile >> molWeight[index];
}

It is hard to tell what you want without some sample data file.

Perhaps the following example of reading a csv data file might help?

// fileReadWriteStudent_csv.cpp //

#include <iostream>
#include <iomanip> // re. setw
#include <fstream>
#include <sstream> // re. stringstream objects //
#include <string>

using namespace std;


const char* FILE_IN  = "Students_in_csv.txt";
/*
1001,Joe Toe Smith,33
1002,Ann Fran Books,44
1003,Harry Larry Sanders,11
*/
const char* FILE_OUT = "Students_adjusted.txt";

const int ADJUST = 50;


struct Student
{
    int id;
    string name;
    int score;

    istream& takeIn( istream& is )
    {
        string line;
        getline(is, line);
        istringstream iss( line ); // form iss obj. from line //
        char comma;
        iss >> id >> comma;
        getline( iss, name, ',' );
        iss >> score;

        return is;
    }

    void print( ostream& os ) const
    {
        int score_adj = score + ADJUST;
        if( score_adj > 100 ) score_adj = 100;
        os << setw(4) << setfill('0') << id  << ' '
           << setfill('.') << left << setw(30) << name << ' '
           << setfill(' ') << right << setw(3) << score_adj << endl;
    }

} ;



int main()
{
    ifstream fin( FILE_IN );
    ofstream fout( FILE_OUT );
    if( fin && fout )
    {
        Student stud;
        while( stud.takeIn( fin ) )
        {
            // do any processing here that you wish on this 'word' ... for example
            // just print it to screen and to file, each 'word' on a new line

            stud.print( fout );
            stud.print( cout );
        }

        fout.close();
        fin.close();
    }
    else
    {
        if( !fin ) cout << "There was a problem opening file " << FILE_IN  << endl;
        if( !fout ) cout << "There was a problem opening file " << FILE_OUT  << endl;
    }
}

Thank you for your response David, this is what i have done now to open this text file: its not opening some reason idk why, text file is chem.txt

Substance, Molecular weight
Benzene, 78.115
Ethyl Alcohol, 46.07
Refrigerant R134a (tetraflouroethane), 102.3

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdlib.h>


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;

char just[10] ;
char just1[10] ;
infile.open("/Users/ea0409952/Documents/COSC1337home/abebe-lab13/chem.txt");

if ( !infile)
{
    cout <<"Unable to open file, try again please!" << endl;
    return 1;

}
 cout  << setw(30) <<left<< "Substance"   << setw(20) <<left<< "Molecular Weight"  << setw(20) <<left<< "# of moles";

cout<<"\n";



infile.getline(just,256,',');

infile.getline(just,256,'\n');


infile.getline(just,256,',');
cout<<just;

for (index = 0; index < 3; index++)
{
    infile.getline(just,50,',');
    substance[index]=just;

    infile.getline(just,50,'\n');
    molWeight[index]=atoi(just);

    cout << setw(30) <<left<<  substance[index]  <<setw(20) << molWeight[index]   << setw(20) << "# of moles";
    cout<<"\n";
}




 infile.close();
    return 0;
}

You probably need the root folder in front of Users. Also if you are using a windows machine all / should be \\.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.