overload operator ?

Discussion in 'C++' started by sd2004, Dec 12, 2005.

  1. sd2004

    sd2004 Guest

    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;

    }
     
    sd2004, Dec 12, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Dec 12, 2005
    #2
    1. Advertising

  3. sd2004

    Guest

    Check your library definition of find.

    Regards,

    Michael
     
    , Dec 12, 2005
    #3
  4. sd2004

    Earl Purple Guest

    > 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
     
    Earl Purple, Dec 13, 2005
    #4
  5. sd2004

    sd2004 Guest

    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
     
    sd2004, Dec 13, 2005
    #5
  6. sd2004

    sd2004 Guest

    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;

    }
     
    sd2004, Dec 13, 2005
    #6
  7. sd2004

    Csaba Guest

    Victor Bazarov <> wrote in
    news:_Dmnf.57118$01.us.to.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.
     
    Csaba, Dec 13, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr Smith
    Replies:
    7
    Views:
    486
    Mr Smith
    Feb 10, 2005
  2. Matthew Monopole

    overload operator -

    Matthew Monopole, Aug 8, 2003, in forum: C++
    Replies:
    1
    Views:
    493
    Jakob Bieling
    Aug 8, 2003
  3. Jakob Bieling

    Q: Overload operator new/delete

    Jakob Bieling, Aug 8, 2003, in forum: C++
    Replies:
    0
    Views:
    410
    Jakob Bieling
    Aug 8, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    392
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    277
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page