okay made a few changed, but still stuck, looks like in inner loop
i am trying to convert a string to a stream, then use getline to extract part of by string to the comma, but it doesn't seem to be working
any ideas
catalog.txt has lines like:
"Intro to C, CS101, 3, CS100"
Code:
#include <iostream.h>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include <string>
#include "sstream.h"
using namespace std;
struct course
{
string name;
string num;
int credits;
vector <string> preq;
};
vector <course> read_catalog(vector<course>&catalog);
int main()
{
vector <course> catalog;
read_catalog(catalog);
system("PAUSE");
return 0;
}
vector <course> read_catalog(vector <course> &catalog)
{
string line, part;
int counter;
course c;
ifstream fin("catalog.txt");
if(fin.fail())
{
cout<<"Failure to open file";
return catalog;
}
while(!fin.eof())
{
getline(fin,line); //taking in a whole line from file
// is this right???????????????????????????????????
stringstream sin(line); //converting string line to a stream
counter=1;
while(true)
{
getline(sin, part, ','); //taking a line until a comma is found
if(part.size()==0)
break;
if(counter==1)
c.name = part;
if(counter==2)
c.num = part;
if(counter==3)
{
const char *st = part.c_str(); //string part is convert to cstring
c.credits = atoi(st); //st is converted to an integer and put in struct
}
counter++;
}
catalog.push_back(c);
/* if(counter>3)
(c.preq).push_back(part); // ????? is this right?????? */
}
cout << line << endl;
return catalog;
}