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

getline and substr

P: n/a
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();
}
}
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,
"Kees Hoogendijk" <ni********@haalditweg-hcopleidingen.nl> wrote in message
news:bs**********@news.cistron.nl...
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.
Jul 22 '05 #2

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

Jul 22 '05 #3

P: n/a

"Kees Hoogendijk" <ni********@haalditweg-hcopleidingen.nl> wrote in message
news:bs**********@news.cistron.nl...
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;

Jul 22 '05 #4

P: n/a
Thank you very much, three of you.
I 'd take a try, it works.
Regards

Wen

"Kees Hoogendijk" <ni********@haalditweg-hcopleidingen.nl> schreef in
bericht news:bs**********@news.cistron.nl...
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();
}
}

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.