为什么此代码段无限循环?

编程入门 行业动态 更新时间:2024-10-08 02:21:27
本文介绍了为什么此代码段无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对编程还很陌生,我正尝试从 cin 过滤输入(没有特殊原因;这是我所知道的唯一输入方法)现在),这样一来只能输入1-10并获得肯定响应。这是我的代码:

I'm fairly new to programming, and I'm trying to filter inputs from cin (for no particular reason; It is the only input method I know of right now) such that one can only enter 1-10 and get the "positive" response. Here is my code:

#pragma region Check cin is a valid input (an integer between 1 and 10, not a string or number out of range) int input; bool valid2=false; while (!valid2) { cout << "Enter an integer (1-10): "; cin >> input; if (!cin || input > 10 || input < 1) { cout << "Error: Invalid. Try again." << endl << endl; cin.ignore(numeric_limits<streamsize>::max()); cin.clear(); } else { valid2 = true; } } #pragma endregion

忽略 pragma ,对于每个小实验,我都有一个带有块的测试脚本,以节省时间,这就是当它们不再有效时我将它们折叠的方法。

Ignore the "pragma", I have one test script with blocks for each little experiment to save me time and this is just how I collapse them when they're no longer valid.

我的问题(除其他外)是,当我输入 f 之类的非整数时,它会无限循环错误:无效。请重试 ,我不知道为什么。我以为也许是由于定义了 input 之后,它又回到了 cin 之后。但是我检查了一个孤立的示例,在继续之前, cin 让我专门重新定义了输入。

My problem (among others) is that when I enter a non-integer such as f, it loops infinitely with "Error: Invalid. Try Again", and I can't figure out why. I thought maybe since input became defined, it was running past cin when it comes back around. But I checked an isolated example, and cin let me specifically redefine input before continuing.

侧边栏:说到 pragma ,有人在c ++中有更好的方法吗?除了创建单个方法之外,因为我的大多数测试都可以在 main 内部运行。

Sidebar: Speaking of pragma, does anyone have a better method for this in c++? Aside from making individual methods, because most of my tests can just run inside main.

编辑: 我已经切换了 cin.ignore(...)和 cin.clear()现在程序可以正确过滤输入了;但是现在,它根本不会循环播放。给我后,错误:无效。请重试。 ,它挂在应再次要求我输入整数的位置。

I've switched cin.ignore(...) and cin.clear() and now the program can properly filter the inputs; however now, it doesn't loop back at all. After giving me "Error: Invalid. Try again.", it hangs where it should ask me again to enter the integer.

推荐答案

您的原始代码有两个问题(至少就我发现的问题而言-不保证不再存在):

Your original code has TWO problems (at least as far as I've spotted - not guaranteeing there aren't any more):

  • cin.clear()和 cin的顺序。 ignore()- cin.clear()应该在 cin.ignore()之前,或 cin.ignore()无效。
  • cin.ignore(numeric_limits< streamsize> :: max()); 一直读取到分隔符为止,默认情况下,该分隔符为 EOF -换句话说,无论缓冲区中输入了什么或在缓冲区中输入了什么,它都会继续接受它。
  • The order of cin.clear() and cin.ignore() - cin.clear() should be before cin.ignore(), or cin.ignore() will do nothing.
  • The cin.ignore(numeric_limits<streamsize>::max()); is reading until the delimiter, which defaults to EOF - in other words, no matter what input is in the buffer, or entered after, it keeps on accepting it.
  • 要解决这两个问题,请使用以下方法:

    To fix both problems use this:

    cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n');

    这将一直读到当前行的末尾,这可能与您所读内容的相似想。

    that will read until the end of the current line, which is probably more along the lines of what you want.

    哦,然后更改:

    cin >> input; if (!cin || input > 10 || input < 1)

    到:

    if (!(cin >> input) || input > 10 || input < 1)

    更多推荐

    为什么此代码段无限循环?

    本文发布于:2023-11-29 15:45:15,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1646836.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:代码

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!