getline and substr



 DEVELOP > c-Plus-Plus > getline and substr

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Kees Hoogendijk"
Date: 20 Dec 2003 08:39:56 AM
Object: getline and substr
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();
}
}
.

User: "Ron Natalie"

Title: Re: getline and substr 20 Dec 2003 09:51:51 AM
"Kees Hoogendijk" <nieuwskees@haalditweg-hcopleidingen.nl> wrote in message
news:bs1n4j$fps$1@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;
.

User: "Moonlit"

Title: Re: getline and substr 20 Dec 2003 09:09:22 AM
Hi,
"Kees Hoogendijk" <nieuwskees@haalditweg-hcopleidingen.nl> wrote in message
news:bs1n4j$fps$1@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.
.

User: "Jeff Schwab"

Title: Re: getline and substr 20 Dec 2003 09:25:09 AM
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;
}
.

User: "Kees Hoogendijk"

Title: Re: getline and substr 21 Dec 2003 04:23:17 AM
Thank you very much, three of you.
I 'd take a try, it works.
Regards
Wen
"Kees Hoogendijk" <nieuwskees@haalditweg-hcopleidingen.nl> schreef in
bericht news:bs1n4j$fps$1@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();
}
}


.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER