![]() |
|
#1
|
|||
|
|||
Need help with getline. Unable to insert strings.Hello everyone,
I wrote a small program in order to find out how getline works but out of nowhere, I ended up trying to understand what happens and it is not working properly. Till now I found out nothing. Could you please help me? Below is the code. CPP / C++ / C Code:
a: (waits value, I insert 4) s1: b: (waits value) Why does it not request from the user to insert a value for s1?? It goes directly to b:. The same happens with b and s2 until the loop ends. Any ideas? Thank you |
|||
|
#2
|
|||
|
|||
Re: Need help with getline. Unable to insert strings.It is because you are mixing cin and getline. Long story short cin leaves the '\n' in the stream and this is the delimiter for getline so getline finds the '\n' and stops reading. Just add cin.ignore();(ignore will skip 1 character with no arguments) before getline.
CPP / C++ / C Code:
|
|
#3
|
||||
|
||||
Re: Need help with getline. Unable to insert strings.To add little info to Sokar's excellent description:
CPP / C++ / C Code:
cin: 5 & 6 read, <enter> is not a digit Buffer: <enter> CPP / C++ / C Code:
cin: <enter> read Buffer: empty CPP / C++ / C Code:
cin: 1 & 2 & 9 read, <enter> is not a digit Buffer: <enter> CPP / C++ / C Code:
cin: <enter> read Buffer: empty __________________
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
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strings tripping me up once more | Elsydeon | C++ Forum | 5 | 04-Dec-2005 18:41 |
| need help - questions about strings | Benayoun | C Programming Language | 6 | 24-Jan-2005 03:15 |
| array of pointers to strings | mirizar | C++ Forum | 5 | 21-Jan-2005 11:24 |
| I am reviewing Arrays and need help converting some strings to arrays | jenmaz | C Programming Language | 22 | 23-Nov-2004 00:26 |
| urgent help needed :c + mysql insert | jack | C Programming Language | 1 | 13-Apr-2004 22:16 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The