|
From: Chris B. <ch...@cb...> - 2000-10-28 15:09:52
|
Hello everyone.
Ive written the following function which overloads the insertion =
operator for a class for restricting the input of a string to digits =
only:
class mystring
{
char *str;
// ....
public:
friend istream& operator >> (istream &is, mystring &rhs);
// ...
};
istream& operator >> (istream &is, mystring &rhs)
{
char buf[256];
char ch;
short len =3D 0;
do
{
ch =3D getch();
if(isdigit(ch))
{
cout << ch;
buf[len++] =3D ch;=20
}
} while(ch!=3D13); // while not 'return'. =20
buf[len] =3D '\0'; // terminate string;
rhs.str =3D new char[len];
strcpy(rhs.str, buf);
return is;
}
This compiles and runs fine on my compilers, but when I compile it and =
run it on my college compiler (MSVC++) getch and cin/cout dont get =
along!
My question is - being as getch() is non ansi and it also uses different =
buffers than cin/cout - is there a way of writing a simular function =
without using getch? getch is good because it doesn't display inputted =
characters and doesn't wait for a return keystroke, but I dont know how =
to achieve the same thing using cin/cout.
Any ideas?
Thanks in advance=20
Chris
|