Maximum being exceeded?

why is the program going past the point i specify

Page 1 of 1

5 Replies - 363 Views - Last Post: 12 July 2009 - 03:46 PM Rate Topic: -----

#1 Pipey85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • 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 :blink: what are u trying to do here?
Was This Post Helpful? 0
  • +
  • -

#3 Pipey85  Icon User is offline

  • New D.I.C Head

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

Re: Maximum being exceeded?

Posted 12 July 2009 - 03:37 PM

View PostxPurplex, on 12 Jul, 2009 - 02:35 PM, said:

sorry.... the code makes no sense to me :blink: 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.
Was This Post Helpful? 0
  • +
  • -

#4 xPurplex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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).
Was This Post Helpful? 0
  • +
  • -

#6 Pipey85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :P but thanks dude
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1