# Thread: help!!! code needs fixing

1. ## help!!! code needs fixing

Ok so the code makes the user type in a number. Ok if the number is odd it must say "Invalid Input". If the number is even it takes the ODD numbers of it and gives you the sum, but doesn’t display it ( I don't want the sum to be displayed).

Example: if you type in 10 it goes 1 + 3 + 5 + 7 + 9

that gives me 25 which is good the program gives me 25 which is good, but for the program I want the added pairs to show up that add to that number(the input).

For example if I type 8

I want

8 = 1 + 7

8 = 3 + 5

to show up how do I do that?

I get the sum how do I get the added pairs to show up?

Here is the code

Code:
```

#include <iostream>
using namespace std;

int main()

{
int sum = 0;
int num;

cout << " Please enter a positive even interger ";
cin >> num;

if ( num &#37; 2 != 0 )

cout << "Invalid Input" << endl;

else if ( num % 2 == 0 )

{
for ( int i = 1; i <= num; i++ )

{  if ( i % 2 != 0 )

sum += i;

}

if ( ( i == sum ) && ( i != i ) )

cout << 8 << " = " <<  i << " + " <<  i << endl;

cin >> num;
}

return 0;

}```

I have error for some reason?
test.cc:29: error: name lookup of &#226;i&#226; changed for new ISO &#226;for&#226; scoping
test.cc:20: error: using obsolete binding at &#226;i&#226;

2. > for ( int i = 1; i <= num; i++ )
In old C++, the scope of i was the rest of the block.
In new C++, it's just the for loop.

> if ( ( i == sum ) && ( i != i ) )
This is outside the loop, so strictly speaking, i is out of scope.
The error is telling you that you're ASSUMING old-style C++ for the scope of i in the for loop.

What doed i != i do anyway?
Isn't that trivially false?
Doesn't that make the whole if statement trivially false?

3. Originally Posted by Salem
> for ( int i = 1; i <= num; i++ )
In old C++, the scope of i was the rest of the block.
In new C++, it's just the for loop.
What?

> if ( ( i == sum ) && ( i != i ) )
This is outside the loop, so strictly speaking, i is out of scope.
The error is telling you that you're ASSUMING old-style C++ for the scope of i in the for loop.
ok what do I do

What doed i != i do anyway?
Isn't that trivially false?
Doesn't that make the whole if statement trivially false?

i != i is so I don't get the same number twice. and I go 1 + 7 and 3 + 5

4. > i != i is so I don't get the same number twice. and I go 1 + 7 and 3 + 5
It doesn't have a memory of all it's previous values.
If you want that, you need to store them yourself, and then check each one.

In old C++, the declaration of i looks something like this

Code:
```int i;
for ( i = 0 ; i < N ; i++ ) {
}

// i still exists here```
In new C++, the declaration of i looks something like this
Code:
```{
int i;
for ( i = 0 ; i < N ; i++ ) {
}
}

// i DOES NOT exist here```
Note the extra braces, hiding the scope of i from later code.

Solutions
- declare i outside the for loop
- declare another variable which can carry the value of i you want out of the loop.

5. Originally Posted by Salem
> i != i is so I don't get the same number twice. and I go 1 + 7 and 3 + 5
It doesn't have a memory of all it's previous values.
If you want that, you need to store them yourself, and then check each one.

In old C++, the declaration of i looks something like this

Code:
```int i;
for ( i = 0 ; i < N ; i++ ) {
}

// i still exists here```
In new C++, the declaration of i looks something like this
Code:
```{
int i;
for ( i = 0 ; i < N ; i++ ) {
}
}

// i DOES NOT exist here```
Note the extra braces, hiding the scope of i from later code.

Solutions
- declare i outside the for loop
- declare another variable which can carry the value of i you want out of the loop.

This doesn't answer my question at all.

7. You are not a very appreciative person. He gave you a solution to one of your problems, you should be able to use logic to solve to rest.

8. >> I get the sum how do I get the added pairs to show up?
You have to keep track of the numbers that you try for the target sum. When you reach the target sum, you would output all the numbers you kept track of.

>> This doesn't answer my question at all.
Salem is offering you important advice though. i != i is trivially false, like he said, *and* you use i in places beside the for loop... so you might want to take a look at his post again.

Is this supposed to work with other numbers than eight?

9. Originally Posted by citizen
>> I get the sum how do I get the added pairs to show up?
You have to keep track of the numbers that you try for the target sum. When you reach the target sum, you would output all the numbers you kept track of.
That's what I've been tring to do this whole time extracting digits, but I don't know how to.

>> This doesn't answer my question at all.
Salem is offering you important advice though. i != i is trivially false, like he said, *and* you use i in places beside the for loop... so you might want to take a look at his post again.
I think I need to explain my myself better lol I just made a new code has no error.

s this supposed to work with other numbers than eight?
Yes it works for all numbers, BUT it doesn't extract the digits. It gives me the sum of the odd numbers, BUT I can't extract the digits which add to the number.