C programming help question (if/else)

  • Thread starter Deleted User
  • Start date
  • Views 1,029
  • Replies 3
D

Deleted User

Guest
OP
So I'm trying to learn c, and trucking along rather nicely, when WHAM, huge fucking truck in the name of if/else.

Below is my attached code.

It should be
1) if 1, AC turned on.
2) if 2, AC turned off.
3) if anything else, incorrect key.

But what's happening is that 2) & 3) are working perfectly normal, but when I use 1...

the text for both conditions 1) & 3) appear at the same time! And I can't understand why!

Maybe someone can point it out to me what I'm doing wrong. I've been here trying different solutions and googling to try and learn myself, so I come and beg the question itself.

If I'm in the wrong forum section, please feel free to alert a moderator and/or have it moved to the correct section.

Thanks in advanced.


Code:
//Learning if statements!

#include <stdio.h>

int main () {
    int iresponse = 0;
    
    printf("\nAc Control Program.\nPlease chose an option.\n[1] to turn on the AC system.\t[2] to turn off the AC system.");
    scanf("%d", &iresponse);
    
        if (iresponse == 1){
            printf ("\nThe AC system is now off!\nThis system will now go into standby mode."); //end 1
        }
        if (iresponse == 2){
            printf ("\nThe AC system is now off!\nThis system will now go into standby mode."); //end 2
        }
        else
            printf ("\nIncorrect key. Shutting down."); //end else   
        
}//end main
 
D

Deleted User

Guest
OP
If you want to have mutually exclusive conditions, use "else if".
Code:
       if (iresponse == 1){
           printf ("\nThe AC system is now off!\nThis system will now go into standby mode."); //end 1
       }
       else if (iresponse == 2){
           printf ("\nThe AC system is now off!\nThis system will now go into standby mode."); //end 2
       }
       else
           printf ("\nIncorrect key. Shutting down."); //end else

That way, only one of these cases will be executed, unlike what you had before, where the first conditional was completely separate and unrelated to the second "if-else" statement. That was why when you input 1, you got the results for both 1 and 3.
 
  • Like
Reactions: VinsCool

Minox

Thanks for the fish
Former Staff
Joined
Aug 27, 2007
Messages
6,985
Trophies
2
XP
6,083
Country
Japan
The problem is that the second if statement should be an "else if" if they should all belong to the same if/else statement. How you've currently arranged it means that there's essentially one if statement directly followed by an if/else statement and both are handled individually.
 
D

Deleted User

Guest
OP
If you want to have mutually exclusive conditions, use "else if".
Code:
       if (iresponse == 1){
           printf ("\nThe AC system is now off!\nThis system will now go into standby mode."); //end 1
       }
       else if (iresponse == 2){
           printf ("\nThe AC system is now off!\nThis system will now go into standby mode."); //end 2
       }
       else
           printf ("\nIncorrect key. Shutting down."); //end else

That way, only one of these cases will be executed, unlike what you had before, where the first conditional was completely separate and unrelated to the second "if-else" statement. That was why when you input 1, you got the results for both 1 and 3.
Thank you! The book I'm reading hadn't noted this, oddly enough, but then again, it was only using if/else and not else if.

It works now, I'll comment it in the morning for reference for the future. Thanks again!

--------------------- MERGED ---------------------------

The problem is that the second if statement should be an "else if" if they should all belong to the same if/else statement. How you've currently arranged it means that there's essentially one if statement directly followed by an if/else statement and both are handled individually.
Yup, I see it now, thanks for the answer! Book never mentioned else if, only if/else seperately!
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • JuanMena @ JuanMena:
    Will you give me mouth to mouth oxygen if my throat closes?
  • K3N1 @ K3N1:
    Nah the air can do that
  • K3N1 @ K3N1:
    Ask @x65943 he's trained for that stuff
  • JuanMena @ JuanMena:
    Kissing random dudes choking in celery? Really? Need to study for that?
  • K3N1 @ K3N1:
    Yes it requires a degree
  • K3N1 @ K3N1:
    I could also yank out the rest of my teeth but theirs professionals for that
  • x65943 @ x65943:
    If your throat closes, putting oxygen in your mouth will not solve anything - as you will be introducing oxygen prior to the area of obstruction
  • JuanMena @ JuanMena:
    Just kiss me Kyle.
  • x65943 @ x65943:
    You either need to be intubated to bypass obstruction or create a stoma inferior to the the area of obstruction to survive
  • x65943 @ x65943:
    "Just kiss me Kyle." And I thought all the godreborn gay stuff was a smear campaign
  • JuanMena @ JuanMena:
    If I die, tell my momma I won't be carrying Baby Jesus this christmas :sad::cry:
  • K3N1 @ K3N1:
    Smear campaigns are in The political section now?
  • JuanMena @ JuanMena:
    Chary! Chary! Chary, Chary, Chary!
  • Sonic Angel Knight @ Sonic Angel Knight:
    Pork Provolone :P
  • Psionic Roshambo @ Psionic Roshambo:
    Sounds yummy
  • K3N1 @ K3N1:
    Sweet found my Wii u PSU right after I ordered a new one :tpi:
  • JuanMena @ JuanMena:
    It was waiting for you to order another one.
    Seems like, your PSU was waiting for a partner.
  • JuanMena @ JuanMena:
    Keep them both
    separated or you'll have more PSUs each year.
  • K3N1 @ K3N1:
    Well one you insert one PSU into the other one you get power
  • JuanMena @ JuanMena:
    It literally turns it on.
  • K3N1 @ K3N1:
    Yeah power supplies are filthy perverts
  • K3N1 @ K3N1:
    @Psionic Roshambo has a new friend
    +1
  • JuanMena @ JuanMena:
    It's Kyle, the guy that went to school to be a Certified man Kisser.
  • Psionic Roshambo @ Psionic Roshambo:
    Cartmans hand has taco flavored kisses
  • A @ abraarukuk:
    hi guys
    A @ abraarukuk: hi guys