Help on istream& operator ?

T

tvn007

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;
}
 
M

Michiel.Salters

tvn007 said:
Please help why this code not compile

Well, the compiler told you, in the error message. Please post it,
there are good reasons why the FAQ says you must do that.
///////////////////////////////////////////////////////////////////////////////////////////////////////
#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;
}

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
 
P

Peter_Julian

| 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::eek:stream&
operator<<(std::eek:stream&, const Record_info&);
};

// global op<<
std::eek:stream& operator<<(std::eek:stream& 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;
}

return 0;
} // main()

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

*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

CLASS ? 2
struct ? 5
Help on struct ? 3
Array size ? 6
Problem overloading extraction operator 16
TF-IDF 1
Help, read input from file ? 3
read data from file into structure ? 2

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top