4

(a bit late for wk73 but I wanted to post this anyway)

Back in my first year of university, we had to write a relatively simple (though it looked super complicated back then) C++ console application. I don't know what it's called, but it's that game where the computer generates a random 4 digit code and you have to try to guess what it is. Every time you try, it will tell you which digits are correct, which would be correct if they were in a different position and which are outright wrong.

Anyway, the program had a main menu with a help option that would output a short guide on how to play the game. Instead of hard coding it into the source code, the "guide" had go be written in a separate text file and then read and dumped to the screen when necessary.

Here came my great idea on how to read files. Instead of looping through the file until I reached the end, I counted the number of lines my text file had and wrote some gem of a piece of code like this:

for (int i = 0; i<11; i++){
line = file.readline();
cout << line << endl;
}

My teacher obviously took points off for doing such a stupid thing, and I remember complaining A LOT about it. I argued that 11 was a constant because I didn't plan on changing the text file, and that the teacher had no right to take points off for only reading 11 lines because the file only had 11 lines, so it was read in full.

Goddammit, what an innocent little brat I was. I'm glad my first programming teachers were good enough to stay firm and teach me how to do things the right way, even if it's the hard way.

Comments
Add Comment