2017-06-21 48 views
-3

這裏是目標:「寫一個C++程序提示用戶輸入一些數字和找到的最小值,最大值,和用於正分別計數輸入的數字的 (號碼)和輸入0應該終止輸入序列並導致結果顯示C++接受0到while循環非法

我的問題是,當我通過www.cpp.sh運行代碼時,它似乎是存儲,我用它來結束序列的最大值或最小值變量(posmax和negmax或posmin和negmin)。我while循環的條件number_entered 0!= 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

更奇怪的是,0被存儲爲最小值或負值僅似乎發生在輸入的最後一個變量序列中。

相關代碼:

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

如果你的末尾,則'CIN >> number_entered進入'0';'打算將其存儲到'number_entered'。 – NathanOliver

+2

然後你可能想刪除它。 – NathanOliver

回答

-2

因爲你的初始值是零。沒有辦法,你會輸入數字低於(正數)和高於(負數)比零。

+0

它們在循環開始之前由額外的輸入語句無條件地賦值。 – Peter

0

我想通了最後,但也許我不好貼了答案,這就是爲什麼我沒有得到我所需要的答案。爲了獲得最大,這不是0負值

,我只是需要將我的|| (negmax == 0)條件轉換爲正確的if語句(它位於最小分支上)。

有與節目沒有其他問題。