本文介绍了C ++编程帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它无法正常工作,需要显示

its not working properly it needs to display the sum of the even integers betweena nd including two numbers enter by the users!

我错过了它让我疯狂的动作

what am i misssing its driving me crazy

int main(){

// declare variables

    int num1 = 0;
    int num2 = 0;
    int sum= 0;

    cout << "Enter the First Number:" << endl;
    cin >> num1;
    cout << "Enter the Second Number:" << endl;
    cin >> num2;

    int num1 = num1 % 2 == 0 ? num1 : num1+1; int num2 = num2 % 2 == 0 ? num2 : num2-1; for (int i = num1; i <= num2; i += 2) sum += i;

    return 0;
}   // end of main function


推荐答案

你在你的计算行中重新声明int。而不是

You're redeclaring the ints in your line of computation. Instead of

int num1 = num1%2 == 0 ? num1 : num1+1;

num1 = num1%2 ==0 ? num1 : num1+1;

和num2相同。您只能将一个变量声明为一个int(或任何其他类型)一次。后续引用不需要指定类型。

And the same for num2. You can only declare a variable as an int (or any other type) once. Subsequent references do not need to specify the type.

这篇关于C ++编程帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 10:27