C++ programming help [duplicate]

2019-08-28 20:45发布

Possible Duplicate:
C++ Programming help

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

2条回答
Emotional °昔
2楼-- · 2019-08-28 21:19

Here's a hint: the second "int num1" is different from the first "int num1" :)

查看更多
贪生不怕死
3楼-- · 2019-08-28 21:38

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

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

write

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

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.

查看更多
登录 后发表回答