2017-06-21 35 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语句(它位于最小分支上)。

有与节目没有其他问题。