Connecting Tech Pros Worldwide Forums | Help | Site Map

Help on istream& operator ?

tvn007
Guest
 
Posts: n/a
#1: Dec 1 '05
Please help why this code not compile
I would like it to be something like this:
istringstream stringstreams(line);
stringstreams>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm;
stringstreams>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname;

///////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
const int MAX=50;
struct Record_info {
string bypassed;
string name;
int midterm;
double quiz;
double final;
string testname;
}student[MAX],*ptrstudent;

int main (void){
ptrstudent = student;
ifstream in ("test2.txt");
string line;
string line2;
char buffer[40];
while (getline(in,line)){
if (line.find("#")!=string::npos)continue ;
istream& operator >>(istream & is,prtstudent & r){
is>>r.bypassed>>r.name>>r.midterm,r.quiz,r.final,r .testname;}
ptrstudent++;
}
return 0;
}


Michiel.Salters@tomtom.com
Guest
 
Posts: n/a
#2: Dec 1 '05

re: Help on istream& operator ?


tvn007 wrote:[color=blue]
> Please help why this code not compile[/color]

Well, the compiler told you, in the error message. Please post it,
there are good reasons why the FAQ says you must do that.
[color=blue]
> ///////////////////////////////////////////////////////////////////////////////////////////////////////
> #include <iostream>
> #include <string>
> #include <fstream>
> #include <sstream>
> using namespace std;
> const int MAX=50;
> struct Record_info {
> string bypassed;
> string name;
> int midterm;
> double quiz;
> double final;
> string testname;
> }student[MAX],*ptrstudent;
>
> int main (void){
> ptrstudent = student;
> ifstream in ("test2.txt");
> string line;
> string line2;
> char buffer[40];
> while (getline(in,line)){
> if (line.find("#")!=string::npos)continue ;
> istream& operator >>(istream & is,prtstudent & r){
> is>>r.bypassed>>r.name>>r.midterm,r.quiz,r.final,r .testname;}
> ptrstudent++;
> }
> return 0;
> }[/color]

Functions should be defined and used separately. It looks like you
don't
understand the difference, and you tried to put something that's
neither
a definition nor a call of operator>> inside main.

Try a simpler program first, defining and calling a function void
print_hello_world( )

HTH,
Michiel Salters

Peter_Julian
Guest
 
Posts: n/a
#3: Dec 2 '05

re: Help on istream& operator ?



"tvn007" <tvn007@hotmail.com> wrote in message
news:1133421143.893522.93260@z14g2000cwz.googlegro ups.com...
| Please help why this code not compile
| I would like it to be something like this:
| istringstream stringstreams(line);
|
stringstreams>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midte
rm;
|
stringstreams>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname
;
|
|
////////////////////////////////////////////////////////////////////////
///////////////////////////////
| #include <iostream>
| #include <string>
| #include <fstream>
| #include <sstream>
| using namespace std;
| const int MAX=50;
| struct Record_info {
| string bypassed;
| string name;
| int midterm;
| double quiz;
| double final;
| string testname;
| }student[MAX],*ptrstudent;
|
| int main (void){
| ptrstudent = student;
| ifstream in ("test2.txt");
| string line;
| string line2;
| char buffer[40];
| while (getline(in,line)){
| if (line.find("#")!=string::npos)continue ;
| istream& operator >>(istream & is,prtstudent & r){
| is>>r.bypassed>>r.name>>r.midterm,r.quiz,r.final,r .testname;}
| ptrstudent++;
| }
| return 0;
| }
|

Start by getting a simplified skeleton to work first. Appropriate
formatting goes a long way to help you identify your individual
declarations.

// test2.txt
name_1
name_2
name_3

So your test.cpp file should look like this:

#include <iostream>
#include <ostream>
#include <string>
#include <fstream>

// Record type
struct Record_info
{
void set(std::string& s) { name = s; }
private:
std::string name;
/* Friend op>> */
friend std::ostream&
operator<<(std::ostream&, const Record_info&);
};

// global op<<
std::ostream& operator<<(std::ostream& os, const Record_info& r)
{
os << "name: " << r.name << "\n";
return os;
}

int main()
{
Record_info students[10];

// open file
std::ifstream in("test2.txt");
if( !in )
{
std::cout << "error while opening file.\n";
return 0;
}

// parse file
int count = 0;
std::string s;
while ( std::getline(in, s) )
{
students[count++].set(s);
}
if( !in.eof() ) // paranoia check, was the error *not* an eof
marker?
{
std::cout << "error while parsing file.\n";
return 0;
}

// display contents
std::cout << "number of students: " << count << "\n";
for (int i = 0; i < count; ++i)
{
std::cout << students[i];
}

return 0;
} // main()

/*
number of students: 3
name: name_1
name: name_2
name: name_3

*/



Closed Thread