Function using cin

This is a discussion on Function using cin within the C++ Programming forums, part of the General Programming Boards category; I am using a function which can be called many times. Inside the function I am using cin to take ...

  1. #1
    Sub
    Guest

    Question Function using cin

    I am using a function which can be called many times. Inside the function I am using cin to take a maximum off 255 characters type char. However, sometimes when the function is called more than once, debugging has shown that it is just going past the cin and not waiting for input.

    Can anyone tell me why this is happening?

    Many thanks, Sub.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Can anyone tell me why this is happening?

    I'd guess it's because there is already input left in the buffer when the function is called. But without seeing any code this is just speculation. What library functions are you using to obtain input?

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Without seeing the code all i can give is some general advice.

    cin >> leaves the delimiter in the stream

    cin.getline () does not leave the delimiter in the stream.

    this is the most common cause of this error. Using cin>> and then next using cin.getline() which appears to have taken no input. This is because the previous cin>> left the delimiter in the stream and getline takes that as its input. Use cin.ignore(80,'\n') after cin>> but before cin.getline(). Whatever the cause you have some chars left in the stream that need clearing.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Sub
    Guest

    Question

    so basically the variable that will be used again has to be cleared before cin?

    The variable declaration is: char Answer[MAX_BUFFER];
    where MAX_BUFFER is 255

    The input is taken form the user in the following way:
    cin >> Answer;

    I can't understand why c++ has to have the variable cleared each time you want to use it. Seems a bit inefficent.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    no you're not understanding us.
    cin is an object. Inside that object is a buffer called a streambuf.
    It is that streambuf that is internal to cin that needs clearing.
    show us your code if you want more help. If we cant see the problem we cant help you fix it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Are you trying to get input seperated by whitespace with operator >> ? If so anything entered after a whitespace character will be left to be picked up by the next call to an input function.

    As I said above, unless you post some code we could be guessing at your problem for some time.

  7. #7
    Unregistered
    Guest
    void FormatCheck()
    {
    char Answer[MAX_BUFFER];

    cin >> Answer;

    strcpy(Answer,strlwr(Answer));

    if(strlen(Answer) == 1 && Answer[0]== 'y')
    {
    AssignPasswords(Buffer);
    i = 0; // Loop is reset to check new passwords
    }
    else //User has selected no or invalid response returning back to main menu
    {
    Messages(3);
    i = MAX_LEVEL;//exit loop
    }
    }

    I was using cin.getline but i had to press enter twice, then once , then wasn't taking input upon pressing enter; loads of weird stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21