您好,我對運算子多載比較陌生,但我確實相信它可以解決我在制作的幾乎每個程式中都面臨的問題。我的目標是多載std::cin >> int_var,使其只能接受整數輸入,如果它檢測到輸入了除 int 之外的輸入,它會將用戶置于回圈中,直到輸入為 int。
到目前為止,這就是我帶給你的
#include <iostream>
std::istream& operator>> (std::istream& Cin, int *var)
{
int var2;
if(!Cin >> var2)
{
std::cout << "Invalid input!";
}
return Cin;
}
int main() {
int var;
std::cin >> var;
std::cout << var;
}
注意:此代碼片段只是為了看看我是否可以檢測到無效輸入。它還不需要它來包含回圈。
uj5u.com熱心網友回復:
獲取用戶輸入的正確方法是:
- 讀取一個字串
- 嘗試將字串轉換為 int (或其他)
- 如果轉換失敗,您應該:
- 詢問并再試一次,只有 IFF 用戶被認為是人類
- 終止(要求您的用戶提供有效的輸入)
這是因為用戶總是在每次輸入提示后按 Enter。
這個問題的變化比比皆是。這是我的最后一個答案,并附有一個作業示例。
uj5u.com熱心網友回復:
我不知道你能不能。您需要做的是驗證所有用戶輸入。無論應用程式是什么,都永遠不會信任用戶。
所以你只需要檢查你得到的值是否是一個整數,然后你可以呼叫你的函式,如果它不是一個整數,然后再次詢問輸入并告訴他們為什么它是錯誤的,比如“輸入只接受整數”。
uj5u.com熱心網友回復:
我不建議您嘗試多載流提取運算子>>以顯著改變其行為。創建自己的函式會更有意義。
流提取將接受用戶輸入,例如"6sdfj23jlj"數字的有效輸入6。如果您想拒絕此類輸入并在這種情況下重新提示用戶輸入新輸入,那么使用面向行的輸入(例如std::getline,而不是流提取運算子 )可能更有意義>>。
用 讀入字串后std::getline,您可以嘗試使用 函式將其轉換為整數std::stoi。
在下面的程式中,我創建了一個函式get_int_from_user,它會反復詢問用戶輸入,直到輸入有效。
#include <iostream>
#include <string>
int get_int_from_user( const std::string& prompt )
{
std::string line;
std::size_t pos;
int i;
//repeat forever, until an explicit return statement or an
//exception is thrown
for (;;) //equivalent to while(1)
{
//prompt user for input
std::cout << prompt;
//attempt to read one line of input from user
if ( !std::getline( std::cin, line ) )
{
throw std::runtime_error( "unexpected input error!\n" );
}
//attempt to convert string to integer
try
{
i = std::stoi( line, &pos );
}
catch ( std::invalid_argument& )
{
std::cout << "Unable to convert input to number, try again!\n";
continue;
}
catch ( std::out_of_range& )
{
std::cout << "Out of range error, try again!\n";
continue;
}
//The remainder of the line is only allowed to contain
//whitespace characters. The presence of any other
//characters should cause the entire input to get rejected.
//That way, input such as "6sdfj23jlj" will get rejected,
//but input with trailing whitespace will still be accepted
//(just like input with leading whitespace is accepted by
//the function std::stoi).
for ( ; pos < line.length(); pos )
{
if ( !std::isspace( static_cast<unsigned char>(line[pos]) ) )
{
std::cout << "Invalid character found, try again!\n";
//we cannot use "continue" here, because that would
//continue to the next iteration of the innermost
//loop, but we want to continue to the next iteration
//of the outer loop
goto continue_outer_loop;
}
}
//input is valid
return i;
continue_outer_loop:
continue;
}
}
int main()
{
try
{
int i = get_int_from_user( "Please enter a number: " );
std::cout << "Input was ok, you entered: " << i << '\n';
}
catch ( std::runtime_error& e )
{
std::cout << "Runtime error:" << e.what() << '\n';
}
}
該程式具有以下行為:
Please enter a number: Test
Unable to convert input to number, try again!
Please enter a number: 3000000000
Out of range error, try again!
Please enter a number: 6sdfj23jlj
Invalid character found, try again!
Please enter a number: 633245183
Input was ok, you entered: 633245183
上面的代碼使用一個goto陳述句。goto通常,如果可能,您不應該使用,但為了打破嵌套回圈,它被認為是合適的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/419327.html
標籤:
