Maximum being exceeded?

Page 1 of 1

5 Replies - 466 Views - Last Post: 12 July 2009 - 03:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=114394&amp;s=bac9e5d84c0c8364a1d6630deda54564&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Pipey85

Reputation: 0
• Posts: 41
• Joined: 30-June 09

Maximum being exceeded?

Posted 12 July 2009 - 03:20 PM

I dont understand why it goes past the 4000000 when I tell it to check to see if goes higher that this to stop the loop? could one of you coding gurus please explain why this is the case?

```

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{

unsigned long Sum = 0;
unsigned long InitialNum = 0;
unsigned long SecNum = 1;
unsigned long Temp =1;
bool Continue = true;

while (Continue) {

cout << endl << InitialNum;
if (InitialNum % 2 == 0)
{
cout << "even";
Sum += InitialNum;
if (InitialNum >= 4000000)
Continue = false;

}

cout << endl << SecNum;
if (SecNum % 2 == 0){
cout << "even";
Sum += SecNum;
if (SecNum >= 4000000)
Continue = false;

}

cout << endl << Temp;
if (Temp % 2 == 0){
cout << "even";
Sum += Temp;
if (Temp >= 4000000)
Continue = false;

}

InitialNum = (SecNum + Temp);
SecNum = (InitialNum + Temp);
Temp = (InitialNum + SecNum);
} //end of while

cout << "\nThe sum of all the even numbers is >>>> " << Sum;

char f;
cin >> f;
return 0;
}

```

Thanks in advance guys and gals!

Is This A Good Question/Topic? 0

Replies To: Maximum being exceeded?

#2 xPurplex

Reputation: 10
• Posts: 93
• Joined: 17-June 09

Re: Maximum being exceeded?

Posted 12 July 2009 - 03:35 PM

sorry.... the code makes no sense to me what are u trying to do here?

#3 Pipey85

Reputation: 0
• Posts: 41
• Joined: 30-June 09

Re: Maximum being exceeded?

Posted 12 July 2009 - 03:37 PM

xPurplex, on 12 Jul, 2009 - 02:35 PM, said:

sorry.... the code makes no sense to me what are u trying to do here?

im trying to write out the fibonacci sequence and add up all the even numbers that are below 4,000,000
but it keeps going way past 4,000,000.

#4 xPurplex

Reputation: 10
• Posts: 93
• Joined: 17-June 09

Re: Maximum being exceeded?

Posted 12 July 2009 - 03:41 PM

two things...

1. proper syntax:
```if(condition){code}
```

2. the above 4,000,000 check is within the check if the # is even, so it will only check if it's over 4,000,000 if the number is even.

Hope I helped

#5 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

Re: Maximum being exceeded?

Posted 12 July 2009 - 03:44 PM

The code does exactly what you told it to, when it reaches 4000000 it ends the loop... whats the problem?

Well, look at where you placed your exit condition -- at the very top if the loop you place Continue = false -- and so it completes that loop and prints numbers that are > exit condition -- but then it ends.

You could use 'break' rather than using the sentinel value. Or, you could put your if-condition at the bottom of the loop (after the next values have been calculated).

#6 Pipey85

Reputation: 0
• Posts: 41
• Joined: 30-June 09

Re: Maximum being exceeded?

Posted 12 July 2009 - 03:46 PM

Ahhhhhhh thankyou! im gunna blame it on the fact its getting late but thanks dude