std :: cin无限循环输入错误

我是蝙蝠侠

在下面的代码中,我想循环播放,直到用户提供正确的输入为止。但是当我尝试时,它变成了不间断的循环。
Please Enter Valid Input.
没有while循环,它也是一样的。

这里有while循环:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <sstream>
using namespace std;

class library {
public:
    library() {
        int mainOption;

        cout<<"Please choose the option you want to perform."<<endl;
        cout<<"1. Member Section"<<"\n"<<"2. Books, Lending & Donate Section"<<"\n"<<"3. Returning Section"<<endl;
        bool option=true;
        while (option==true) {
            cin>>mainOption;
            if (mainOption==1) {
                cout<<"section 1"<<endl;
                option=false;
            } else if (mainOption==2) {
                cout<<"section 1"<<endl;
                option=false;
            } else if (mainOption==3) {
                cout<<"section 1"<<endl;
                option=false;
            } else {
                cout<<"Please Enter Valid Input. "<<endl;
                //option still true. so it should ask user input again right?
            }
        }
    }
};

int main(int argc, const char * argv[])
{
    library l1;
    return 0;
}

这里没有while循环。但是,同样的事情正在发生。

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <sstream>
using namespace std;

class library {
public:
    library() {
        int mainOption;

        cout<<"Please choose the option you want to perform."<<endl;
        cout<<"1. Member Section"<<"\n"<<"2. Books, Lending & Donate Section"<<"\n"<<"3. Returning Section"<<endl;

        cin>>mainOption;
        if (mainOption==1) {
            cout<<"section 1"<<endl;
        } else if (mainOption==2) {
            cout<<"section 1"<<endl;
        } else if (mainOption==3) {
            cout<<"section 1"<<endl;
        } else {
            cout<<"Please Enter Valid Input. "<<endl;
            library();//Calling library function again to input again.
        }
    }
};

int main(int argc, const char * argv[])
{
    library l1;
    return 0;
}
谢尔盖·卡里尼琴科(Sergey Kalinichenko)

问题是当您打电话时

cin>>mainOption; // mainOption is an int

但用户输入int,则cin使输入缓冲区保持旧状态。除非您的代码占用了输入的无效部分,否则最终用户输入的错误值将保留在缓冲区中,从而导致无限次重复。

解决方法如下:

} else {
    cout<<"Please Enter Valid Input. "<<endl;
    cin.clear(); // Clear the error state
    string discard;
    getline(cin, discard); // Read and discard the next line
    // option remains true, so the loop continues
}

请注意,我还删除了递归,因为您的while循环足以应付手头的任务。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

来自无效输入的std :: cin无限循环

来自分类Dev

std::cin 导致无限循环

来自分类Dev

使用std :: cin输入停止while循环

来自分类Dev

std :: cin不会在输入错误时抛出异常

来自分类Dev

(C ++)不会在std :: cin上停止并进入无限循环吗?

来自分类Dev

(C ++)不会在std :: cin上停止并进入无限循环吗?

来自分类Dev

我可以循环直到 `std::cin` 在输入缓冲区中看到 `\n` 字符吗?

来自分类Dev

使用std :: cin中的std :: setw限制输入大小

来自分类Dev

使用std :: ofstream C ++的无限while循环

来自分类Dev

错误:std :: cin的“ operator >>”不匹配

来自分类常见问题

输入错误的std :: strftime崩溃

来自分类Dev

输入错误的std :: strftime崩溃

来自分类Dev

输入错误时程序进入无限循环

来自分类Dev

std :: cin跳过空格

来自分类Dev

std :: cin:清空输入缓冲区而不会阻塞

来自分类Dev

std :: cin:清空输入缓冲区而不会阻塞

来自分类Dev

std :: cin不接受输入,程序立即关闭

来自分类Dev

为什么std :: cin同时接受多个输入?

来自分类Dev

在while循环中如何评估“ std :: cin >> value”?

来自分类Dev

即使我调用ignore()和clear(),std :: cin也会循环

来自分类Dev

std :: cin >> std :: string如何实现?

来自分类Dev

std :: cout << std :: cin有什么作用?

来自分类Dev

在opengl应用程序中使用std :: cin进行std :: thread和输入

来自分类Dev

std :: cin.fail()问题

来自分类Dev

输入std :: function的别名

来自分类Dev

std :: list是循环的吗?

来自分类Dev

在最后一次出现定界符之后,std :: getline会从std :: cin跳过输入,但不会与std :: istringstream的输入一起跳过

来自分类Dev

while循环无限验证对错误输入的响应

来自分类Dev

while/if 语句导致无限循环,正确的输入读取错误

Related 相关文章

  1. 1

    来自无效输入的std :: cin无限循环

  2. 2

    std::cin 导致无限循环

  3. 3

    使用std :: cin输入停止while循环

  4. 4

    std :: cin不会在输入错误时抛出异常

  5. 5

    (C ++)不会在std :: cin上停止并进入无限循环吗?

  6. 6

    (C ++)不会在std :: cin上停止并进入无限循环吗?

  7. 7

    我可以循环直到 `std::cin` 在输入缓冲区中看到 `\n` 字符吗?

  8. 8

    使用std :: cin中的std :: setw限制输入大小

  9. 9

    使用std :: ofstream C ++的无限while循环

  10. 10

    错误:std :: cin的“ operator >>”不匹配

  11. 11

    输入错误的std :: strftime崩溃

  12. 12

    输入错误的std :: strftime崩溃

  13. 13

    输入错误时程序进入无限循环

  14. 14

    std :: cin跳过空格

  15. 15

    std :: cin:清空输入缓冲区而不会阻塞

  16. 16

    std :: cin:清空输入缓冲区而不会阻塞

  17. 17

    std :: cin不接受输入,程序立即关闭

  18. 18

    为什么std :: cin同时接受多个输入?

  19. 19

    在while循环中如何评估“ std :: cin >> value”?

  20. 20

    即使我调用ignore()和clear(),std :: cin也会循环

  21. 21

    std :: cin >> std :: string如何实现?

  22. 22

    std :: cout << std :: cin有什么作用?

  23. 23

    在opengl应用程序中使用std :: cin进行std :: thread和输入

  24. 24

    std :: cin.fail()问题

  25. 25

    输入std :: function的别名

  26. 26

    std :: list是循环的吗?

  27. 27

    在最后一次出现定界符之后,std :: getline会从std :: cin跳过输入,但不会与std :: istringstream的输入一起跳过

  28. 28

    while循环无限验证对错误输入的响应

  29. 29

    while/if 语句导致无限循环,正确的输入读取错误

热门标签

归档