![]() |
|
#1
|
|||
|
|||
getline function problemHi 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:
|
|||
|
#2
|
|||
|
|||
Re: getline function problemQuote:
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:
Another way might be to use an extra getline after the ">>" operation: CPP / C++ / C Code:
Another way, that some prefer, is to use the istream member function ignore() CPP / C++ / C Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: getline function problemWow !
That's something you don't see everyday. Thanks Dave, |
|
#4
|
||||
|
||||
Re: getline function problemQuote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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