1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

if then statement in C++ 6.0

Discussion in 'Software Development' started by The G-man, Oct 18, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. The G-man

    The G-man Thread Starter

    Joined:
    Jun 3, 2001
    Messages:
    140
    Hi,

    I have created an array called intDigit[]. It holds 8 integer values. I am trying to sum these values excluding the 4th and 5th ones:

    for (i=1; i<=8; i++)
    {

    //fill array here

    }

    //create if statement to exclude the 2 values

    if (i!=4 || i!=5)
    {
    intSum = intSum + intDigit;
    }


    I assume this if statement is incorrect but i'm not sure what I need to do. Can anyone help???

    Thanks
     
  2. coderitr

    coderitr

    Joined:
    Oct 12, 2003
    Messages:
    3,015
    First, arrays in c++ are zero-based. An array of 8 elements is indexed as 0 - 7 and not 1 - 8. So ...

    for ( i = 0; i < 8; i++ )
    {

    //fill array here

    }

    Secondly, you need another for( ) loop around the sum routine

    //create if statement to exclude the 2 values

    for ( i = 0; i < 8; i++ )
    {
    if (i != 4 && i != 5)
    intSum = intSum + intDigit;
    }

    Third if you use || (or) when you're checking whether the index is 4 or 5 then you'll get numbers 4 and 5 in your sum. Use && (and) instead
     
  3. The G-man

    The G-man Thread Starter

    Joined:
    Jun 3, 2001
    Messages:
    140
    Thanks coderitr! Works perfectly now.
     
  4. coderitr

    coderitr

    Joined:
    Oct 12, 2003
    Messages:
    3,015
    you're welcome
    :)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/172922

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice