-
why error??
Here is some code which I can not understand why I get an error.
This is the error I get from Visual C++
(30) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
What am I doing wrong??
It is just reading some text from a file, putting it into a struct, then in to a vector. Now I would like to read what is in the vector.
Would appreciate any pointers.
Thanks
#include <iostream.h>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <cctype>
#include <cstdlib>
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");
for(int i=0; i<catalog.size(); i++)
cout << catalog[i].name << endl;
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
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=="")
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
}
if(counter>3)
(c.preq).push_back(part);
counter++;
}
}
return catalog;
}
-
you smarter guys can correct me if im wrong, but if you use a struct or class to define a data type, you cant use the << or >> operators unless you overload them.
-
Try getting rid of the .h at the end of iostream. (and making it <cstdlib> probably wouldn't hurt either).
-
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;
}
-
this i got to compile in bloodshed, by the way