开发者

C++ programming help [duplicate]

This question already has answers here: Closed 11 years ago.

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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜