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
Here's a hint: the second "int num1" is different from the first "int num1" :)
You're redeclaring the ints in your line of computation. Instead of
write
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.