Thread: begginer: having trouble with "if" command.

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    Unhappy begginer: having trouble with "if" command.

    Im 15 years old and just starting out with programming. I was wondering if anyone could help a begginer out with a simple code. Basicly im adding 2 integers together, 1 is already set and the other i get from the user. If the 2 integers are equal i want a certain message to dislplay, but for some reason when i use "if" it just displays the message regardless of the value. It also will not add. Here is the code.

    [code]

    #include <stdio.h>
    #include <stdlib.h>

    int one(int a, int b,int ans);
    int a;
    int b;
    int ans;
    char cnuma[20];


    int main(int argc, char *argv[])
    {
    b=15;
    printf("Hello, my age is %i years old, what is your age?\n", b);
    gets(cnuma);
    a=atoi(cnuma);
    one(a,b,ans);


    }

    int one(int a, int b, int ans)
    {
    ans=a+b;
    if(b=a)
    {
    printf("You say your age is %i? Hey we are the same age!!!\n",a);
    printf("This gives us the combined age of %i\n", a);
    system("pause");
    return 0;
    }
    printf("You say your age is %i, this makes are combined ages %i.\n", a, ans);
    system("pause");
    return 0;

    }

    [code]

    when i compile and run the program without the message it will state the age of the person and the combined ages like its sopposed to. when i compile it with the "if" in there it says the "if" message no matter what and instead of adding the 2 numbers it just repeats the second. Thanks for the help.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > if(b=a)
    Is b equal to a? It is now, you used the assignment operator (=) instead of the equality operator (==).

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    i c, thanks.
    ~matt~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with "touch" command in c program
    By Moony in forum C Programming
    Replies: 10
    Last Post: 08-01-2006, 09:56 AM
  2. Replies: 5
    Last Post: 04-10-2006, 03:03 PM
  3. in program command console
    By howzer in forum C++ Programming
    Replies: 11
    Last Post: 03-19-2006, 03:12 PM
  4. Command Interface : Black Box testing
    By GlowinCelica in forum C Programming
    Replies: 1
    Last Post: 03-10-2003, 10:21 PM
  5. CLI (Command Line Interperator)
    By Mark in forum C Programming
    Replies: 3
    Last Post: 08-31-2001, 06:21 AM