簡體   English   中英

C ++ find_first_not_of

[英]C++ find_first_not_of

我是一位c ++初學者,對此我感到困惑,我們將不勝感激。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    cout << "CONVERSION\n\n";
    cout << "Base 11 to Decimal\n";
    cout << "Base 11: ";
    getline(std::cin, str);
    const auto bad_loc = str.find_first_not_of("0123456789aA");
    if (bad_loc != std::string::npos) 
    {
        throw "bad input"; // or whatever handling
    }
    unsigned long ul = std::stoul(str, nullptr, 11);
    cout << "Decimal: " << ul << '\n';
    return 0;
}

輸出是

CONVERSION

Base 11 to Decimal
Base 11: 1234AB 

該程序停止了,並且沒有向我發送“錯誤的輸入”。 找不到任何解決方案。 提前致謝

BoBTFish在評論中給出了答案:

好吧,您不會捕獲並處理拋出的字符串。 因此,您的程序將剛剛退出,並且您的操作系統將執行其所有操作,而這可能不包括嘗試打印字符串。 就本測試而言,更換起來可能更簡單

 throw "bad input"; 

 std::cerr << "bad input\\n"; return 1; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM