getline and substr

Discussion in 'C++' started by Kees Hoogendijk, Dec 20, 2003.

  1. Hallo,
    I've got 2 error msg, line 11 and 24. I don't understand both of them.
    Can someone type me in the normal language and help me here out?
    TI@
    Wen


    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;

    int main()
    {
    char postco[7];
    cout<<"postcode : ";
    getline(postco,7); //11 no matching function for
    call

    if (postco[0]!=0 && isdigit(postco[0] ))
    {
    cout <<"s ";
    cin.get();
    }
    else
    {
    cout<< "w";
    cin.get();
    }

    if (postco.substr(4,2)!=" ") //24 request for member `substr'
    in
    {
    cout<<"G";
    cin.get();
    }
    else
    {
    cout<<"ng";
    cin.get();
    }
    }
     
    Kees Hoogendijk, Dec 20, 2003
    #1
    1. Advertising

  2. Kees Hoogendijk

    Moonlit Guest

    Hi,


    "Kees Hoogendijk" <> wrote in message
    news:bs1n4j$fps$...
    > Hallo,
    > I've got 2 error msg, line 11 and 24. I don't understand both of them.
    > Can someone type me in the normal language and help me here out?
    > TI@
    > Wen
    >
    >
    > #include <iostream>
    > #include <string>
    > #include <cctype>
    > using namespace std;
    >
    > int main()
    > {


    string postco;
    i.s.o char postco

    > char postco[7];
    > cout<<"postcode : ";
    > getline(postco,7); //11 no matching function for
    > call
    >
    > if (postco[0]!=0 && isdigit(postco[0] ))
    > {
    > cout <<"s ";
    > cin.get();
    > }
    > else
    > {
    > cout<< "w";
    > cin.get();
    > }
    >
    > if (postco.substr(4,2)!=" ") //24 request for member

    `substr'
    > in
    > {
    > cout<<"G";
    > cin.get();
    > }
    > else
    > {
    > cout<<"ng";
    > cin.get();
    > }
    > }
    >
    >


    Regards, Ron AF Greve.
     
    Moonlit, Dec 20, 2003
    #2
    1. Advertising

  3. Kees Hoogendijk

    Jeff Schwab Guest

    Kees Hoogendijk wrote:
    > Hallo,
    > I've got 2 error msg, line 11 and 24. I don't understand both of them.
    > Can someone type me in the normal language and help me here out?
    > TI@
    > Wen
    >
    >
    > #include <iostream>
    > #include <string>
    > #include <cctype>
    > using namespace std;
    >
    > int main()
    > {
    > char postco[7];
    > cout<<"postcode : ";
    > getline(postco,7); //11 no matching function for call


    cin.getline( postco, 7 ); // ITYM
    >
    > if (postco[0]!=0 && isdigit(postco[0] ))
    > {
    > cout <<"s ";
    > cin.get();
    > }
    > else
    > {
    > cout<< "w";
    > cin.get();
    > }


    cout << ( isdigit( postco[ 0 ] ) ? "s " : "w" );

    cin.get( );

    > if (postco.substr(4,2)!=" ") //24 request for member `substr' in


    If you want substr, use a std::string instead of an array of characters.
    Anyway, the two strings used here will never be the same. The one on
    the left has length two, but the one on the right has length 1.

    > {
    > cout<<"G";
    > cin.get();
    > }
    > else
    > {
    > cout<<"ng";
    > cin.get();
    > }
    > }


    What's with all those cin.get()'s? Here's how to do what I think you meant.
    #include <iostream>
    #include <string>
    #include <cctype>

    /* Format a postal code.
    */
    std::string format( std::string const& s )
    {
    if( s.empty( ) )
    {
    throw "Can't format postal code for empty string.";
    }

    return
    std::string( "postcode: " ) +
    ( std::isdigit( s[ 0 ] ) ? "s " : "w" ) +
    ( s.substr( 4, 1 ) != " " ? "G" : "ng" );
    }

    int main( )
    {
    std::string postal_code;

    std::getline( std::cin, postal_code );

    std::cout << format( postal_code ) << std::endl;
    }
     
    Jeff Schwab, Dec 20, 2003
    #3
  4. Kees Hoogendijk

    Ron Natalie Guest

    "Kees Hoogendijk" <> wrote in message
    news:bs1n4j$fps$...
    > Hallo,
    > I've got 2 error msg, line 11 and 24. I don't understand both of them.
    > Can someone type me in the normal language and help me here out?
    >

    postco is an array of 7 char, it is NOT a string,
    Change the definition to
    string postco;
     
    Ron Natalie, Dec 20, 2003
    #4
  5. Thank you very much, three of you.
    I 'd take a try, it works.
    Regards

    Wen

    "Kees Hoogendijk" <> schreef in
    bericht news:bs1n4j$fps$...
    > Hallo,
    > I've got 2 error msg, line 11 and 24. I don't understand both of them.
    > Can someone type me in the normal language and help me here out?
    > TI@
    > Wen
    >
    >
    > #include <iostream>
    > #include <string>
    > #include <cctype>
    > using namespace std;
    >
    > int main()
    > {
    > char postco[7];
    > cout<<"postcode : ";
    > getline(postco,7); //11 no matching function for
    > call
    >
    > if (postco[0]!=0 && isdigit(postco[0] ))
    > {
    > cout <<"s ";
    > cin.get();
    > }
    > else
    > {
    > cout<< "w";
    > cin.get();
    > }
    >
    > if (postco.substr(4,2)!=" ") //24 request for member

    `substr'
    > in
    > {
    > cout<<"G";
    > cin.get();
    > }
    > else
    > {
    > cout<<"ng";
    > cin.get();
    > }
    > }
    >
    >
     
    Kees Hoogendijk, Dec 21, 2003
    #5
    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. entropy123

    [C++] Odd Problem with "substr"

    entropy123, Jun 1, 2005, in forum: C++
    Replies:
    7
    Views:
    546
    Stephen Howe
    Jun 2, 2005
  2. David Resnick
    Replies:
    1
    Views:
    312
    Victor Bazarov
    Apr 10, 2006
  3. tfelb

    Missing removeStr and substr function

    tfelb, Nov 26, 2007, in forum: C Programming
    Replies:
    13
    Views:
    598
    Harald van Dijk
    Dec 6, 2007
  4. Veli-Pekka Tätilä
    Replies:
    6
    Views:
    104
    Anno Siegel
    Aug 23, 2005
  5. Replies:
    12
    Views:
    191
    Tad McClellan
    Mar 8, 2007
Loading...

Share This Page