Code::Blocks Forums

User forums => General (but related to Code::Blocks) => Topic started by: sirhakonik on July 23, 2019, 05:06:56 pm

Title: else command does not work
Post by: sirhakonik on July 23, 2019, 05:06:56 pm
While using codeblocks, i've noticed a bug, that when coding:
#include <iostream>
using namespace std;
string PIN;
int main()
{
    cout << "Hello!";
    cout<<endl <<"insert PIN:";
    cin>> PIN;
    if(PIN=="2137");
    {
    cout<<"PIN Correct";
    }
    else;
    {
    cout<<"PIN Incorrect";
    }
    return 0 ;

when i press f9 my program does not work, and i see "error: else without previous if".
 Is there way to repair it?
Title: Re: else command does not work
Post by: stahta01 on July 23, 2019, 05:18:42 pm
Please read the rules and find a website to ask your programming questions on.
http://forums.codeblocks.org/index.php/topic,9996.0.html (http://forums.codeblocks.org/index.php/topic,9996.0.html)

Note: Putting an semi colon after else is wrong!

Tim S.
Title: Re: else command does not work
Post by: sodev on July 23, 2019, 06:52:31 pm
While browsing the forum, i noticed a bug, it's written after sirhakonik ;D

Well, i couldn't resist, i had to write this.

Note: Putting an semi colon after else is wrong!

I couldn't resist to answer this one as well, this is actually wrong :). Of course you can write a semicolon after the else, this is just the shortcut to create an empty else case, you could also do this by opening an empty block with parentheses.

It's not even an error to put the semicolon after the if, the effect is the same, the error however is that extra block between the if and else, with the semicolon after the if this results into two blocks of code between the if and else, and that is the error, there can be only one ;).

Looks like i have smart-ass-day today ::)
Title: Re: else command does not work
Post by: raynebc on July 24, 2019, 07:23:18 pm
Even in the case of being pedantic, I have to disagree.  stahta01 didn't say it can never be used, in the case of this code example he said it was wrongly used here and is why the program isn't behaving as is wanted.
Title: Re: else command does not work
Post by: sodev on July 24, 2019, 08:16:44 pm
Dammit, i got beaten, how could i miss that?

Ok, you two are right, the semicolon after the else is an error because it is causing unwanted behavior. I am right that the semicolon after the if is causing the compile error, plus it is causing unwanted behavior.

So we are all right, what did we win ;D?
Title: Re: else command does not work
Post by: jrelzbq on May 30, 2020, 10:47:18 am
While browsing the forum, i noticed a bug, it's written after sirhakonik ;D dqfansurvey (https://www.dqfansurvey.website/)

Well, i couldn't resist, i had to write this.

Note: Putting an semi colon after else is wrong!

I couldn't resist to answer this one as well, this is actually wrong :). Of course you can write a semicolon after the else, this is just the shortcut to create an empty else case, you could also do this by opening an empty block with parentheses.

It's not even an error to put the semicolon after the if, the effect is the same, the error however is that extra block between the if and else, with the semicolon after the if this results into two blocks of code between the if and else, and that is the error, there can be only one ;).

Looks like i have smart-ass-day today ::)

Very informative post. I really do hope and pray this stuff works!