Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overload operator ?

Reply
Thread Tools

overload operator ?

 
 
sd2004
Guest
Posts: n/a
 
      12-12-2005
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;

}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-12-2005
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
 
Reply With Quote
 
 
 
 
michaelkatsilis@yahoo.com
Guest
Posts: n/a
 
      12-12-2005
Check your library definition of find.

Regards,

Michael

 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      12-13-2005
> 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

 
Reply With Quote
 
sd2004
Guest
Posts: n/a
 
      12-13-2005

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


 
Reply With Quote
 
sd2004
Guest
Posts: n/a
 
      12-13-2005
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;

}

 
Reply With Quote
 
Csaba
Guest
Posts: n/a
 
      12-13-2005
Victor Bazarov <> wrote in
news:_Dmnf.57118$ 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to handle the lack of operator overload ? Mr Smith Java 7 02-10-2005 08:37 AM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM
Q: Overload operator new/delete Jakob Bieling C++ 0 08-08-2003 12:20 PM
overload operator - Matthew Monopole C++ 1 08-08-2003 09:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57