当cin不是一个int时,C ++程序继续循环 [英] C++ program keeps looping when cin is not an int

查看:97
本文介绍了当cin不是一个int时,C ++程序继续循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++做一个简单的猜测数字游戏。
我的程序检查用户输入是否是一个整数。
但是当我输入例如abc时,程序保持说:输入一个数字!

 

code> #include< iostream>
#include< cstdlib>
#include< ctime>
using namespace std;

int chances = 3;
void ask();
void checkAnswer(int ans);
void defineNumber();
int correctAnswer;

void defineNumber(){
srand(time(0));
correctAnswer = rand()%11;
}

void checkAnswer(int ans){
if(ans == correctAnswer){
cout< 答案是对的!\\\
< endl;
exit(0);
} else {
if(chances> 0){
cout< 错误的答案,再试一次!\\\
< endl;
chances--;
ask();
} else {
cout<< 你输了! << endl;
exit(0);
}
}
}

void ask(){
int input;
cout<< correctAnswer<< endl;
try {
cin>>输入;
if(input> 11 || input< 0){
if(!cin){
cout< 输入数字! << endl; // HERE LIES THE PROBLEM
cin.clear(); // I TRIED THIS BID DID NOT WORK as WELL
ask();
} else {
cout<< 10岁以下你白痴! << endl;
ask();
}
} else {
checkAnswer(input);
}
} catch(exception e){
cout<< 一个意料之外的问题发生了! << endl;
ask();
}
}

int main(){
cout< 欢迎猜猜数字! << endl;
cout<< 猜数10以下;
defineNumber();
ask();
}

提前感谢。

解决方案

尝试:

 试试{
cin> ;输入;
if(cin.good()){
if(input> 11 || input< 0){
cout< 10岁以下你白痴! << endl;
ask();
} else {
checkAnswer(input);
}

} else {
cout<< 输入数字! << endl;
cin.clear();
cin.ignore(std :: numeric_limits< std :: streamsize> :: max(),'\\\
');
ask();
}

} catch(exception e){
cout<< 一个意料之外的问题发生了! << endl;
ask();
}

并且不要忘记在开头使用: #include< climits>



cin.ignore(std :: numeric_limits< std :: streamsize> ; :: max(),'\\\
');
行将忽略一切,直到下一个int数字..因此它不会再循环..


I am making a simple guess the number game using C++. My program checks if the user input is an integer or not. But when I input for example "abc" the program keeps saying: "Input a number!" instead of saying it once and let the user input something again..

Code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int chances = 3;
void ask();
void checkAnswer(int ans);
void defineNumber();
int correctAnswer;

void defineNumber(){
    srand(time(0));
    correctAnswer = rand()%11;
}

void checkAnswer(int ans){
    if(ans == correctAnswer){
        cout << "The answer was right!\n" << endl;
        exit(0);
    }else{
        if(chances > 0){
            cout << "Wrong answer, try again!\n" << endl;
            chances--;
            ask();
        }else{
            cout << "You lost!" << endl;
            exit(0);
        }
    }
}

void ask(){
    int input;
    cout << correctAnswer << endl;
    try{
        cin >> input;
        if(input > 11 || input < 0){
            if(!cin){
                cout << "Input a number!" << endl; //HERE LIES THE PROBLEM
                cin.clear(); //I TRIED THIS BUT DIDN'T WORK AS WELL
                ask();
            }else{
                cout << "Under 10 you idiot!" << endl;
                ask();
            }
        }else{
            checkAnswer(input);
        }
    }catch(exception e){
        cout << "An unexpected error occurred!" << endl;
        ask();
    }
}

int main(){
    cout << "Welcome to guess the number!" << endl;
    cout << "Guess the number under 10: ";
    defineNumber();
    ask();
}

Thanks in advance.

解决方案

Try this:

try{
    cin >> input;
    if (cin.good()) {
      if(input > 11 || input < 0) {
        cout << "Under 10 you idiot!" << endl;
        ask();
      } else {
        checkAnswer(input);
      }

    } else {
      cout << "Input a number!" << endl;
      cin.clear();
      cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      ask();
      }

}catch(exception e){
    cout << "An unexpected error occurred!" << endl;
    ask();
}

and don't forget to use this at the beginning: #include <climits>

The cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); line will ignore everything until the next int number.. Therefore it will not loop anymore..

这篇关于当cin不是一个int时,C ++程序继续循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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