Do-while不断循环cout,忽略cin [英] Do-while endlessly looping cout, ignores cin

查看:175
本文介绍了Do-while不断循环cout,忽略cin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此程序在指定范围内打印指定数量的数字。然而,当我输入一个字符,它只是无休止地循环,无论do-while循环我在做什么。例如:如果我在输入最大数cin输入一个字符,它只是垃圾邮件输入最大数无休止,它只是跳过任何人都知道为什么?

  #include< iostream> ; 
#include< cmath>
#include< cstdlib>
#include< ctime>

使用命名空间std;
$ b b int roll(int mini,int maxi)
{
int v = maxi - mini;
int x = mini +(rand()%(v + 1));
return x;

}
void caller()
{
int a;
int b;
int c;

do {
cout<<Enter minimum number<< endl;
cin.clear();
cin>> a;
} while(cin.fail());

do {
cout<<<Enter maximum number<< endl;
cin.clear );
cin>> b;
} while(cin.fail()|| a> b);

do {
cout<< 多少卷? << endl;
cin.clear();
cin>> C;
} while(cin.fail());

for(int i = 0; i cout<辊(a,b) endl;
}

int main()
{
srand(time(NULL));
caller();
return 0;
}


解决方案

code> istream :: fail()用于循环控制。请参阅为什么iostream :: eof处于循环条件中会被视为错误?类似的问题。



相反,我依赖 istream :: operator>> 的返回值。



我还使用以下函数重置标志并清除输入流上的输入:

  void flush_stream :: istream& stream)
{
stream.clear();
stream.ignore(std :: numeric_limits< std :: riversize> :: max(),'\\\
');
}

查看如何刷新cin缓冲区?更多信息。



我会编写你的输入检查,因此:

  int get_valid_number(const std :: string& prompt)
{
int number = 0;

bool valid = false;
while(!valid)
{
std :: cout<提示<< std :: endl;
if(std :: cin>> number)
{
valid = true;
}
flush_stream(std :: cin);
}

return number;希望将此提取到函数中的好处是显而易见的。 查看


This program prints a specified amount of numbers within a specified range. However, when I enter a character, it just endlessly loops whichever do-while loop I do it in. E.g: If I enter a character in "Enter maximum number" cin, it just spams "Enter maximum number" endlessly, it just skips the cin and loops the cout (same goes for the other 2 do-whiles. Anyone know why?

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int roll(int mini, int maxi)
{
        int v = maxi - mini;
        int x  = mini + (rand() % (v+1));
        return x;

}
void caller()
{
    int a;
    int b;
    int c;

    do {
    cout << "Enter minimum number" << endl;
    cin.clear();
    cin >> a;
    } while (cin.fail());

    do {
    cout << "Enter maximum number" << endl;
    cin.clear();
    cin >> b;
    } while (cin.fail() || a > b);

    do {
    cout << "How many rolls?" << endl;
    cin.clear();
    cin >> c;
    } while (cin.fail());

    for (int i = 0; i < c; i++)
    cout << roll(a, b) << endl;
}

int main()
{
    srand (time(NULL));
    caller();
    return 0;
}

解决方案

I prefer not to use istream::fail() for loop control. See Why is iostream::eof inside a loop condition considered wrong? for a similar issue.

Instead, I rely upon the return value of istream::operator >>.

I also use the following function to reset the flags and clear the input on input stream:

void flush_stream(std::istream& stream)
{
    stream.clear();
    stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

See How do I flush the cin buffer? for more on this.

So I'd code your input checks thus:

int get_valid_number(const std::string& prompt)
{
    int number = 0;

    bool valid = false;
    while (!valid)
    {
        std::cout << prompt << std::endl;
        if (std::cin >> number)
        {
            valid = true;
        }
        flush_stream(std::cin);
    }

    return number;
}

Hopefully the benefits of extracting this into a function are obvious. See it run.

这篇关于Do-while不断循环cout,忽略cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆