Thread: My if statement doesn't seem to work.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    14

    My if statement doesn't seem to work.

    I have the following as code:
    Code:
    #include <stdio.h>
    main()
    {
        char answer = getche();
        char pi = 3;
        if (answer != pi)
            printf("\a");
    }
    But whenever I run the program, the \a sound (which is a beep) still goes off, no matter what I type, which is supposed to be a 3. This happens whether it says \a, a, or aaa. When it says == instead of !=, it never makes a sound. It doesn't matter if I type a 3, 4, or 8.

    Is this a problem with the if statement? I'm using what is apparently a horribly outdated compiler called Digital Mars, so that may be the cause. It could also be compiler corruption, since I used a flash drive to transport the fiels from my computer to the folder, but i have no way to re-download it now, so if that's the case, I'll have to get a new compiler entirely (which I should probably do anyways).

    So, is it a problem with my code or the compiler?

    Semi-Edit: Before I even put the post up, I am already leaning towards the assumption that it's corrupted; it won't even open properly anymore.

    Real Edit: As soon as I closed Chrome, my compiler magically opened up out of nowhere. So the compiler is working now, but that doesn't mean it isn't a compiler problem.
    Last edited by KittenAqua; 02-22-2012 at 07:09 PM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    charaters need to be initilized with single quotes if that is what you are going for '3' or do you want ascii code 3 in pi?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The number 3 is different from the character '3'. getche (which returns an int IIRC, so you should change answer to an int) will give you the character '3', so check for that:
    Code:
    int answer = getche();
    char pi = '3';
    if (answer != pi)

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    /facepalm

    It's the littlest mistakes... it does work now, thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  2. What doesn't this work.
    By bobbelPoP in forum C++ Programming
    Replies: 11
    Last Post: 08-11-2008, 02:31 PM
  3. why doesn't work
    By maro009 in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 04:33 AM
  4. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM
  5. do-while doesn't work
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 12-25-2001, 07:05 PM