[prog] Re: more C++

Dominik Schramm dominik.schramm at gmxpro.net
Sun Dec 14 00:47:53 EST 2003


On 12/13/2003 11:23 PM, wolf wrote:

 > Okay, I'm starting to get really confused : )

Oh, I'm sorry, I guess that's my fault... my attempt at formally
explaining what an if statement must look like.

Here's the correct code (and below, the explanation why yours
doesn't work):

#include <iostream>
in main()
{  
    int integer1;

   std::cout << "Enter a whole number\n";
   std::cin >> integer1;

   if(integer1 % 2 == 0) 
   {
      std::cout << "Your number is even.\n";
   }
   else
   {
      std::cout << "Your number is odd.\n";
   }

   return 0;
}

In this case -- because there is only one simple statement
in both the "then" part and the "else" part -- you could 
alternatively write:

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

Two recommendations:
1. Don't leave away the braces, or: forget what "you could
   alternatively write" !
   (I just mentioned this one alternative so you can
    maybe still make sense out of my formal definition)

2. Always align the opening and closing braces (see above)
   so you see right away if you forgot one or have one too many.


Why doesn't your code work?

In a nutshell:
You confused if-else and if-else-if(-else) and supplied an empty
else-if statement.


*This* code snippet of yours

if(integer1 % 2 == 0) {

std::cout << "Your number is even.\n";
}

else(integer1 % 2 !=0);

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


is equivalent to this:


if (integer1 % 2 == 0)
{
   std::cout << "Your number is even.\n";
}
else
{
   integer1 % 2 != 0;
}

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


See the error?
Translated into English:
If integer1 is even then print "it's even"; if it's not, then 
do some nonsense.
In any case print "it's odd".

You probably meant to write an if-elseif statement.
Here's how that would correctly be written:

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

I hope you can see the difference.
Can you? If not, write again, please!

The part "if (integer1 % 2 != 0)" after the else is 
not necessary.

regards,

Dominik



>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?
>  
>

See the English translation above:
If the number is not even, then don't print that it's even. But do
print that it's odd.
If the number *is* even, then print that it's even and
anyway print that it's odd.

>#include <iostream>
>
>
>int main()
>
>
>{
>
>int integer1;
>
>std::cout << "Enter a whole number\n";
>std::cin >> integer1;
>
>if(integer1 % 2 == 0) {
>
>std::cout << "Your number is even.\n";
>}
>
>else(integer1 % 2 !=0);
>
>
>std::cout << "Your number is odd.\n";
>
>
>
>return 0;
>}
>
>  
>





More information about the Programming mailing list