iansane
May 7th, 2008, 11:55 AM
Hi, I have been learning from a book that uses system("pause") at the end of simple programs to keep the window open so I can look at the output.
Actually I don't think that came from the book, but somewhere I picked it up and it works for windows.
The book mentions one time, the following line.
cin.ignore(std::cin.rdbuf()->in_avail() + 1)
It doesn't explain the code at all. Just says use it to wait for the user to press enter.
The problem is, for it to work in Ubuntu terminal, I had to put cin.get() after every cin in my program to clear the cin stream.
I think my problem might be, not understanding the cin stream, because I wrote a program a few days ago where when a user inputs a name with spaces in it, the program uses everything after the first space in the cin to answer all of the other cin's in the program. Like "my app" would cause the program to take "my" as the name and then put " ", "a", "p", and "p" into the next 4 cin's in the program as input.
Someone told me not to use cin, but to use std::getline() instead. If std::getline() is the correct way, then why does the book I'm using teach to use cin?
Anyway, the questions I have are, is there a simpler way than the cin.ignore()? And can someone give me a newb like explaination of what's going on in cin stream and how to clear it up? BTW, I tried cin.clear() and it had no affect on the app.
Also, that cin.ignore, seems to not require a "+ 1" at the end of it in windows, but it does in linux terminal. Is terminal counting an extra space or something?
Sorry so long and Thanks for any replies.:-)
Actually I don't think that came from the book, but somewhere I picked it up and it works for windows.
The book mentions one time, the following line.
cin.ignore(std::cin.rdbuf()->in_avail() + 1)
It doesn't explain the code at all. Just says use it to wait for the user to press enter.
The problem is, for it to work in Ubuntu terminal, I had to put cin.get() after every cin in my program to clear the cin stream.
I think my problem might be, not understanding the cin stream, because I wrote a program a few days ago where when a user inputs a name with spaces in it, the program uses everything after the first space in the cin to answer all of the other cin's in the program. Like "my app" would cause the program to take "my" as the name and then put " ", "a", "p", and "p" into the next 4 cin's in the program as input.
Someone told me not to use cin, but to use std::getline() instead. If std::getline() is the correct way, then why does the book I'm using teach to use cin?
Anyway, the questions I have are, is there a simpler way than the cin.ignore()? And can someone give me a newb like explaination of what's going on in cin stream and how to clear it up? BTW, I tried cin.clear() and it had no affect on the app.
Also, that cin.ignore, seems to not require a "+ 1" at the end of it in windows, but it does in linux terminal. Is terminal counting an extra space or something?
Sorry so long and Thanks for any replies.:-)