Like Tree1Likes
  • 1 Post By hk_mp5kpdw

Missing an input

This is a discussion on Missing an input within the C++ Programming forums, part of the General Programming Boards category; In the following function it works fine the first time through the loop but on the second pass instead of ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    Missing an input

    In the following function it works fine the first time through the loop but on the second pass instead of outputting "Employees name : " and waiting for the input it outputs "Employees name : Payrate : " and waits for that output.

    Why is it skipping the name input please?

    Code:
    int GetInfo()
    {
         char name[256];
         float rate;
         float hours;
    
        for (int x(0); x<6; x++)
           {
            cout<<"Employees name : ";
            cin.getline (name,256);
            //employee.at[x]=name;
    
            cout<<"Payrate : ";
            cin>>rate;
            //payrate.at[x]=rate;
    
            cout<<"Hours worked : ";
            cin>>hours;
            //hoursworked.at[x]=hours;
            //totalpay.at[x]=rate * hours;
    
            cout<<"Employee : "<<name<<"\n";
            cout<<"Payrate     : "<<rate<<"\n";
            cout<<"Hours       : "<<hours<<"\n";
           }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    istream::ignore - C++ Reference
    Between >> and getline calls, you need to "flush" the input stream up to the next newline.

    Mixing input methods is always a hit and miss affair.
    Ideally, you should just use getline() to read to some buffer, then extract what you need from the buffer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Salem has explained what to do, here is the "why this is happening" part:

    At the end of the first time through the loop you ask the user for the "hours worked". You enter a value and press the enter key. The cin>> statement extracts and stores the numeric value but leaves the newline character in the input stream. This leftover data causes the problem when you start the loop body over again. The getline statement sees the newline character and extracts it (effectively acting as if the user simply pressed enter at this point without entering a name) before continuing on with the rest of the code by asking for the pay rate. This has the effect of the program skipping allowing the user to enter data for the employee's name for the second and subsequent loop iterations (the first loop iteration is fine).

    As hinted by Salem this is due a difference in how input is handled between cin>> and getline and why Salem mentioned the caveat about mixing such input statements. This is a very common problem for beginners to trip themselves up on.
    Salem likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    I have found that if I use cin.ignore(); after the cin>>hours; that works OK; however if I use it before cin.getline (name,256); I lose the first character of the name but only on the first pass through the loop; it works OK for the second and subsequent passes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Am I missing something?
    By ke121885 in forum C Programming
    Replies: 8
    Last Post: 10-11-2009, 09:45 PM
  2. "Missing '}' before input" error
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 05:39 PM
  3. Missing something
    By campermama in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2004, 11:43 PM
  4. Someone tell me what I'm missing here?
    By Furious_George in forum C++ Programming
    Replies: 7
    Last Post: 10-06-2003, 12:27 AM
  5. missing something ?
    By Unregistered in forum C# Programming
    Replies: 1
    Last Post: 04-29-2002, 08:08 PM

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