[prog] Re: more C++

Conor Daly conor.daly at oceanfree.net
Sat Dec 13 23:47:17 EST 2003


On Sat, Dec 13, 2003 at 05:23:38PM -0500 or so it is rumoured hereabouts, 
wolf thought:
> Okay, I'm starting to get really confused : )
> This is my program now and it compiles without an error, when you type 
> in an odd number you get a correct response but when
> you enter in an even number it prints out both the number is odd and 
> the number is even. Why isn't there any error message and
> what did I do incorrectly that it only works for odd numbers?
> 
> 
> 
> #include <iostream>
> 
> int main()
> {
> 
> int integer1;
> 
> std::cout << "Enter a whole number\n";
> std::cin >> integer1;

This is an if block
 
> if(integer1 % 2 == 0) {
> std::cout << "Your number is even.\n";
> }

This is _not_ an if test. 
 
> else(integer1 % 2 !=0);

This line will be executed every time
 
> std::cout << "Your number is odd.\n";
> 
> return 0;
> }

> else(integer1 % 2 !=0);
> std::cout << "Your number is odd.\n";

The two lines above should be an if or an else block.  Since you've
already done an "is integer1 even" test above, you can then just do:

else {
 std::cout << "Your number is odd.\n";
}

because that will only happen if the "is integer1 even" test fails.

Conor
-- 
Conor Daly <conor.daly at oceanfree.net>

Domestic Sysadmin :-)
---------------------
Faenor.cod.ie
 10:44pm  up 51 days, 15:52,  0 users,  load average: 0.00, 0.15, 0.15
Hobbiton.cod.ie
 10:40pm  up 51 days, 15:44,  1 user,  load average: 0.00, 0.00, 0.00


More information about the Programming mailing list