struct ?

T

tvn007

I am working on the data struc. below:
is there way I can abbrev. the expression below to avoid long
expression.
Thanks......
///////////////////////////////////////////////////////////////////////////////////
#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 ;
istringstream anyname(line);

/////////// HOW CAN I ABBREV. EXPRESSION BELOW ?
////////////////////////////////////////

anyname>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm;

anyname>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ptrstudent++;
}
}
 
R

Ron Natalie

tvn007 said:
struct Record_info {
string bypassed;
string name;
int midterm;
double quiz;
double final;
string testname;

istream& operator>> (istream& is) (
is >> bypassed;
is >> name
is >> midterm;
is >> quiz
is >> final;
is >> testname;
return is;
}
}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 ;
istringstream anyname(line);

/////////// HOW CAN I ABBREV. EXPRESSION BELOW ?
////////////////////////////////////////


anyname>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm;

anyname>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname;

anyname >> *ptrstudent;
 
R

Richard Herring

Ron Natalie said:
istream& operator>> (istream& is) (

If its _left_ operand is to be an istream, this can't be a member
function of Record_info. It has to be a non-member function with two
arguments.

friend istream & operator>>(istream & is, Record_info & rec) {
is >> bypassed;
is >> rec.bypassed; // etc.
is >> name
is >> midterm;
is >> quiz
is >> final;
is >> testname;
return is;
}
}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 ;
istringstream anyname(line);
/////////// HOW CAN I ABBREV. EXPRESSION BELOW ?
////////////////////////////////////////


anyname>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm;

anyname>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname;

anyname >> *ptrstudent;
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
ptrstudent++;
}
}
 
T

tvn007

I am not sure if it will work since I am using struct array and
pointer.

struct Record_info {
string bypassed;
string name;
int midterm;
double quiz;
double final;
string testname;
}student[MAX],*ptrstudent;
ptrstudent = student;

anyname>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm;


anyname>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname;
 
R

Richard Herring

I am not sure if it will work

Not sure if _what_ will work? Please quote some context.
since I am using struct array and
pointer.

I don't think that's relevant. Fundamentally you want to implement
operator>>(istream&, Record_info &). If the Record_info happens to be an
array element or the target of a pointer it makes no difference.
struct Record_info {
string bypassed;
string name;
int midterm;
double quiz;
double final;
string testname;
}student[MAX],*ptrstudent;
ptrstudent = student;

anyname>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm;


anyname>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname;

anyname >> *ptrstudent;
 

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

Help on istream& operator ? 2
Help on struct ? 3
CLASS ? 2
Why struct not globally changed in function? 1
read data from file into structure ? 2
Help, read input from file ? 3
Array size ? 6
TF-IDF 2

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top