简体   繁体   English

如果else语句混淆了c ++

[英]if else statement confused c++

#include <iostream>
using namespace std;

int main()
{
    int positiveInteger;

    cout << "Please input an integer up to 100." << endl;

    cin >> positiveInteger;

    int result = 0;
    for (int i = 0; i <= positiveInteger; i++)
    {
        if ( positiveInteger >= 0 )
        {
          result += i;
        }
        else 
        {
          cout << "Please input a positive integer." << endl;
        }
    }

    cout << result;
    return 0;
}

Above I have a for loop with an if else statement in the center. 上面我有一个for循环,中间是if else语句。 I am confused because I want it to be so when I enter a integer that is not negative it will loop the if result. 我很困惑,因为我希望这样,所以当我输入一个不为负的整数时,它将循环if结果。 But I want it to be so if I put in a negative number it says please input a positive integer. 但是我希望是这样,如果我输入一个负数,它表示请输入一个正整数。 That's why I set it so in the if statement only numbers above and = to 0 would return the result, but if I enter a negative number I just get 0 I want it to say "Please input a positive integer". 这就是为什么我在if语句中设置它的原因,只有上面的数字= = 0会返回结果,但是如果我输入一个负数我只会得到0,我想让它说“请输入一个正整数”。 I don't understand what I'm doing wrong. 我不明白我在做什么错。 Isn't the if statement if true pulls the if and if its not true pulls the else? if语句if true如果不是拉if,如果不是true则拉else? Or am I missing something? 还是我错过了什么?

If you enter a negative number, you never get to enter the loop. 如果输入负数,则永远不会进入循环。 An approach that fits better to such cases is: 更适合此类情况的方法是:

do
{
    input a number;
    if( positive )
         do something;
} while( not positive );

If I got it right, you want to 1. input a number; 如果我没看错,您想1.输入一个数字; 2.1 If the number is positive you loop through it. 2.1如果数字为正,则循环遍历。 2.2 If it's negative you show the error message. 2.2如果为负,则显示错误消息。

The problem is your loop, where the conditional is, checks if i (which is zero) is smaller then the number. 问题是您的循环(有条件的地方)检查i(零)是否小于数字。 However if you input a negative number i will be bigger then positiveInteger and you won't loop through the if. 但是,如果您输入一个负数,我会更大,然后是positiveInteger,您将不会遍历if。 I fixed your code 我修复了您的代码

using namespace std;

int main()
{
    int positiveInteger;

    cout << "Please input an integer up to 100." << endl;

    cin >> positiveInteger;

    int result = 0;
    if ( positiveInteger >= 0 ){
       for (int i = 0; i <= positiveInteger; i++)
       {
           result += i;
       }
       cout << result;
    }
    else {
        cout << "Please input a positive integer." << endl;
    }

    return 0;

}

I think your code would benefit from some rearrangement. 我认为您的代码将从某些重新安排中受益。 I'd structure it something like this: 我会像这样构造它:

do { 
    cout << "please enter a number between 1 and 100";
    number = get_number():
} while (number < 1 || number > 100);

cout << sum_series(1, number);

The output you are getting is because your loop fails at non-negative numbers. 您得到的输出是因为循环在非负数时失败。 You should try the following: 您应该尝试以下方法:

int main()
{

int positiveInteger;

cout << "Please input an integer upto 100." << endl;

cin >> positiveInteger;

int result = 0;
if ( positiveInteger < 0 )
{

    cout << "Please input a positive integer." << endl;

}
else{


   for (int i = 0; i <= positiveInteger; i++)
   {

    result += i;

   }

 cout << result;

 }
return 0;

}

From my understanding, if the number is negative, you wish to take another input. 据我了解,如果数字为负,您希望再输入一次。 But the cin statement is not under loop control to do it so. 但是cin语句不受循环控制。 It just executes only one time irrespective of the nature of the number. 无论号码的性质如何,它仅执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM