There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
if then statement in C++ 6.0


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
The G-man's Avatar
Senior Member with 133 posts.
 
Join Date: Jun 2001
18-Oct-2003, 05:21 PM #1
Question if then statement in C++ 6.0
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];
}


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

Thanks
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
18-Oct-2003, 06:00 PM #2
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[i];
}

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
The G-man's Avatar
Senior Member with 133 posts.
 
Join Date: Jun 2001
18-Oct-2003, 06:22 PM #3
Thumbs up Thanks!
Thanks coderitr! Works perfectly now.
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
18-Oct-2003, 07:36 PM #4
you're welcome
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:53 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.