2015-11-06 71 views
1

我在這個程序上工作了一段時間,我找不到一個方法來使cin.fail()輸出「錯誤的輸入」。在第二個二進制數的第二,第三,第四,......位。例如,「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; 
} 

回答

0

輸入處理終止於遇到的第一個非數字字符。

一旦你得到的值來解析,用一個函數來驗證它:

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

--Typed了我的頭頂部;錯別字可能已經發生。

+0

您可以指定將新功能添加到編程功能的位置,並且我只是根據需要編輯此代碼。 – Fallenone

+0

所有功能都是獨立的東西。如果你不確定如何放置一個函數,你可能需要閱讀一些基本的C++。然後,在'main()'中,你需要做的就是使用你喜歡的任何方法獲得一個字符串,然後轉換:'if(!convert_to (s,my_int))抱怨...' –

0

我通過使用字符串和檢查那些不正確的輸入,將字符串轉換爲整數,現在它工作,我修復了我的程序。

#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; 
}