Help with C++ if-else statements

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Schmendreck
Posts: 197
Joined: Wed Aug 22, 2007 6:18 pm UTC
Location: New York

Help with C++ if-else statements

Postby Schmendreck » Sun Apr 06, 2008 10:11 pm UTC

I recently started teaching myself how to code in C++ and as you might have guessed from the title I am having trouble with if else statements.
I have the code:

Code: Select all

int n;
cout << "Input a number.";
cin >> n;
if (n % 2 == 1);
    cout << "It is odd";
else
    cout << "It is even";


However, every time i try to compile, it demands that there must be a primary expression before else. I am unable to figure out what is wrong. Any help would be appreciated.
A critic is a person who creates nothing of their own and therefore feels entitled to judge others.
-Robert A. Heinlein

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Help with C++ if-else statements

Postby Rysto » Sun Apr 06, 2008 10:22 pm UTC

You have a semi-colon at the end of the if statement.

User avatar
Schmendreck
Posts: 197
Joined: Wed Aug 22, 2007 6:18 pm UTC
Location: New York

Re: Help with C++ if-else statements

Postby Schmendreck » Sun Apr 06, 2008 10:26 pm UTC

OH, thanks
A critic is a person who creates nothing of their own and therefore feels entitled to judge others.

-Robert A. Heinlein

MatrixFrog
Posts: 21
Joined: Tue Dec 25, 2007 7:16 am UTC

Re: Help with C++ if-else statements

Postby MatrixFrog » Tue Apr 15, 2008 8:04 am UTC

May I take this opportunity to suggest a minor modification to your code?

Code: Select all

int n;
cout << "Input a number.";
cin >> n;
if (n % 2 == 1) {
    cout << "It is odd";
}
else {
    cout << "It is even";
}


Completely not necessary in this case, but suppose you wanted to keep track of whether n is even or odd, and access that information later?

Code: Select all

int n;
bool nIsEven;
cout << "Input a number.";
cin >> n;
if (n % 2 == 1)
    cout << "It is odd";
    nIsEven = false;
else
    cout << "It is even";
    nIsEven = true;

This will give you the same error as before.

It's just too easy to make that mistake when adding lines to your code, so I recommend using {}s for all if/while/for statements.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Help with C++ if-else statements

Postby Sc4Freak » Tue Apr 15, 2008 12:33 pm UTC

That's a stylistic issue. Personally, I don't bother with the extra parentheses. My IDE automatically indents code and updates indentations for me, so when writing code I would see something like that immediately because the auto-indentation would be incorrect.

User avatar
evilbeanfiend
Posts: 2650
Joined: Tue Mar 13, 2007 7:05 am UTC
Location: the old world

Re: Help with C++ if-else statements

Postby evilbeanfiend » Tue Apr 15, 2008 1:41 pm UTC

ditto for me but my current workplace mandates the braces anyway, it not an uncommon practice. i certainly think that 1 or other of always fixing indentation and extra braces is a good idea (though indentation is better imho as it help in other ways too)
in ur beanz makin u eveel

User avatar
JamesCFraser
Posts: 32
Joined: Wed Nov 14, 2007 9:47 pm UTC

Re: Help with C++ if-else statements

Postby JamesCFraser » Tue Apr 15, 2008 3:04 pm UTC

MatrixFrog wrote:May I take this opportunity to suggest a minor modification to your code?

Code: Select all

int n;
cout << "Input a number.";
cin >> n;
if (n % 2 == 1) {
    cout << "It is odd";
}
else {
    cout << "It is even";
}


If you're going to suggest adding unnecessary braces, at least chose a logical indentation paradigm. E.G:

Code: Select all

int n;
cout << "Input a number.";
cin >> n;
if (n % 2 == 1)
{
    cout << "It is odd";
}
else
{
    cout << "It is even";
}


By placing matching braces on the same horizontal level, it becomes easy to see the matching pairs of braces, which is the whole point of indentation IMO

User avatar
Marvin
Posts: 153
Joined: Thu Nov 22, 2007 8:03 am UTC
Location: Croatia
Contact:

Re: Help with C++ if-else statements

Postby Marvin » Tue Apr 15, 2008 3:19 pm UTC

JamesCFraser wrote:
MatrixFrog wrote:May I take this opportunity to suggest a minor modification to your code?

If you're going to suggest adding unnecessary braces, at least chose a logical indentation paradigm. E.G:

Code: Select all

int n;
cout << "Input a number.";
cin >> n;
if (n % 2 == 1)
{
    cout << "It is odd";
}
else
{
    cout << "It is even";
}


By placing matching braces on the same horizontal level, it becomes easy to see the matching pairs of braces, which is the whole point of indentation IMO

Not really, it's just wasting lines of code, you see the block because of indentation. But that's just my opinion.
42
--
If God intended us to program we would be born with serial I/O ports.

User avatar
Hammer
Because all of you look like nails.
Posts: 5491
Joined: Thu May 03, 2007 7:32 pm UTC
Contact:

Re: Help with C++ if-else statements

Postby Hammer » Tue Apr 15, 2008 3:20 pm UTC

Enough with the bracing style crap! Take it over here if you absolutely must repeat all your points about bracing style yet again.
"What's wrong with you mathematicians? Cake is never a problem."


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 10 guests