I need to sum the odd numbers between 2 values. In online compilers the code works as expected, adding up any odd input values and giving the expected output, but in my terminal (4.15.0-34-generic) when putting, for example, 6 and -5, the output comes out something in around 32655, as if it had reached the limit of int
, which I checked on the internet and is not even close to -5 the negative limit of int
.
#include <iostream>
using namespace std;
int main() {
int x,y,r;
cin>>x>>y;
if(x<y) {
for (x; x<y ; x++){
if(x%2 == 0) {
continue;
}
else {
r += x;
}
}
}
else {
for (y; y<x ; y++){
if(y%2 == 0) {
continue;
}
else {
r += y;
}
}
}
cout<<r<<endl;
return 0; }
The code is this, it's in C ++.