c ++非法接受0进入while循环

时间:2017-06-21 17:55:02

标签: c++ while-loop

目标是:“编写一个c ++程序,提示用户输入一些数字并找出最小值,最大值和数量 (数字)分别为正数和负数输入的数字。然后打印出这些信息 格式正确。输入0应该终止输入序列并导致显示结果。

我的问题是,当我通过www.cpp.sh运行代码时,它似乎存储了我用来结束序列的0到最大或最小变量(posmax和negmax或posmin和negmin)。我的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

奇怪的是,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;
}

2 个答案:

答案 0 :(得分:0)

我终于明白了,但也许我的答案很糟糕,这就是为什么我没有得到我需要的答案。

为了获得负值的最大值,我只需要移动我的|| (negmax == 0)条件到正确的if语句(它在最小分支上)。

该计划没有其他问题。

答案 1 :(得分:-2)

因为您的初始值为零。您无法输入数字低于(对于正数)和更高(对于负数)而不是零。