This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Strange behaviour of "cin"


At 05.00 17/02/2002 (GMT +0530), Arun Saini , Gurgaon wrote:

>Ok. I finally traced the problem to the ctype header. I wrote a small test
>program. It consists of a shared library and an executable that is linked to
>that shared library.
>
>The contents of the shared library are :
>
>/* file : mylib.cpp */
>#include <ctype.h>
>
>int f(unsigned char c)
>{
>   if (isspace(c));
>
>   return 0;
>}
>
>
>The executable is :
>
>/* file : mytest.cpp */
>#include <iostream>
>
>int main()
>{
>   int num1, num2;
>
>   std::cout << "Enter number 1 ->> ";
>   std::cin >> num1;
>
>   std::cout << std::endl << "Enter number 2 ->> ";
>   std::cin >> num2;
>
>   std::cout << std::endl << num1 << " + " << num2 << " = " << num1 + num2;
>
>   return 0;
>}
>
>I do the following :
>
>$ g++ -g3 -fPIC -c mylib.cpp
>$ g++ -G mylib.o -o libmylib.so
>$ g++ -g3 -fPIC -c mytest.cpp
>$ g++ mytest.o -L. -lmylib -o mytest
>
>$ ./mytest
>Enter number 1 ->> 12
>
>Enter number 2 ->>
>-2147398527 + -2147110048 = 4587211535$
>
>The crux is that the "cin" fails to accept a value. It waits for the user to
>input a value first time but subsequently it doesn't even stop.
>Now in the shared library (mylib.cpp) above, if I change the header from
>"ctype.h" to "cctype", the program executes correctly.
>
>Any ideas???
>
>Arun Saini

Well, <cctype> is the Right header for C++.
However, I tried it, and it works for me, both with <ctype.h> and <cctype>. 
In either case, it goes like

$ g++3 -g3 -fPIC -c mylib.cc
$ g++3 -shared mylib.o -o libmylib.so
$ g++3 -g3 -fPIC -c mytest.cc
$ g++3 mytest.o -L. -lmylib -o mytest
$ ./mytest
Enter number 1 ->> 12

Enter number 2 ->> 30

12 + 30 = 42


/*
RedHat Linux 7.2
GCC 3.0.2
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man 
--infodir=/usr/share/info --enable-shared --enable-threads=posix 
--disable-checking --host=i386-redhat-linux
Thread model: posix
gcc version 3.0.2 20010905 (Red Hat Linux 7.1 3.0.1-3)
*/

fwyzard



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]