[英]Input validation for reading in vector of ints
我正在從用戶讀取一個整數向量,並且嘗試進行一些輸入驗證,以便如果用戶輸入字母,則將其更改為0。這就是我所擁有的。
for(int i=0; i < columns;i++)
{
int temp3;
cin >> temp3;
if (temp3 > 100 or temp3 < 0)
temp3 = 0;
if (isalpha(temp3)) //run-time error here
temp3 = 0;
newstu.push_back(temp3);
}
注釋行是問題所在,但由於它是運行時錯誤。 我不確定為什么這是錯誤的/不起作用。 有任何想法嗎? 提前致謝
如果您有權使用增強功能,則還可以嘗試使用“嘗試/捕獲”設計。
您在此處有一個示例: https : //www.boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast/examples.html
try
{
args.push_back(lexical_cast<int>(temp3));
}
catch(const bad_lexical_cast &)
{
args.push_back(0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.