Thread: If command problems.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    15

    If command problems.

    I feel so bad making another topic so soon after the other one. I was just wondering if there was something wrong with my if statements here. When I choose an option other than option 1,2, or 3 it works fine the program looks back and asks for another option to be inputted.

    However when I input 1, 2, or 3 as the option, it's not working out.
    The positive if commands seem to be working. When I select option 1 it prints option 1, however it then prints option 3 also. When I select option 2, it prints option 2 however it then prints option 3 also. If I select option 3, it continues in the while loop even though it should pass it by.

    Hopefully this is just a small thing I've done wrong and quick to solve.
    Alex

    Code:
    //A program that calculates the reduced mass of diatomic molecules.
    //The following libraries will be used.
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    Manual_Input()
    {
    
    }
    
    Textfile_Input()
    {
    
    }
    
    int main()
    {
    int selection;//Defines the variable used to decide on a keyboard or text file input.
    while (selection!=1||selection!=2)
    {
    
        printf("A program to calculate the reduced mass of diatomic molecules.\n");//Intro Message.
        Data_input://Allows the program to loop if the user selects a value other than 1,2, or 3.
        printf("Input data from the keyboard, from a text\nfile, or close the program?\n");
        printf("1. From the keyboard.\n");printf("2. From a text file.\n");printf("3. Close program.\n");printf(":");
        scanf("%d",&selection);//Allows the user to choose whether or not to input from a text file or the keyboard.
    
        if (selection==1)
            {
                printf("Hello");
            }
        if (selection==2)
            {
                printf("how are you?");
            }
        if (selection!=1 || selection!=2 || selection!=3)
            {
                printf("Invalid Option, please choose again. ");
            }
    
    }
    return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    int selection;
    while (selection!=1||selection!=2)
    Tell me what this code does
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (selection!=1 || selection!=2 || selection!=3)
    You want && here, not ||

    At the moment, you have say
    selection = 1
    which means you have
    if ( false || true || true )

    But then if you have
    selection = 2
    you have
    if ( true || false || true )

    Your if statement as written is always true.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    Quote Originally Posted by std10093 View Post
    Code:
    int selection;
    while (selection!=1||selection!=2)
    Tell me what this code does
    Well that code was providing the loop, it seemed to be working because it kept looping back.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    Quote Originally Posted by Salem View Post
    > if (selection!=1 || selection!=2 || selection!=3)
    You want && here, not ||

    At the moment, you have say
    selection = 1
    which means you have
    if ( false || true || true )

    But then if you have
    selection = 2
    you have
    if ( true || false || true )

    Your if statement as written is always true.
    But if it wasn't 1 it doesn't necessarily mean False || True || True, because someone could have put in the wrong number like 4. No? Then they would all be false.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    When I reach for first time the line of while (line 2 of the quoted code) want is the value of selection?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    Ermm it wouldn't have a value?

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Exactly. So what's the behavior of the code?

    Better initialize your variable
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    Well this is the thing, because I have a chemistry teacher and not a programming teacher who taught us, we didn't go into that depth of detail. I assumed before you assigned the value, it still existed just the value had an arbritray value of 0 or something like that.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What happens to a declared, unitialized variable in C?

    You may use the do while loop, if you don't want to initialize it
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    Code:
    //A program that calculates the reduced mass of diatomic molecules.
    //The following libraries will be used.
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    Manual_Input()
    {
    
    }
    
    Textfile_Input()
    {
    
    }
    
    int main()
    {
    int selection;//Defines the variable used to decide on a keyboard or text file input.
    selection=4;//Sets the value of the variabe selection so it can be used for the do while loop.
    
    do {
        printf("A program to calculate the reduced mass of diatomic molecules.\n");//Intro Message.
        Data_input://Allows the program to loop if the user selects a value other than 1,2, or 3.
        printf("Input data from the keyboard, from a text\nfile, or close the program?\n");
        printf("1. From the keyboard.\n");printf("2. From a text file.\n");printf("3. Close program.\n");printf(":");
        scanf("%d",&selection);//Allows the user to choose whether or not to input from a text file or the keyboard.
    
        if (selection==1)
            {
                printf("Hello");
            }
        if (selection==2)
            {
                printf("how are you?");
            }
        if (selection!=1 || selection!=2 || selection!=3)
            {
                printf("Invalid Option, please choose again. ");
            }
    
    } while (selection!=1 || selection!=2 || selection!=3);
    return 0;
    }
    I've changed the code to this, and also set the value of the selection variable so it has a different value to that of 1, 2 or 3.

    Code:
    int selection;//Defines the variable used to decide on a keyboard or text file input.
    selection=4;//Sets the value of the variabe selection so it can be used for the do while loop.
    However the loop doesn't seem to be working. Even when I set the value to 1, 2 or 3 the loop repeats.

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    Quote Originally Posted by Salem View Post
    > if (selection!=1 || selection!=2 || selection!=3)
    You want && here, not ||

    At the moment, you have say
    selection = 1
    which means you have
    if ( false || true || true )

    But then if you have
    selection = 2
    you have
    if ( true || false || true )

    Your if statement as written is always true.
    Never mind, I maybe misunderstood how if statements work. This is now working. Thank you very much.

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Read again Salem's post

    Also when you say doesn't work, specify what is happening

    //you just read it :P
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Alex Saunders View Post
    Never mind, I maybe misunderstood how if statements work. This is now working. Thank you very much.
    No, it appears that you misunderstood boolean logic, not if-statements.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems building a "command-line" calculator
    By mmario10 in forum C Programming
    Replies: 6
    Last Post: 10-31-2011, 04:27 AM
  2. Problems in the Code with if command.
    By Mr.Lnx in forum C Programming
    Replies: 21
    Last Post: 10-08-2011, 03:31 AM
  3. Command Line problems
    By jiahwa008 in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2011, 02:41 PM
  4. Command line how to handle optional command.
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2010, 11:41 PM