function keeps crashing my program???

Started by
10 comments, last by Driv3MeFar 17 years, 6 months ago
Hi. I'm trying to create a function that will return false if the argument passed to it is false. Here is what i have:

bool EvenNo(int value)
{
	int val = value-1;
	for (int i = 0; i <= value; i + 2)
	{
		if(i == val)
		{
			return false;
		}
        }
	return true;
}
but when i call it the program crashes. Any idea why? hear is coppy and past friendly pice of code to compile:

#include <iostream>
using namespace std;
bool EvenNo(int value);

int main()
{
    int x;
    cout<<"Please enter a number -->";
    cin>>x;
    
    if(EvenNo(x) == true)
    {
        cout<< x << " is an even number"<<endl;
    }
    else
    {
        cout<< x << " is an odd number"<<endl;
    }
    system("pause");
    return 0;
}
bool EvenNo(int value)
{
	int val = value-1;
	for (int i = 0; i <= value; i + 2)
	{
		if(i == val)
		{
			return false;
		}
	}
	return true;
}
thanks in advance.
Advertisement
If the last bit is set, then the number is odd.

bool EvenNo(int value)
{
return (value & 1);
}

Actually, I think your logic would be:


bool EvenNo(int value)
{
return !(value & 1);
}

The reason it's locking up is that 'i + 2' should be 'i += 2'
I assume, you do not mean crash, but your program hangs. (does not respond anymore)

The reason for that would be your "for" loop, because you do not increment your counter variable. The loop should better read like:

for (int i = 0; i <= value; i += 2)


But, on the other hand, there are much better ways to test for an even/odd number. Something like

cout << "number " << value << " is " << (0 != (value % 2) ? "odd" : "even") << endl;
God is real... unless declared integer.
Quote:Original post by aaroncox_123
I'm trying to create a function that will return false if the argument passed to it is false.


You mean if the arguement is even? There are much better ways (look up modulo if you want) but this is probably a good exercise on loops, so whatever.

About your bug. Look carefully at the for loop.
for (int i = 0; i <= value; i + 2)

Will i ever change? If you go: w = i + 2, does that change i? Think about it.

You've probably got it already. i just stays 0, so it sits there forever checking if i is less than value. It always is. So your program never ends. what you meant was i += 2. Which makes your loop:
for (int i = 0; i <= value; i += 2)


EDIT: NatasDM, that's an evil thing to suggest in a beginners forum.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!
Quote:Original post by RAZORUNREAL
EDIT: NatasDM, that's an evil thing to suggest in a beginners forum.


I find it fine - if one don't forget to link an appropriate tutorial about bitwise operators. This one can help, but he assume that you already know something about bit and bytes. Fortunately, wikipedia come to the rescue :) [very basic informations; infos about binary arithmetic].

Regards,
Sorry, for some reason I thought it would only work on little-endian systems. Which doesn't make a whole lot of sense...
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!
Quote:Original post by rklaffehn
I assume, you do not mean crash, but your program hangs. (does not respond anymore)

The reason for that would be your "for" loop, because you do not increment your counter variable. The loop should better read like:

for (int i = 0; i <= value; i += 2)


But, on the other hand, there are much better ways to test for an even/odd number. Something like

cout << "number " << value << " is " << (0 != (value % 2) ? "odd" : "even") << endl;

For an even faster solution than modulo 2, you can just bitwise AND (&) the number with 1. It will return 0 if even, 1 if odd.

(value & 1 ? "odd" : "even")
NextWar: The Quest for Earth available now for Windows Phone 7.
Quote:
For an even faster solution than modulo 2, you can just bitwise AND (&) the number with 1. It will return 0 if even, 1 if odd.


That's really just a micro-optimization. It isn't going to speed anything up; the compiler can trivially replace the operations, so it can pick whichever it decides is faster. Use the one that makes more sense.
Thanks for posting...

I found that changing the for loop didnt make any difference ie: i += 2.

could someone please explanin return(value & 1);

thanks.
Quote:Original post by aaroncox_123
Thanks for posting...

I found that changing the for loop didnt make any difference ie: i += 2.

could someone please explanin return(value & 1);

thanks.

You would really be better off understanding what you've done wrong now than moving on indiscriminantly. If you can't write a for loop, then you aren't quite ready to move onto things like bitwise arithmetic.

What is your modified code, and how does it behave? Be specific...if it crashes, how does it crash? When? What value did you provide the function?

CM

This topic is closed to new replies.

Advertisement