相同的 C++代码在 Windows 上导致无限循环,在 OSX 上导致预期行为

发布于 2024-09-24 19:54:36 字数 955 浏览 0 评论 0原文

这是我见过的最奇怪的事情之一。我在一所大学教授 C++ 入门课程,我的一个学生联系我说他的代码永远运行而不停止。我在课堂上简单地浏览了他的代码,并没有立即看到任何明显的东西,所以我让他通过电子邮件给我发送了他的代码。

在没有进行任何更改的情况下,我下载了他的代码并在我的机器上运行 - 并且运行良好。我能看到的唯一区别是我使用的是 OSX,而​​他使用的是 Windows。

这是代码: http://pastie.textmate.org/private/9rzpttixnuhudsvsm1yl4q

有什么想法吗?

已解决:PEBKAC 仍然存在

该问题实际上与学生输入的内容有关。当提示存款时,我根本没有想到要尝试输入字符串。

顺便说一句,关于最近大学出来的程序员“质量”的贬低评论:我不确定这个评论是针对我还是我的学生,但我想简要谈谈这两个观点。

假设它是针对我的:我是该课程的助教,而不是主讲师,我负责教授实验作业。因此,“教授 C++ 入门课程”是指“实验室作业中涵盖的教学材料”。就我自己的背景和经历而言,我不得不承认我感觉有点被轻视。首先,我是一名人工智能研究员,工作的领域主要是理论(即逻辑和数学),我不需要编写 C++ 代码。我将这个问题发布到 Stack Overflow 的全部原因是因为我总是在这里找到有用且富有创意的解决方案。我心想,“这里有一些奇怪的地方,我没有立即看到,但不用担心,SO 上的人已经明白了。”

总而言之:我正在尽我所能帮助这些孩子学习这些材料,而且很好。 这种态度对任何人都没有帮助。

假设它是针对我的学生:来吧,真的吗?他甚至不是计算机科学专业的学生,​​而这项作业是从课程的第三周开始的 - 他任何类型编程的第三周,曾经。我不想在比赛初期就让他泄气。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唐婉 2024-10-01 19:54:36

不检查任何输入操作。有多种行,例如

cin >> deposit;

如果从标准输入中提取失败(即,在这种情况下,如果流中的下一个内容不是有效的双精度),则将在流上设置失败状态,并且不会来自 stdin 的后续输入操作将成功,直到状态重置。

由于您只检查循环条件中的 cont 值,而不测试流状态,因此如果设置了失败状态,程序可能会连续循环。

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.

山田美奈子 2024-10-01 19:54:36

我在 Visual Studio 2010 Professional 上工作得很好。据我所知,代码没有任何问题,除了他使用了 getline() 和运算符==。 getline 与 cin 一起使用是错误的,他应该使用运算符>>看看是否效果更好。

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.

人心善变 2024-10-01 19:54:36

至少,代码:

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

需要在继续之前验证输入(不断重新提示,直到给出 YN),并在某些时候放弃并退出多次尝试失败后程序出现错误。这至少可以防止代码无限循环,但它并不能解决更大的问题。

从更广泛的意义上来说,詹姆斯·麦克内利斯是对的。除了检查帐户类型之外,该程序实际上没有错误检查。

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.

一张白纸 2024-10-01 19:54:36

我认为这与他如何从控制台获取输入有关。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文