Pages

Friday, July 2, 2010

iostream buffer issues, cin, cout, cin.get, cin.getline

Here are useful links to iostream buffer related issues.

http://www.cplusplus.com/forum/beginner/7405/
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html


you may also wish to read the chapter on I/O in the book by Walter Savitch, it is available in the library

In case you are having issues where your code seems to skip cin or cin.getline you can use
std::cin.ignore(std::numeric_limits::max(),'\n');
or, since you have 'using namespace std;', you can add this
cin.ignore(numeric_limits::max(),'\n');

to flush the buffer before you take input you will need to include ios.h and limits.h in order for it to work

sample

# include
# include
# include
# include
using namespace std;

int main()
{

.
.
.
.
.

string string1;
cin.ignore(numeric_limits::max(),'\n');
cin.getline(cin,string1);


}

4 comments:

  1. sir, i found an error in passing socketobject as parameter in thread function...
    error:
    `new_sock' was not declared in this scope.!!!!

    ReplyDelete
  2. Paste your code so that i can figure out the error

    ReplyDelete
  3. Please refer to my latest post, tips for socket programming lab 2. I have tried to answer all your questions, even those unasked :)

    ReplyDelete
  4. #include
    #include "ServerSocket.h"
    #include "SocketException.h"
    #include
    #include
    #include
    using namespace std;

    void *message_function( void *ptr );

    int main ( int argc,int argv[] )
    {
    pthread_t thread1;
    int iret1;
    cout << "running....\n";
    string data;
    try
    {
    // Create the socket
    ServerSocket server ( 30003 );
    while ( true )
    {
    ServerSocket new_sock;
    server.accept ( new_sock );
    try
    {
    while(1)
    {
    new_sock >> data;
    std::cout << "We received this response from the client:\n\"" << data << "\"\n";;

    iret1 = pthread_create( &thread1, NULL, message_function, (void*) new_sock);
    }
    }
    catch ( SocketException& ) {}
    }
    }
    catch ( SocketException& e )
    {
    std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
    }
    pthread_join( thread1, NULL);
    exit(0);
    return 0;
    }


    void *message_function( void *ptr )
    {
    string reply;
    char A[1000];
    new_sock = (ServerSocket *) ptr;

    std::cout<<"Enter:";
    cin.getline(A, 200);
    reply = A;
    new_sock <
    #include
    #include
    #include
    using namespace std;

    void *message_function( void *ptr );

    int main ( int argc,int argv[] )
    {
    pthread_t thread1;
    int iret1;
    try
    {
    ClientSocket client_socket ( "localhost", 30003 );
    string reply;
    try
    {
    while(1)
    {
    iret1 = pthread_create( &thread1, NULL,
    message_function, (void*)client_socket );

    client_socket >> reply;
    std::cout << "We received this response from the server:\n\"" << reply << "\"\n";;
    }
    }
    catch ( SocketException& )
    {
    std::cout << "Exception was caught:";
    }
    }
    catch ( SocketException& e )
    {
    std::cout << "Exception was caught:" << e.description() << "\n";
    }
    pthread_join( thread1, NULL);
    exit(0);
    return 0;
    }


    void *message_function( void *ptr )
    {
    string *data=new string;
    client_socket = (ClientSocket *) ptr;

    std::cout << "enter : ";
    std::getline( cin, data, '\n' );
    client_socket << data;
    }

    ReplyDelete