Thread: help!!! code needs fixing

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    8

    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;
    Last edited by hockey123; 10-17-2008 at 10:50 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    Quote Originally Posted by Salem View Post
    > 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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    Quote Originally Posted by Salem View Post
    > 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.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It answers your first question, namely "What?"

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> 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. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    Quote Originally Posted by citizen View Post
    >> 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing the Indentation draft
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-23-2008, 11:17 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM