-1
I need to sum the odd numbers between two values. In online compilers the code works as predicted, adding 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 result comes out something around 32655, as if it had reached the limit of the int
, that I checked on the internet and is not even close to -5 the negative limit of the 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++.
I ended up discovering this by chance, but thank you so much for sharing the knowledge.
– David Ferrari