我需要檢查每個分數值,它們需要在0和100之間,如果不是,需要提示用戶重新輸入一個有效值。
我的代碼:
#include <iostream>
使用 命名空間 std.com.cn>。
void sort(int*, int);
void displaySort(int*, int);
int main()
{
int lInput;
cout << "輸入你的串列的大小。"。
cin >> lInput。
int* lPtr = new int【lInput】。
for (int i = 0; i < lInput; i )
{
cout << "輸入一個分數:"。
cin >> *(lPtr i)。
if (*(lPtr i) < 0 || *(lPtr i) > 100)
{
cout << "無效的輸入,重新輸入。"。
}
}
cout << endl;
sort(lPtr, lInput)。
displaySort(lPtr, lInput)。
cout << endl;
delete[] lPtr;
system("PAUSE")。
return 0;
}
void sort(int* array, int size)
{
int scan, minIndex, minValue;
for (int scan = 0; scan < (size - 1); scan )
{
minIndex = scan;
minValue = *(陣列 掃描)。
for (int i = scan 1; i < size; i )
{
if (*(array i) < minValue)
{
minValue = *(array i);
minIndex = i;
}
}
*(array minIndex)= *(array scan)。
*(array scan) = minValue;
}
}
void displaySort(int* array, int size)
{
cout << "按升序排列的分數串列:" << endl;
for (int i = 0; i < size; i )
{
cout << *(array i) << " "/span>;
}
我現在的方式,它仍然采取無效的數字。我希望如果輸入了一個無效的數字,它就不會接受這個數字,而是要求輸入一個有效數字。
uj5u.com熱心網友回復:
在下面的代碼中
for (int i = 0; i < lInput; i )
{
cout << "輸入一個分數:"。
cin >> *(lPtr i)。
if (*(lPtr i) < 0 || *(lPtr i) > 100)
{
cout << "無效的輸入,重新輸入。"。
}
}
問題是,在無效輸入時,你輸出包含錯誤資訊的文本,并提示用戶輸入一個新的數字,但實際上你并沒有讀取任何新的輸入。相反,你只是跳轉到下一個回圈迭代,這實際上意味著你接受了錯誤輸入。
解決該問題的一種方法是建立一個無限回圈,它將繼續運行,直到用戶輸入有效的輸入。當這種情況發生時,你可以使用 break 陳述句跳出該回圈。
for (int i = 0; i < lInput; i )
{
for (;;) //無限回圈,等同于while(1)。
{
cout << "輸入一個分數:"。
cin>> *(lPtr i)。
if (*(lPtr i) < 0 || *(lPtr i) > 100)
{
cout << "輸入必須在0到100之間,再試一次!
"。
繼續。
}
break;
}
然而,這段代碼的一個問題是,它只執行了一個范圍檢查,但根本沒有檢查輸入是否有效。特別是,它沒有檢查流提取運算子>>是否成功地將用戶的輸入轉換為一個數字。這可以通過呼叫cin.fail()來檢查。
最好是在范圍檢查之前執行這個額外的檢查,就像這樣:
for (int i = 0; i < lInput; i )
{
//這個回圈將繼續下去,直到輸入有效。
for (;;) //無限回圈,等同于while(1)。
{
cout << "輸入一個分數:"。
cin >> *(lPtr i)。
//檢查是否發生流錯誤。
if ( cin.fail( ) )
{
//檢查錯誤是否可以恢復。
if ( cin.bad( ))
{
throw std::runtime_error( "unrecoverable I/O error"/span> )。
}
//print error message; }
cout << "Input must be a number, try again!
"。
//discard bad input ( remainder of line)
cin.ignore( std::numeric_limits<std::streamsize>::max( ), '
' )。
//清除流狀態標志。
cin.clear()。
continue。
}
if (*(lPtr i) < 0 || *(lPtr i) > 100)
{
cout << "輸入必須在0到100之間,再試一次!
"。
繼續。
}
//input is valid, so break out of the infinite loop.
break;
}
注意,上面的代碼需要你另外#include <limit>。
然而,這段代碼仍然不是很完美。如果你輸入諸如12sdlhfh這樣的輸入,那么它將接受12作為有效的輸入,但是接下來的流提取將失敗,因為sdlhfh不是一個有效的數字,并且它將列印一個錯誤資訊。這個錯誤資訊可以通過在每次流提取后丟棄該行的剩余部分來防止,但在這種情況下,這可能不是理想的解決方案,因為你可能想拒絕諸如12sdlhfh的輸入。
為了能夠拒絕這樣的輸入,你不應該使用流提取運算子>>,因為它一遇到非數字就會停止讀取。相反,你應該總是一次讀一行,使用std::getline,并使用std::stoi和一些額外代碼來驗證數字后面是否出現非空格字符。
for (int i = 0; i < lInput; i )
{
//這個回圈將繼續下去,直到輸入有效。
for (;;) //無限回圈,等同于while(1)。
{
std::string line;
std::size_t pos;
cout << "輸入一個分數:"。
getline( cin, line );
//check if stream error occurred; getline( line ); //check if stream error occurred
if ( cin.fail( ) )
{
//檢查錯誤是否可以恢復。
if ( cin.bad( ))
{
throw std::runtime_error( "unrecoverable I/O error"/span> )。
}
//print error message; }
cout << "輸入錯誤,重試!
"。
//清除流狀態標志。
cin.clear()。
continue。
}
//嘗試執行實際的轉換。
try
{
*(lPtr i) = std::stoi( line, &pos ) 。
}
catch ( std::invalid_argument )
{
cout << "無法將輸入轉換為數字,再試一次!
"。
繼續。
}
catch ( std::out_of_range )
{
cout << "range error, try again!
"。
continue;
}
//verify that rest of line does not contain any non-whitespace characters.
for ( ; pos < line.length( ); pos )
{
if ( ! std::isspace( static_cast<unsigned char> ( line[pos]) ) )
{
cout << "發現無效字符,重試!
"。
//我們不能在這里使用 "continue",因為那會。
//繼續進行最里面的下一次迭代。
//loop,但我們想繼續到下一個迭代。
//the outer loop。
goto continue_outer_loop;
}
}
if (*(lPtr i) < 0 || *(lPtr i) > 100)
{
cout << "輸入必須在0到100之間,再試一次!
"。
繼續。
}
//input is valid, so break out of the infinite loop.
break。
continue_outer_loop。
continue。
}
注意,上面的代碼還需要。#include <string>和#include <ctype>。
上面的代碼使用了一個goto陳述句。通常情況下,如果可能的話,你不應該使用goto,但對于退出嵌套回圈來說,它是可以接受的。
同時,如果你的代碼中使用了goto,那么你就應該使用goto。
還要注意的是,如果你使用上述代碼,那么它將與在回圈外使用cin >> lInput;不兼容。混合使用std::getline和std::istream::operator>>通常不會成功,因為std::istream::operator>>將在緩沖區中留下換行,所以下一次呼叫std::getline可能只會檢索到一個空行。
uj5u.com熱心網友回復:
試試這個--
int main()
{
int lInput;
cout << "輸入你的串列的大小。"。
cin >> lInput。
int* lPtr = new int【lInput】。
for (int i = 0; i < lInput; i )
{
cout << "輸入一個分數:"。
cin >> *(lPtr i)。
while (*(lPtr i) < 0 || *(lPtr i) > 100)
{
cout << "無效的輸入,重新輸入。"。
cin >> *(lPtr i)。
}
}
cout <<endl。
sort(lPtr, lInput)。
displaySort(lPtr, lInput)。
cout << endl;
delete[] lPtr;
system("PAUSE")。
return 0;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/332771.html
標籤:
