Thread: cin.getline

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    11

    cin.getline

    I'm having a problem with prompting and getting user input. Below is my code
    Code:
    sendRegisterSymbol() 
    {
       char tmp[256];
    
       CSIString domain;
       CSIString mfile;
       CSIString instance;
       CSIString symbol;
    
       std::cout << "Enter a domain name:\n";
       std::cin.getline(tmp, sizeof(tmp));
       domain.assign(tmp);
      
       std::cout << "Enter an mfile name:\n";
       std::cin.getline(tmp, sizeof(tmp));
       mfile.assign(tmp);
    
       std::cout << "Enter an mfile instance:\n";
       std::cin.getline(tmp, sizeof(tmp));
       instance.assign(tmp);
    
       std::cout << "Enter a symbol:\n";
       std::cin.getline(tmp, sizeof(tmp));
       symbol.assign(tmp);
    
       .
       .
       .
    The problem here is that first time this code is encountered, the first piece(everything dealing with domain is sort of passed by. For instance, when I run the program, the user gets prompted as follows:

    >Enter a domain name: Enter an mfile name:

    The user doesn't get the option to enter the domain.

    Every subsequent time that sendRegisterSymbol() is called, everything works as I want it to:

    >Enter a domain name:

    Its only the first time I enter the function that I get the bad output.

    I tried adding cin.ignore prior to the code dealing with domain. That fixes it the first time through, but then every subsequent time I call sendRegisterSymbol, I have to hit enter twice to get to where I want to.

    Any ideas???? Thanks in advance!


    Please use [code][/code]Tags

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    11
    so would it make sense to do a fflush(stdin); as soon as the sendRegisterSymbol() method is called? I would think so, but it doesn't seem to be helping.

  3. #3
    boredsilly@work
    Guest
    You cant call fflush on stdin. You can only flush output streams like that never input streams.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    It is never ok to flush stdin: http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Your only solution is to place the summy getline() before your getline() sequence, as Salem explained.

    And you might want to place a return type on that function. Defualting to int is no longer allowed.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    11
    disregard the fflush solution. I did a work around:

    code:
    __________________________________________
    sendRegisterSymbol()
    {
    char tmp[256];
    static bool firstTime=true;
    CSIString domain;
    CSIString mfile;
    CSIString instance;
    CSIString symbol;

    if(firstTime)
    {
    cin.ignore(1, '\n');
    firstTime=false;
    }

    std::cout << "Enter a domain name:\n";
    std::cin.getline(tmp, sizeof(tmp));
    domain.assign(tmp);

    std::cout << "Enter an mfile name:\n";
    std::cin.getline(tmp, sizeof(tmp));
    mfile.assign(tmp);

    std::cout << "Enter an mfile instance:\n";
    std::cin.getline(tmp, sizeof(tmp));
    instance.assign(tmp);

    std::cout << "Enter a symbol:\n";
    std::cin.getline(tmp, sizeof(tmp));
    symbol.assign(tmp); .
    .
    .
    _____________________________________________
    static bool firstTime=true;

    if(firstTime)
    {
    cin.ignore(1, '\n');
    firstTime=false;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 06:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM