2014-09-10 67 views
1

所以,我有幾個字段填入數字。如果我嘗試用字母(例如'noway'或'gg1337')填寫輸入 - 它會生成錯誤並要求輸入有效的數字(不包括例如'13'或'1500000'的字母)C++ cin.clear()和cin.ignore(...)問題

但有一個問題,如果我開始用數字填充輸入,然後添加一些字母(例如'12nowshithappens'),則跳轉到下一個輸入字段,認爲它是有效數字,但顯示。錯誤在一個輸入字段

下面是函數代碼:

int appled() 
{ 
    cin >> appleds; 
    while(cin.fail()) 
    {  
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
     cout << "An arror occured!\nPlease, enter a valid number: "; 
     cin >> appleds; 
    } 
    return appleds; 
} 

如果我描述了一些錯誤 - 這裏是我的測試程序的完整代碼:)

// exZerry presents 

#include <iostream> 
#include <conio.h> 

int apples; 
int fruit; 
int oranges; 
int x; 
int appleds; 
int orangeds; 

using std::cout; 
using std::cin; 
using std::endl; 
using std::numeric_limits; 
using std::streamsize; 

char newline = '\n'; 

bool ok;  
bool ok2; 
bool ok3; 

int appled() //Function to receive 'apples' input 
{ 
    cin >> appleds; 
    while(cin.fail()) 
    {  
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
     cout << "An arror occured!\nPlease, enter a valid number: "; 
     cin >> appleds; 
    } 
    return appleds; 
} 
int oranged() //Function to receive 'oranges' input 
{ 
    cin >> orangeds; 
    while(cin.fail()) 
    { 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
     cout << "An arror occured!\nPlease, enter a valid number: ";   
     cin >> orangeds; 
    } 
    return orangeds; 
} 

int main() 
{ 
    ok = ok2 = ok3 = false; //Some testing 
    while(!ok2) //Actual program loop 
    {  
     x = 0; // Dropping program restart. 
     //cout << "-----------------------" << endl; 
     //cout << "DEBUG MODE: " << x << endl; 
     //cout << "-----------------------" << endl; 
     cout << "=====================================================" << endl; 
     cout << "Enter apples: "; 
     apples = appled(); 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(),'\n'); //Now we have apples 
     cout << "Enter oranges: ";  
     apples = oranged(); 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(),'\n'); //And now we have oranges 
     cout << "\n=====================================================" << endl; 
     cout << "Calculating..." << endl; 
     cout << "=====================================================" << endl; 
     fruit = apples + oranges; 
     cout << "In total we have " << fruit << " fruit" << endl; 
     cout << "=====================================================" << endl; 
     cout << newline; 
     //Option to go out or continue the loop 
     //If you enter 1 - resets the program, any other char - exit 
     cout << "Go out? (1 - 'No', Others - 'Yeap'):" << " "; 
     cin >> x; 
     cout << newline; 
     // ok2 = true; 
     if (x == 1) 
     { 
      cout << "Continue the program..." << endl; 
      //cout << "-----------------------" << endl; 
      //cout << "DEBUG MODE: " << x << endl; 
      //cout << "-----------------------" << endl; 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
      ok = false; 
      ok3 = false; 
      continue; 
     } 
     if (x == 0) 
     { 
      cout << "Shutting down the app..." << x << endl; 
      //cout << "-----------------------" << endl; 
      //cout << "DEBUG MODE: " << x << endl; 
      //cout << "-----------------------" << endl; 
      break; 
     } 
     else 
     { 
      cout << "Shutting down the app..." << x << endl; 
      //cout << "-----------------------" << endl; 
      //cout << "DEBUG MODE: " << x << endl; 
      //cout << "-----------------------" << endl; 
      break;  
     } 
    } 
    cout << "Press any key to exit :D" << endl; 
    getch(); 
    return 0; 
} 
+0

'12nowshithappens'基本上被視爲兩個字段,'12'和'nowshithappens'。您還可以輸入多個以空格分隔的數字,如'1 2 3' - 這些將由三個不同的'cin >> some_int_variable'調用愉快地消耗。如果這不是你想要的,使用'getline'將整行讀入'std :: string',然後使用任何你想要的語法解析它。 – 2014-09-10 14:54:21

回答

1

你可以使你自己的方面分析像添加無效的字符序列。就像這樣:

class num_get : public std::num_get<char> 
{ 
public: 
    iter_type do_get(iter_type it, iter_type end, std::ios_base& str, 
         std::ios_base::iostate& err, long& v) const 
    { 
     auto& ctype = std::use_facet<std::ctype<char>>(str.getloc()); 
     it = std::num_get<char>::do_get(it, end, str, err, v); 

     if (it != end && !(err & std::ios_base::failbit) 
         && ctype.is(ctype.alpha, *it)) 
      err |= std::ios_base::failbit; 

     return it; 
    } 
}; 

現在,你可以把它安裝到你的流:

std::locale orig(std::cin.getloc()); 
std::cin.imbue(std::locale(orig, new num_get)); 

while (!(std::cin >> appleds)) { 
    // input was not entirely numeric 
} 

// input was entirely numeric 
0

於是,我就和它的工作,感謝,Batmaaaan:d

添加完整的工作代碼。它在做什麼?簡單:當你想計算一些東西,你顯然不想在你的程序中計算字母時,這可能會幫助你做到這一點,而風格C++或其他。一些基本知識,我猜,對每個人=)

在Visual Studio 2012中做了測試。花了一些時間在一個地方收集代碼的每一個和平。

// exZerry presents 
// Apples & Oranges V2.1 

#include <iostream> 
#include <conio.h> 

int apples; 
int juce; 
int oranges; 
int x; 
int appleds; 
int orangeds; 

using std::cout; 
using std::cin; 
using std::endl; 
using std::numeric_limits; 
using std::streamsize; 
using std::locale; 


char newline = '\n'; 

bool ok;  
bool ok2; 
bool ok3; 

class num_get : public std::num_get<char> 
{ 
public: 
    iter_type do_get(iter_type it, iter_type end, std::ios_base& str, 
         std::ios_base::iostate& err, long& v) const 
    { 
     auto& ctype = std::use_facet<std::ctype<char>>(str.getloc()); 
     it = std::num_get<char>::do_get(it, end, str, err, v); 

     if (it != end && !(err & std::ios_base::failbit) 
         && ctype.is(ctype.alpha, *it)) 
      err |= std::ios_base::failbit; 

     return it; 
    } 
}; 

/* 
int appled() 
{ 
    cin >> appleds; 
    while(cin.fail()) 
    {  
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
     cout << "An arror occured!\nPlease, enter a valid number: "; 
     cin >> appleds; 
    } 
    return appleds; 
} 
int oranged() 
{ 
    cin >> orangeds; 
    while(cin.fail()) 
    { 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
     cout << "An arror occured!\nPlease, enter a valid number: ";   
     cin >> orangeds; 
    } 
    return orangeds; 
} 
*/ 

int main() 
{ 
    cout << "=====================================================" << endl; 
    cout << "Welcome to exZerry's 'Apples & Oranges V2'" << endl; 
    cout << "=====================================================" << endl; 
    cout << newline; 
    cout << newline; 

    ok = ok2 = ok3 = false; 
    while(!ok2) 
    {  
     x = 0; 
     //cout << "-----------------------" << endl; 
     //cout << "DEBUG MODE: " << x << endl; 
     //cout << "-----------------------" << endl; 
     cout << "=====================================================" << endl; 
     cout << "Enter apples: "; 
     // apples = appled(); 
     locale orig(cin.getloc()); 
     cin.imbue(locale(orig, new num_get)); 
     while (!(cin >> apples)) { 
      cout << "An arror occured!\nPlease, enter a valid number: "; 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
     } 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
     cout << "Enter oranges: ";  
     // oranges = oranged(); 
     //std::locale orig(std::cin.getloc()); 
     cin.imbue(locale(orig, new num_get)); 
     while (!(cin >> oranges)) { 
      cout << "An arror occured!\nPlease, enter a valid number: "; 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
     } 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
     cout << "\n=====================================================" << endl; 
     cout << "Calculating..." << endl; 
     cout << "=====================================================" << endl; 
     juce = apples + oranges; 
     cout << "In total we have " << juce << " fruit" << endl; 
     cout << "=====================================================" << endl; 
     cout << newline; 
     cout << "Go out? (1 - 'No', Others - 'Yeap'):" << " "; 
     cin >> x; 
     cout << newline; 
     // ok2 = true; 
     if (x == 1) 
     { 
      cout << "Continue the program..." << endl; 
      //cout << "-----------------------" << endl; 
      //cout << "DEBUG MODE: " << x << endl; 
      //cout << "-----------------------" << endl; 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
      ok = false; 
      ok3 = false; 
      continue; 
     } 
     if (x == 0) 
     { 
      cout << "Shutting down the app..." << x << endl; 
      //cout << "-----------------------" << endl; 
      //cout << "DEBUG MODE: " << x << endl; 
      //cout << "-----------------------" << endl; 
      break; 
     } 
     else 
     { 
      cout << "Shutting down the app..." << x << endl; 
      //cout << "-----------------------" << endl; 
      //cout << "DEBUG MODE: " << x << endl; 
      //cout << "-----------------------" << endl; 
      break;  
     } 
    } 
    cout << "Press any key to exit :D" << endl; 
    getch(); 
    return 0; 
}