overload operator ?

S

sd2004

Hi ,

could someone please help ?
What I would like to do is to skip line with # .

/////// Error message ////////////////////////////////
test7.cpp: In function `std::istream& operator>>(std::istream&,
astruct&)':
test7.cpp:18: error: `find' undeclared (first use this function)
test7.cpp:18: error: (Each undeclared identifier is reported only once
for each
function it appears in.)
test7.cpp:18: error: continue statement not within a loop

///////////// input file "test7.txt" //////////////////

################################
N Kait 555 2124.80 5.5
Y Tina 777 9184.30 8.2
################################


///////////////////// source code "test7.cpp" ////////////////////

#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;
class astruct
{
public:
string skip;
string name;
int id;
float loan;
float interest_rate;
};

istream& operator>>(istream& is, astruct& s){
// Just want to skip line with #
if(is.find("#")!=string::npos) continue; // this cause the progam
NOT compile.

is >>s.skip>>s.name >> s.id >>s.loan >> s.interest_rate;
return is;
}

int main()
{
vector<astruct> v;
astruct astr;

ifstream in ("test7.txt");
if(!in) {cout << "cannot open file"<<endl;}
string line;
while (in>>astr){
v.push_back(astr);
}

vector<astruct> ::iterator test;
for (test=v.begin();test!=v.end();++test){
if (test->skip.find("Y")!=string::npos)continue;
float total_due = test->loan * test->interest_rate;
cout <<"Name : "<<test->name<<" "<<" ID: "<<test->id<<" Loan: "
<<test->loan<<" Principal: "<<total_due<< endl;
}
return 0;

}
 
V

Victor Bazarov

sd2004 said:
could someone please help ?
What I would like to do is to skip line with # .

/////// Error message ////////////////////////////////
test7.cpp: In function `std::istream& operator>>(std::istream&,
astruct&)':
test7.cpp:18: error: `find' undeclared (first use this function)
test7.cpp:18: error: (Each undeclared identifier is reported only once
for each
function it appears in.)
test7.cpp:18: error: continue statement not within a loop

///////////// input file "test7.txt" //////////////////

################################
N Kait 555 2124.80 5.5
Y Tina 777 9184.30 8.2
################################


///////////////////// source code "test7.cpp" ////////////////////

#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;
class astruct
{
public:
string skip;
string name;
int id;
float loan;
float interest_rate;
};

istream& operator>>(istream& is, astruct& s){
// Just want to skip line with #
if(is.find("#")!=string::npos) continue; // this cause the progam
NOT compile.

You're trying to use a non-existent function "find" on the 'is' object.
The class 'istream' does NOT have a 'find' member. Perhaps you should
use some other way of determining that the line you're reading starts with
a pound character...
is >>s.skip>>s.name >> s.id >>s.loan >> s.interest_rate;
return is;
}

[...]

V
 
E

Earl Purple

istream& operator>>(istream& is, astruct& s){
// Just want to skip line with #
if(is.find("#")!=string::npos) continue; // this cause the progam
NOT compile.

is >>s.skip>>s.name >> s.id >>s.loan >> s.interest_rate;
return is;
}

If you really have to use find then you might be able to work out a way
to use std::find with istream_iterator.

What I think you want to do though is:

std::string line;
std::getline( is, line );
if ( line.find("#") != std::string::npos ) continue;
std::istringstream iss( line );
// then continue as before but with iss instead of is
 
S

sd2004

Earl said:
If you really have to use find then you might be able to work out a way
to use std::find with istream_iterator.

What I think you want to do though is:

std::string line;
std::getline( is, line );
if ( line.find("#") != std::string::npos ) continue;
std::istringstream iss( line );
// then continue as before but with iss instead of is
 
S

sd2004

This seems to work,
does anyone has better solution, please help me out.
/////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;
class astruct
{
public:
string skip;
string name;
int id;
float loan;
float interest_rate;
};

istream& operator>>(istream& iss, astruct& s){
iss >>s.skip>>s.name >> s.id >>s.loan >> s.interest_rate;
return iss;
}

int main()
{
vector<astruct> v;
astruct astr;

ifstream in ("test7.txt");
if(!in) {cout << "cannot open file"<<endl;}
string line;
while (getline (in,line)){
if (line.find("#") !=std::string::npos)continue;
istringstream iss(line);
while (iss>>astr){
v.push_back(astr);
}
}

vector<astruct> ::iterator test;
for (test=v.begin();test!=v.end();++test){
if (test->skip.find("Y")!=string::npos)continue;
float total_due = test->loan * test->interest_rate;
cout <<"Name : "<<test->name<<" "<<" ID: "<<test->id<<" Loan: "
<<test->loan<<" Principal: "<<total_due<< endl;
}
return 0;

}
 
C

Csaba

sd2004 wrote: [snip]
istream& operator>>(istream& is, astruct& s){
// Just want to skip line with #
if(is.find("#")!=string::npos) continue; // this cause the
progam
NOT compile.

You're trying to use a non-existent function "find" on the 'is'
object. The class 'istream' does NOT have a 'find' member. Perhaps
you should use some other way of determining that the line you're
reading starts with a pound character...

....perhaps by reading each line from the istream into a std::string
std::string DOES have a find() member.

istream& operator>>(istream& is, astruct& s)
{
std::string line;
while( std::getline( is, line ) )
{
if( line.find('#') == std::string::npos )
{
// no '#', create an in-memory stream and read it
std::istringstream mem_strm( line );
mem_strm >>s.skip>>s.name >> s.id >>s.loan >> s.interest_rate;
}
}
}
 

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

vect of struct ?? 2
Chatbot 0
vector of struct ? 2
passing argument in STL loop ? 3
Filter sober in c++ don't pass test 0
STL ? 4
Overload of operator[] 9
Class not recognising string type with gcc (?) 9

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top