getline and substr

K

Kees Hoogendijk

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();
}
}
 
M

Moonlit

Hi,


Kees Hoogendijk said:
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.
 
J

Jeff Schwab

Kees said:
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;
}
 
R

Ron Natalie

Kees Hoogendijk said:
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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top