C programming help question (if/else)

  • Thread starter Deleted User
  • Start date
  • Views 1,114
  • 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,995
Trophies
2
XP
6,156
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!
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    AncientBoi @ AncientBoi: All right! Who stole my right sock? :angry: