C programming help question (if/else)

  • Thread starter Deleted User
  • Start date
  • Views 1,132
  • 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,999
Trophies
2
XP
6,166
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
  • BakerMan @ BakerMan:
    or that german ninja turtle commercial
    +1
  • K3Nv2 @ K3Nv2:
    Forgot to buy BBQ sauce at the store so made my own ffs
  • RedColoredStars @ RedColoredStars:
    Blues Hog BBQ sauces are pretty good.
  • K3Nv2 @ K3Nv2:
    My insurance has a thing where if I do a virtual visit they'll just send me a $100 gift card to Walmart I'm about to sell a company my medical history for $100 lol
  • BigOnYa @ BigOnYa:
    My insurance has a thing called, " Nope, we aren't paying for that." that they like to use on us all the time. Then we play the "Ok we will go to some other insurance company." then they miraculously say ok we will pay for that.
  • The_Dizzy_Vizzy @ The_Dizzy_Vizzy:
    Well look at what the cat dragged in!!!
  • BigOnYa @ BigOnYa:
    Hey, do you feel any older?
  • K3Nv2 @ K3Nv2:
    No I don't
    +1
  • Xdqwerty @ Xdqwerty:
    @BigOnYa, not right now
    +1
  • BigOnYa @ BigOnYa:
    @K3Nv2 That's my phrase, stop it
    +1
  • K3Nv2 @ K3Nv2:
    Your phrase is I'm not drunk
    +1
  • Xdqwerty @ Xdqwerty:
    I feel a discomfort in my throat
  • SylverReZ @ SylverReZ:
    BigOnYa after a long day.
  • AlbertJulian @ AlbertJulian:
    I got scammed last year by some internet fraudsters who made away with my crypto worth $210,000.I almost lost my life because i was so frustrated and was depressed..5 months ago a friend introduced me to {Assets lab} a crypto recovery company.At first i was very scared because then i was afraid because of the bad experience i had ..Three months ago i decided to contact {Assets lab} and I made my complain to them and they took some information and assured me that they will assist me recover my crypto .. They helped me throughout the process and so far so good they have recovered 95 % of the money that was stolen from me..Today am a happy woman and i will continue to spread the good news.. Contact {Assets lab} for any of your funds lost to scams.. They are the best..Below is their contact informaation Name : ASSETS LAB RECOVERYEmail: [email protected] / Whatsapp: +17126004729Telegram: @Assets_labWebsite: https://lab-assets.tech/
  • Xdqwerty @ Xdqwerty:
    Reported
  • K3Nv2 @ K3Nv2:
    Mario kart 8s been out ten years now due for a 9 any time soon
    +1
  • DTApple @ DTApple:
    Maybe in the upcoming Direct?
  • K3Nv2 @ K3Nv2:
    Probably saving it for a switch 2 announcement
    +2
  • DTApple @ DTApple:
    Ay!
    +1
  • Xdqwerty @ Xdqwerty:
    @DTApple, gonna download the gdevelop app cuz sometimes i don't have the chance to use my pc
  • Xdqwerty @ Xdqwerty:
    And I also got an idea for another game
    Xdqwerty @ Xdqwerty: And I also got an idea for another game