[prog] Re: more C++

wolf wolf at wolfrising.net
Sat Dec 13 18:23:38 EST 2003


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;

if(integer1 % 2 == 0) {

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

else(integer1 % 2 !=0);


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



return 0;
}



On Dec 13, 2003, at 4:54 PM, Dominik Schramm wrote:

> On 12/13/2003 10:27 PM, wolf wrote:
>
> > I've tried taking the { and } out but I thought if there was an if
> > statement you needed the braces.
>
> The if-else statement looks like this
> (everything in "" has to be typed literally!):
>
> "if" "(" condition ")" statement1 "else" statement2
>
> statement1 (and statement2) has either the form:
>
>  simple_statement ";"
>  e.g. "x = 1;"
>
> or
>
>  "{" simple_statement ";" simple_statement2 ";" ... "}"
>  e.g. "{ x = 1; y = 2; }"
>
> You wrote (condensed and simplified):
>
>  "if (condition) { simple_statement1; else simple_statement2; }"
>
> This is a syntax error.
> It should be (always using braces):
> "if (condition) { statement1; } else { statement2; }"
>
> Some consider it good style to always use braces in if-else statements.
>
> Note that between the words and characters in "",
> you can put as much whitespace and multiline comments as you want
> (also none!):
> This is legal (but looks ugly in my opinion):
> if(         a == b
>   )/*just a small comment*/ {do_something();}
>
> > Is it a little error I'm making that
> > I just don't seem to see? Or am I way off track?
>
> No, you're very close. :-)
>
> regards,
> dominik
>
>
>
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
>



More information about the Programming mailing list