Thread: Can sumone help me with this piece of code im close to tearing out my hair

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    10

    Unhappy Can sumone help me with this piece of code im close to tearing out my hair

    Hello everyone im new to this forum so id like to use this first post to say hello.
    Anyway im new to C and am currentley learning the basics. I am trying to create a simple program that will take two numbers from the user, add them together and print the result.
    However when i compile and run the below code no matter what inputs i enter the result is always 37814100.

    I have no idea why this is happening and after studying the code can only assume its sumthing wrong with my compiler. Can anyone shed some light on this. thanks for your help


    Code:
    #include <stdio.h>
    int add(int x, int y);
    int main(int argc, char *argv[])
    {
      int w;
      int p;
      int result;
      printf("enter two numbers\n");
      printf("Number 1: ");
      scanf("%d", &w, "\n");
      getchar();
      printf("Number 2: ");
      scanf("%d", &p, "\n");
      getchar();
      result = add( w, p );
      printf("The product of those two values is: %d\n", &result);
      getchar();
    
    }
    
    int add(int x, int y)
    {
    return x*y;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%d", &w, "\n");
    Drop the "\n"

    > printf("The product of those two values is: %d\n", &result);
    Drop the &

    > return x*y;
    Strange definition of 'add'

    Also, jack up the warning levels on your compiler. If it's a gcc port, then you get lots of help with the printf/scanf formats, which can be a real pain for newbies
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:10: warning: too many arguments for format
    foo.c:13: warning: too many arguments for format
    foo.c:16: warning: int format, pointer arg (arg 2)
    foo.c: At top level:
    foo.c:3: warning: unused parameter 'argc'
    foo.c:3: warning: unused parameter 'argv'
    foo.c: In function `main':
    foo.c:19: warning: control reaches end of non-void function
    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.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Thanks for your help it works now. I made the changes you suggested. One question though why do we use the & before the variable in the scanf statements but not printf statements:

    scanf("&#37;d", &p);
    printf("The product of those two values is: %d\n", result);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    C has no idea about references, so if you want to store a result in a variable (like scanf needs to store the result), then you need the address of the variable.

    printf just reads data, so it only needs the current value of a variable.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Thanks for your help. the reaons i was so confused is that cprogramming.com says u need the & in the printf statement. oh well i guess their wrong lol. thanks

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Where does the site say that?

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    oh actually after checking it doesnt. i just changed the code more than i had thought from the original from the site. This is the original code from the cprogramming site:

    Code:
    #include <stdio.h>
    
    int mult ( int x, int y );
    
    int main()
    {
      int x;
      int y;
      
      printf( "Please input two numbers to be multiplied: " );
      scanf( "%d", &x );
      scanf( "%d", &y );
      printf( "The product of your two numbers is %d\n", mult( x, y ) );
      getchar(); 
    }
    
    int mult (int x, int y)
    {
      return x * y;
    }

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Iv modified the code to create a larger program but when trying to compile the code in GCC in mac os x I keep getting the error:
    program.c: In function 'main':
    program.c:43: error: parse error before 'printf'

    My code:

    Code:
    //Written by Karim Hmaissi
    //First C program in a bash shell
    #include <stdio.h>
    int add(int x, int y);
    int subtract(int x, int y);
    int multiply(int x, int y);
    int divide(int x, int y);
    
    main()
    {
    
     int number1 = 0;
     int number2 = 0;
     int result = 0;
     int option;
    
     printf("War gwan please enter two numbers\n");
     printf("Number 1: ");
     scanf("%d", &number1);
     getchar();
    
     printf("Number 2: ");
     scanf("%d", &number2);
     getchar();
    
     printf("\n");
     printf("You have entered ");
     printf("%d", number1);
     printf(" ");
     printf("and ");
     printf("%d", number2);
     getchar();
    
    //menu
     printf("\n");
     printf("Please select an option from below\n");
     printf("What would you like to do to your two numbers\n");
     printf("Option 1 Add numbers\n");
     printf("Option 2 Subtract numbers\n");
     printf("Option 3 Multiply numbers\n");
     printf("Option 4 Divide numbers\n")
    
     printf("Option: ");
     scanf("%d", &option);
     getchar();
    
    
    //menulogic
     if (option == 1)
     {
     result = add(number1, number2);
     }
    
     else if (option == 2)
     {
     result = subtract(number1, number2);
     }
    
     else if (option == 3)
     {
     result = multiply(number1, number2);
     }
    
     else if (option == 4)
     {
     result = divide(number1, number2);
     }
    
     else
     {
     printf("You have entered an incorrect option for your menu choice");
     result = 0;
     }
    
     printf("Your result is ");
     printf("%d", result);
    
    
    }
    
    //Functions
    int add(int x, int y)
    {
    return x + y;
    }
    
    int subtract(int x, int y)
    {
    return x - y;
    }
    
    int multiply(int x, int y)
    {
    return x * y;
    }
    
    int divide(int x, int y)
    {
    return x / y;
    }
    Can someone please help me identify the problem here;

    Thanks for your help

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    printf("Option 4 Divide numbers\n");

    printf("Option: ");
    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.

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    I can’t believe I missed that lol I must read over the code like ten times. Thanks.
    One more quick question is there any difference compiling C source code on windows or Linux when the functions of the OS are not called. For example when the windows API is not used and the Linux equivalent.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Standard ISO C has about a dozen header files (stdio.h, stdlib.h, string.h etc).

    If you stick to using only functions in these files, then your code should be portable to anything which supports an ISO C compiler.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  2. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  3. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  4. What is your favorite piece of code?
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-22-2002, 07:12 AM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM