Thomas Hruska wrote:
> Jon wrote:
>
>> I am using Visual C++ 2008 Express Edition.
>> I don't get any warnings for the Break;, should I?
>>
>
> Victor - you still around? Remember a while back I said 'bool' had
> issues under VC++? IIRC, 'bool' becomes some sort of weird COM
> object'ish thing and barfs up wrong values as a result. This could be
> the same thing only not buried in 100,000 lines of code.
>
I'm still around...and I've NOT seen any problems with "bool" (other
than their size)
other than having written the code incorrectly the following works
#include <iostream>
using namespace std;
bool bisPrime(int number)
{
for( int i=2; i < number; i++)
{
int leftOver=(number % i);
if (leftOver==0)
{
return false;
break;
}
}
return true;
}
int iisPrime(int number)
{
int response;
for( int i = 2; i < number; i++)
{
int leftOver = (number % i);
if (leftOver == 0)
{
response = 0;
return response;
break;
}
}
response = 1;
return response;
}
int main()
{
int val;
//Get a number from the user to check
cout << "please enter a number" << endl;
cin >> val;
if (bisPrime(val))
// Calls the function and sends val to function
cout << val << " is a prime\n";
else
cout << val << " is not a prime\n.";
if (iisPrime(val))
// Calls the function and sends val to function
cout << val << " is a prime\n";
else
cout << val << " is not a prime\n.";
// test the first 100,000 numbers
for (int j = 1; j < 100000; ++j)
{
if (bisPrime(j) != iisPrime(j))
cout << val << "has a problem\n";
}
return(0);
}
I put it all in one, including a test on the bool vs int flavors of all
the numbers from 1 to 99,999
with nary a huccup
>
> Jon, the warning you should be getting is something along the lines of
> "code is unreachable". The warning indicates that the return statement
> on the line before exits the function and the following break statement
> will never, ever execute. The warning level should be high enough (at
> least /W3) so you don't end up writing code later on that does weird
> stuff causing you to spend forever debugging that the compiler would
> have caught and let you know about in the first place.
>
> Jon, if you put a breakpoint on the return statements and use F11 to
> step through the code at the breakpoint, what happens with the 'bool'
> stuff? Does it immediately return or does the code wander off into
> "la-la land" when stepping _into_ the return value (COM object code -
> stuff you probably wouldn't recognize unless you've messed with COM
> before - hence I'm calling it 'la-la land' for lack of a better
> description)? There is a chance you've accidentally stumbled upon one
> of the most bizarre and _unreplicatable_ VC++ bugs I've ever run into.
>
>
[Non-text portions of this message have been removed]