I do not seem to figure out what is wrong

using namespace std;
int main()
int first, last, sum=0;
cout<<"Enter first and last value: ";
if(first %2== 0) first++;
for( int num=first; num<=last; num+2)
{sum = sum+num;}
cout<< "The sum of the numbers from "<<first<<"thru"<<last<<" ="<<sum<<endl;
when i run the program the only thing i get is "enter first and last value".
What happens when you actually enter the first and last value?
Please use code tags

this won't even compile
for( int num=first; num<=last; num+2)
should be
for( int num=first; num<=last; num = num+2)
and you are missing a ; on this line system("pause")
Last edited on
@Yanso it will compile perfectly fine - num+2 is a statement, though it does nothing, and it is acceptable there.
no it won't I tried. I think maybe my post was a little misleading. I meant the code in general won't compile, not specifically the for loop. The missing ; won't allow it to compile.
Last edited on
closed account (3CXz8vqX)
Yanson...it compiled just fine for me.

But we do have an infinite loop >.>

The sum of 3 thru 5 is not 8.... 3 + 4 + 5 = 12. According to my interpretation.
Topic archived. No new replies allowed.