GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Dec-2008, 11:14
nostaque nostaque is offline
New Member
 
Join Date: Oct 2008
Posts: 19
nostaque will become famous soon enough

getline function problem


Hi guys,

I am stuck at a very simple code. Could you please help me out.

The getline() function works as normal, but inside the if statement the function does not perform.

I tried on dev-c++ and code::block under winxp sp2.

Thanks in advance,

CPP / C++ / C Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string s, line;
   cout <<"enter line: ";
   getline(cin, line);
   cout << line << endl;

   cout <<"1. Add chore to the list. \n";
   cout <<"2. Delete chore from the list. \n";

   int choice;
   cout <<"Enter your choice: ";
   cin >> choice;
   if(choice == 1)
   {
      cout <<"Enter chore: ";
      getline(cin, line);
      cout << line << endl;
   }
   if(choice == 2)
   {
      cout <<"Enter chore to delete: \n";
      getline(cin, line);
      cout << line << endl;
   }
return 0;
}
  #2  
Old 03-Dec-2008, 11:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: getline function problem


Quote:
Originally Posted by nostaque
...
The getline() function works as normal...

Here's the drill: getline() stops reading when it encounters a newline character in the stream. (It removes the newline from the stream but does not put it into the string.)

When you use ">>" to read a numeric data value into your program, the ">>" converts everything it can from the input stream, and then stops. So, there will always be (at least) a newline character left in the system input buffer. The next time you call getline, it sees the newline and returns with the string empty.

Bottom line: After using ">>" to read a numeric value, you get rid of the rest of that line before calling getline.

For example, in your program:
CPP / C++ / C Code:
    int choice;
    cout << "Enter your choice: ";
    cin >> choice;
    if (!cin) {
        cout << "Invalid entry: Can't convert to integer." << endl;
        exit(EXIT_FAILURE);
    }

    while (cin.get() != '\n') // A do-nothing loop to eat the rest of the line
        ;

    if (choice == 1) {
        cout << "Enter chore: ";
        getline(cin, line); // Starts with a brand new input line
.
.
.


Another way might be to use an extra getline after the ">>" operation:
CPP / C++ / C Code:
    int choice;
    cout << "Enter your choice: ";
    cin >> choice;
    if (!cin) {
        cout << "Invalid entry: Can't convert to integer." << endl;
        exit(EXIT_FAILURE);
    }
    string dummy;
    getline(cin,dummy); // Read the rest of the line and throw it away

    if (choice == 1) {
        cout << "Enter chore: ";
        getline(cin, line);
        cout << line << endl;
    }

Another way, that some prefer, is to use the istream member function ignore()

CPP / C++ / C Code:
    cin >> choice;
    if (!cin) {
        cout << "Invalid entry: Can't convert to integer." << endl;
        exit(EXIT_FAILURE);
    }
    // Eat the rest of the line
    cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

    if (choice == 1) {
        cout << "Enter chore: ";
        getline(cin, line);
        cout << line << endl;
    }

Regards,

Dave
  #3  
Old 03-Dec-2008, 15:29
nostaque nostaque is offline
New Member
 
Join Date: Oct 2008
Posts: 19
nostaque will become famous soon enough

Re: getline function problem


Wow !
That's something you don't see everyday.
Thanks Dave,
  #4  
Old 05-Dec-2008, 01:09
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: getline function problem


Quote:
Originally Posted by nostaque
Wow !
That's something you don't see everyday.
We do! .
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Flex and bison coding lucky88star C++ Forum 5 24-Dec-2007 12:57
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:34.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.