2017-06-21 14 views
-3

目的は次のとおりです: "ある数字を入力するようにユーザに促し、入力した数字の最小値、最大値、およびカウント値を別々に求めます。 を正しい形式で出力します0を入力すると入力シーケンスが終了し、結果が表示されますC++はwhileループに違反して0を受け入れる

私の問題は、www.cpp.shでコードを実行すると、それは私が最大または最小変数(posmaxとnegmaxまたはposminとnegmin)にシーケンスを終了するために使用する0を格納しているようだ。私のwhileループの条件はnumber_entered!= 0なので、0に入るべきではないループ... if the first 3 in the sequence are negative and the last 3 are positive;if the first 3 in the sequence are positive and the last 3 are negative

さらに、見知らぬ人は、入力された変数の最後のシーケンスには、最小値またはマイナス値として格納されているように見えます。

関連するコード:

int main() 
{ 

double number_entered, posmax, posmin, negmax, negmin; 
int positive_count, negative_count; 
positive_count = 0; 
negative_count = 0; 
posmax = 0; 
posmin = 0; 
negmax = 0; 
negmin = 0; 

//before it goes into a loop it will do the following: 
cout << "Entering 0 will terminate the sequence of values.\n" << endl; 
cout << "Enter a number: ";         
cin >> number_entered; //stores input to number_entered 

if (number_entered > 0) //if positive 
{ 
    posmax = number_entered; //needs to be initialized before use in loop 
    posmin = number_entered; //needs to be initialized before use in loop 
} 
else if (number_entered < 0) //if negative 
{ 
    negmax = number_entered; //needs to be initialized before use in loop 
    negmin = number_entered; //needs to be intiialized before use in loop 
} 

while (number_entered !=0) //will keep looping as long as the while condition is true 
{ 
    if (number_entered > 0) //branch if number_entered is positive 
    { 
     if (number_entered > posmax) //sub-branch to compare to get max 
    { 
     posmax = number_entered; //if number is larger than the current max, it gets stored as the new max 
    } 
    else if ((number_entered < posmin)||(posmin == 0)) //sub-branch to compare to get min; since posmin is initialized to 0 it needs to be updated 
    { 
     posmin = number_entered; //if number is less than min than it gets stored as the new min 
    } 
    positive_count++; //under main if branch for if the number is positive, add to positive_count 
} 
else if (number_entered < 0) //branch for if number_entered is negative 
{ 
    if (number_entered > negmax) //sub-branch if number_entered is more than the max 
    { 
     negmax = number_entered; //it then gets stored as the new negmax 
    } 
    else if ((number_entered < negmin)||(negmin == 0)) //sub-branch if number_entered is less than min; since negmin is initialized to 0 it needs to be updated 
    { 
     negmin = number_entered; //it then gets stored as the new negmin 
    } 
    negative_count++; 
} 
cout << "Enter a number: "; //prompts for input again after it is done counting, and comparing to store a max and min 
cin >> number_entered; 
} //end of while loop 

if (number_entered == 0) 
{ 
    cout << endl; 
    if ((negative_count > 0) && (positive_count > 0)) //for situations where it received both positive and negative values 
    { 
    cout << "There were " << negative_count << " negative values entered, with minimum "<< negmin << " and maximum " << negmax << endl << endl; 
    cout << "There were " << positive_count << " positive values entered, with minimum "<< posmin << " and maximum " << posmax << endl<< endl; 
} 
else if (negative_count > 0 && positive_count == 0) //for sitautions where only negative input was received 
{ 
    cout << "There were " << negative_count << " negative values entered, with minimum "<< negmin << " and maximum " << negmax << endl << endl; 
    cout << "No positive numbers were entered" << endl; 
} 
else if (positive_count > 0 && negative_count == 0) //for situations where only positive input was received 
{ 
    cout << "There were " << positive_count << " positive values entered, with minimum "<< posmin << " and maximum " << posmax << endl<< endl; 
    cout << "No negative numbers were entered" << endl; 
} 
else if (negative_count == 0 && positive_count == 0) //for if only 0 was received 
{ 
cout << "No positive numbers were entered.\n" 
     << endl 
     << "No negative numbers were entered.\n" 
     << endl; 
} //end of nested branching if-else if statement 
} //end of if statement 
return 0; 
} 
+1

最後に '0'を入力すると、' cin >> number_entered; 'は' number_entered'にそれを格納します。 – NathanOliver

+2

これを削除してください。 – NathanOliver

答えて

-2

あなたの初期値はゼロであるため。ゼロよりも低い数値(ポジティブ)と高い数値(ネガティブ)を入力する方法はありません。

+0

ループが始まる前に、余分な入力ステートメントによって無条件に割り当てられます。 – Peter

0

私は最終的にそれを理解しましたが、多分回答をひどく投稿したので、必要な答えが得られなかったのです。

負の値で0でなかった最大値を得るために、私は単に私の|| (negmax == 0)の条件を正しいif文(最小の分岐にあった)に置き換えます。

このプログラムには他の問題はありませんでした。

関連する問題