views:

168

answers:

4

This is one of the weirder things I've seen. I'm teaching an intro C++ course at a university, and one of my students contacted me saying that his code was running forever without stopping. I briefly glanced over his code during class, and didn't see anything immediately obvious, so I had him email me his code.

Without making any changes, I downloaded and ran his code on my machine - and it worked fine. The only difference that I can see is that I'm using OSX, whereas he is using Windows.

Here's the code: http://pastie.textmate.org/private/9rzpttixnuhudsvsm1yl4q

Any ideas?

SOLVED: PEBKAC Abides

The issue actually had to do with what the student was typing. It simply never occured to me to try and enter a string when prompted for the deposit.

As an aside, regarding a disparaging comment made about the "quality" of programmers coming out universities these days: I'm not sure if that comment was directed at me, or at my student, but I'd like to briefly address both perspectives.

Assuming it was directed at me: I'm a TA for the course, not the main lecturer, and I'm responsible for teaching the lab assignments. So, by "teaching an intro C++ course," I meant "teaching material that's covered in the lab assignments." Regarding my own background and experience, I have to admit I'm feeling somewhat slighted. I am, first and foremost, an AI researcher, working in an area which is mostly theory (read: logic and math), where I don't have to write C++ code. The entire reason I posted this question to Stack Overflow was because I've always found helpful and creative solutions here. I thought to myself, "there's some weirdness here that I don't immediately see, but not to worry, the guys on SO have got this."

All that to say: I'm doing everything I can to help these kids learn the material, and well. That attitude helps nothing and no one.

Assuming it was directed at my student: c'mon, really? He's not even a CS major, and this assignment was from the third week of class - his third week of programming of any kind, ever. I'd rather not discourage him this early in the game.

+10  A: 

None of the input operations are checked. There are various lines like

cin >> deposit;

If the extraction from stdin fails (i.e., if the next thing in the stream is not a valid double in this case) then the fail state will be set on the stream and none of the subsequent input operations from stdin will succeed until the state is reset.

Since you only check the value of cont in the loop condition and don't test the stream state, the program might loop continuously if the fail state is ever set.

James McNellis
+1, I didn't notice this, but if you input "fiftythree" for deposit, it'll loop infinitely.
DeadMG
Turns out the student was actually doing something like that. That's the exact reason I couldn't reproduce the error - it never even occurred to me to enter a string.
Glen
A: 

Works fine for me on Visual Studio 2010 Professional. There's nothing wrong with the code, as far as I can see, except that he used getline(), and operator==. getline is the wrong thing to use with cin, he should use operator>> and see if that works better.

DeadMG
Please explain why `std::getline()` is wrong to use with `std::cin`. The `std::getline` function allows for parsing of different types and other things.
Thomas Matthews
Because if you get a line, it's not going to be exactly equal to input, it's going to have '\n' or '\r\n' on the end.
DeadMG
@Thomas is right on this, DeadMG, and you are wrong. `std::getline()` will return the line sans delimiter.
sbi
A: 

At a bare minimum, the code:

cout << setw(COL1) << "Do you want to enter another account? (Y/N) ";
getline(cin, cont);

needs to validate the input before continuing (keep re-prompting until either Y or N is given) and at some point give up and exit the program with an error after a number of failed attempts. This would at least prevent the code from looping endlessly, but it doesn't address the larger problem.

In a larger sense, James McNellis has it right. Other than checking the account type, there is practically no error checking in this program.

bta
A: 

I would assume it has something to do with how he gets his input from the console.

bobber205