【问题标题】:How to check for non-integer input in multiple inputs如何检查多个输入中的非整数输入
【发布时间】:2016-02-07 01:01:23
【问题描述】:

我在这个程序上工作了一段时间,但我找不到使 cin.fail() 输出“输入错误”的方法。在 2nd,3rd,4th,... 第二个二进制数的数字。例如,“11111 a11”被检测为输入失败,但未检测到“11111 1a1”或“11111 1abfcds”。它似乎只检查第一个数字。这是程序。

#include <iostream>
#include <cmath>
using namespace std;
int binary_decimal_1(int n);
int binary_decimal_2(int m);
int decimal_binary(int s);
int main()
{
int n, m, s;
cout << "Input 2 binary numbers" << endl;
cin >> n;
if (cin.fail())
{
   cout << "Incorrect input." << endl;
   return 0;
}
cin >> m;
if (cin.fail())
{
   cout << "Incorrect input." << endl;
   return 0;
}
s= binary_decimal_1(n) + binary_decimal_2(m);
cout << "Sum: " << decimal_binary(s) << endl;

return 0;
}
int decimal_binary(int s)  /* Function to convert decimal sum to binary result.*/
{
int rem, i=1, binary=0;
while (s!=0)
{
    rem=s%2;
    s/=2;
    binary+=rem*i;
    i*=10;
}
return binary;
}
int binary_decimal_1(int n) /* Function to convert binary number 1 to decimal.*/
{
int decimal_1=0, i=0, rem;
while (n!=0)
{
    rem = n%10;
    n/=10;
    decimal_1 += rem*pow(2,i);
    ++i;
}
return decimal_1;
}
int binary_decimal_2(int m) /* Function to convert binary  number 2 to decimal.*/
{
int decimal_2=0, i=0, rem;
while (m!=0)
{
    rem = m%10;
    m/=10;
    decimal_2 += rem*pow(2,i);
    ++i;
}
return decimal_2;
}

【问题讨论】:

    标签: c++ input


    【解决方案1】:

    输入处理在遇到第一个非数字字符时终止。

    一旦获得要解析的值,请使用函数对其进行验证:

    template <typename T>
    bool convert_to( const std::string& s, T& value )
    {
      std::istringstream ss( s );
      ss >> value >> std::ws;
      return ss.eof();
    }
    

    --从我的头顶输入;可能有错别字。

    【讨论】:

    • 你能指定把函数放在哪里吗?我刚开始编程,我刚刚根据需要编辑了这段代码。
    • 所有功能都是独立的东西。如果您不确定如何放置函数,您可能需要阅读一些基本的 C++。然后,在main() 中,您只需使用您喜欢的任何方法获取一个字符串,然后转换:if (!convert_to&lt;int&gt;(s, my_int)) complain...
    【解决方案2】:

    我修复了我的程序,使用字符串并检查那些输入是否不正确,将字符串转换为整数,现在它可以工作了。

    #include <iostream>
    #include <cmath>
    #include <sstream>
    using namespace std;
    int binary_decimal_1(int n);
    int binary_decimal_2(int m);
    int decimal_binary(int s);
    
    int main()
    {
    string n, m;
    int s;
    cout << "Input 2 binary numbers:" << endl;
    
    cin >> n;
    cin >> m;
    
    bool bValidn = true; // Function to check for non numeric input in n.
        for (unsigned int nIndex=0; nIndex < n.length(); nIndex++)
            if (!isdigit(n[nIndex]))
            {
                bValidn = false;
                cout << "Bad input.";
    
                return 0;
            }
    bool bValidm = true; // Function to check for non numeric input in m.
        for (unsigned int nIndex=0; nIndex < m.length(); nIndex++)
            if (!isdigit(m[nIndex]))
            {
                bValidm = false;
                cout << "Bad input.";
    
                return 0;
            }
    
    // Now to convert strings into integers.
    
    
    int intn;
    stringstream convertn(n);
    
    if ( !(convertn >> intn) )
    intn = 0;
    
    int intm;
    stringstream convertm(m);
    
    if ( !(convertm >> intm) )
    intm = 0;
    
    // And the hardest part.
    
    s = binary_decimal_1(intn) + binary_decimal_2(intm);
    cout << "Sum is: " << decimal_binary(s) << endl << endl;
    
    return 0;
    }
    
    int decimal_binary(int s)  // Function to convert decimal sum to binary result.
    {
    int rem, i=1, binary=0;
    while (s!=0)
    {
        rem=s%2;
        s/=2;
        binary+=rem*i;
        i*=10;
    }
    return binary;
    }
    int binary_decimal_1(int intn) // Function to convert binary number 1 to decimal.
    {
    int decimal_1=0, i=0, rem;
    while (intn!=0)
    {
        rem = intn%10;
        intn/=10;
        decimal_1 += rem*pow(2,i);
        ++i;
    }
    return decimal_1;
    }
    int binary_decimal_2(int intm) // Function to convert binary  number 2 to decimal.
    {
    int decimal_2=0, i=0, rem;
    while (intm!=0)
    {
        rem = intm%10;
        intm/=10;
        decimal_2 += rem*pow(2,i);
        ++i;
    }
    return decimal_2;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 1970-01-01
      • 2014-03-28
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      相关资源
      最近更新 更多