All Questions

439
votes
4answers
37526 views

Why is iostream::eof inside a loop condition considered wrong?

I just found a comment in this answer saying that using iostream::eof in a loop condition is "almost certainly wrong". I generally use something like while(cin>>n) - which I guess implicitly che...
62
votes
2answers
8817 views

Why does std::getline() skip input after a formatted extraction?

I have the following piece of code that prompts the user for their name and state: #include <iostream> #include <string> int main() { std::string name; std::string state; if...
393
votes
10answers
136340 views

C++: "std::endl" vs "\n"

Many C++ books contain example code like this... std::cout << "Test line" << std::endl; ...so I've always done that too. But I've seen a lot of code from working developers like this in...
18
votes
9answers
1893 views

Why does reading a record struct fields from std::istream fail, and how can I fix it?

Suppose we have the following situation: A record struct is declared as follows struct Person { unsigned int id; std::string name; uint8_t age; // ... }; Records are stored ...
257
votes
10answers
306922 views

How do I print a double value with full precision using cout?

So I've gotten the answer to my last question (I don't know why I didn't think of that). I was printing a double using cout that got rounded when I wasn't expecting it. How can I make cout print a d...
60
votes
8answers
51140 views

Output unicode strings in Windows console app

Hi I was trying to output unicode string to a console with iostreams and failed. I found this: Using unicode font in c++ console app and this snippet works. SetConsoleOutputCP(CP_UTF8); wchar_t s[...
52
votes
3answers
128241 views

Why would we call cin.clear() and cin.ignore() after reading input?

Google Code University's C++ tutorial used to have this code: // Description: Illustrate the use of cin to get input // and how to recover from errors. #include <iostream> using namespace std;...
104
votes
7answers
204920 views

Reading from text file until EOF repeats last line

The following C++ code uses a ifstream object to read integers from a text file (which has one number per line) until it hits EOF. Why does it read the integer on the last line twice? How to fix this?...
61
votes
9answers
58595 views

Java IO implementation of unix/linux "tail -f"

I'm wondering what techniques and/or library to use to implement the functionality of the linux command "tail -f ". I'm essentially looking for a drop in add-on/replacement for java.io.FileReader. C...
167
votes
5answers
250223 views

How to properly overload the << operator for an ostream?

I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my comput...

Previous Next