By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

overload operator ?

P: n/a
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;

}

Dec 12 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
sd2004 wrote:
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
Dec 12 '05 #2

P: n/a
Check your library definition of find.

Regards,

Michael

Dec 12 '05 #3

P: n/a
> 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

Dec 13 '05 #4

P: n/a

Earl Purple wrote:
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


Dec 13 '05 #5

P: n/a
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;

}

Dec 13 '05 #6

P: n/a
Victor Bazarov <v.********@comAcast.net> wrote in
news:_D*******************@newsread1.mlpsca01.us.t o.verio.net:
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;
}
}
}

--
Life is complex, with real and imaginary parts.
Dec 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.