输入字符而不是整数时程序中断

问题描述 投票:0回答:1

我是C ++的新手,发现很难找到有关cin功能的信息。这是我的第一个程序,因此非常简单。它确定输入的数字是质数还是复合数,同时过滤掉诸如负数和小数之类的错误值。该程序还将在10个失败的条目后超时。

我要添加到该程序中的最后一个功能是能够过滤字符输入,但是我对尝试不同的解决方案没有好运。我尝试将字符转换为float / double / int,但无济于事。我仅需要cin方面的帮助,如果用户输入字母“ K”或其他任何无效字符,我希望程序识别出该错误,然后生成“无效值” cout消息+新条目,请尝试x次。

当前输入一个字符时,程序崩溃,并且一次尝试了所有10次尝试,将输出结果留在终端上非常草率。

#include <iostream>
#include <cmath>

using namespace std ;

int main (void)
{
    //Declare variables
    int y, c, i1 ;
    double i ;

     //Ask for user input
     cout << endl << endl << "Please enter a number: " ;
     cin >> i ;

     //Eliminate bad user input values
     i1 = i ;

    //Detect when a decimal number is entered for the first attempt
    for ( c = 1 ; int(i1) != i ; c ++ )
    {
        cout << endl << "This value is invalid. Please retry: " ;
        cin >> i ;

         i1 = i ;

            //Terminate program after so many failed attempts
            if ( 8 < c )
            {
                cout << endl << "This value is invalid. Try again next time! \n\n" ;
                return 0 ;
            }

        //Detect if a '0' is entered on the latter attempts
        for ( c ; i < 1 ; c ++ )
        {
            cout << endl << "This value is invalid. Please retry: " ;
            cin >> i ;

            i1 = i ;

            //Terminate program after so many failed attempts
            if ( 8 < c )
            {
                cout << endl << "This value is invalid. Try again next time! \n\n" ;
                return 0 ;
            }
        }
    }

    //Detect when '0' is entered on the first attempt
    for ( c = 1 ; i < 1 ; c ++ )
    {
        cout << endl << "This value is invalid. Please retry: " ;
        cin >> i ;

         i1 = i ;

            //Terminate program after so many failed attempts
            if ( 8 < c )
            {
                cout << endl << "This value is invalid. Try again next time! \n\n" ;
                return 0 ;
            }

         //Detect if a decimal number is entered on the latter attempts
         for ( c ; int(i1) != i ; c ++ )

        {
            cout << endl << "This value is invalid. Please retry: " ;
            cin >> i ;

             i1 = i ;


            //Terminate program after so many failed attempts
            if ( 8 < c )
            {
                cout << endl << "This value is invalid. Try again next time! \n\n" ;
                return 0 ;
            }
        }
    }


     //Find prime numbers
    for ( y = 1 ; y <= i ; y ++ )
    {
        //Give instant result if user input is no. 1
        if ( i == 1 )
        {
            cout << endl << "The number: " << i << " is a composite number.\n\n" ;
            break ;
        }

        //Eliminate no. 1 from prime number search
        if ( y == 1 )
            continue ;

        //Search for a whole number division using modulus
        if ( fmod(i, y) == 0 )
        {
            if ( i == y )
                cout << endl << "The number: " << i << " is a prime number.\n\n" ;

            else if ( i != y )
            {   
                cout << endl << "The number: " << i << " is a composite number.\n\n" ;
                break ;
            }
        }   
    }   

    return 0 ;
}
c++ character exit cin
1个回答
0
投票

我终于找到了答案。我只需要使用此循环。尽管在x次失败尝试后超时仍然有很多需要改进的地方。我将不得不将此循环嵌套到其他代码中。

#include <limits>
     //Detect character input and refuse
     while( 1 == 1 )
    {
        if ( cin.fail() )
        {
            cin.clear () ;
            cin.ignore (numeric_limits <streamsize> :: max(), '\n' ) ;
            cout << endl << "This value is invalid. Please retry: " ;
            cin >> i ;
        }

        if ( !cin.fail() )
            break ;
    }
© www.soinside.com 2019 - 2024. All rights reserved.