Thread: Beginner Error

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    Beginner Error

    I've just started the C programming tutorial and I'm trying to modify the code to practise what I've learnt. This is what I have:

    Code:
    #include <stdio.h>
    
    int main()
    {
      int num1, num2, ans1;
      printf( "Insert a number pl0x: " );
      scanf("%d", &num1);
      printf("Insert another number pl0x: ", &num2);
      scanf("%d", &num2);
      ans1 = num1 + num2;
      printf("The two numers entered added together are: %d", &ans1);
      getchar();
      return 0;
    }
    No matter what number I enter, I get the same (wrong response):

    Code:
    Insert a number pl0x: 3
    Insert another number pl0x: 3
    The two numers entered added together are: 2293580
    I think it has something to do with the last printf statement. Can anyone point me into the right direction?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by SvenRL View Post
    I think it has something to do with the last printf statement. Can anyone point me into the right direction?
    For what it's worth, here's my 2c.
    Code:
    #include <stdio.h>
    
    int main()
    {
      int num1, num2, ans1;
      printf( "Insert a number pl0x: " );
      scanf("%d", &num1);
      printf("Insert another number pl0x: ", &num2);                   /* why do you need &num2 here?? */
      scanf("%d", &num2);
      ans1 = num1 + num2;
      printf("The two numers entered added together are: %d", &ans1);  /* print the contents of ans1 not its location */
      getchar();                                                       /* why do you need to read more stuff?? */
      return 0;
    }
    Last edited by itCbitC; 07-03-2009 at 06:59 PM.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    Quote Originally Posted by itCbitC View Post
    For what it's worth, here's my 2c.
    Code:
    #include <stdio.h>
    
    int main()
    {
      int num1, num2, ans1;
      printf( "Insert a number pl0x: " );
      scanf("%d", &num1);
      printf("Insert another number pl0x: ", &num2);                   /* why do you need &num2 here?? */
      scanf("%d", &num2);
      ans1 = num1 + num2;
      printf("The two numers entered added together are: %d", &ans1);  /* print the contents of ans1 not its location */
      getchar();                                                       /* why do you need to read more stuff?? */
      return 0;
    }
    Thank you for the quick response and constructive criticism. I'm guessing my mistake is putting the num2 into the printf statement. I don't need it there.

    so....

    What I seem to be doing wrong is the mistake mentioned above and the fact that I'm printing the location of ans1 and not the contents of.

    The getchar() at the end of my program is simply to prevent the DOS window from closing. Is that not the write way to do it? It's the only way I know of. Suggestions would be great.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The red highlight was showing you what was wrong. You only use & if you need the address of a variable, rather than the value the variable contains. Typically with printf you don't need an address, but a value.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by SvenRL View Post
    The getchar() at the end of my program is simply to prevent the DOS window from closing. Is that not the write way to do it? It's the only way I know of. Suggestions would be great.
    Here are a list of methods to stop the console from closing.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User Automatik's Avatar
    Join Date
    Jun 2009
    Posts
    1
    You're getting the wrong value because you're using &(address of operator) which is not needed in the case of using printf()

    the getchar() isn't needed either, I assume you're on Windows and running the program by double clicking.. you could remove the getchar() and launch the program from cmd (Start>Run>cmd) then cd to your working dir and type your programs name

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

Tags for this Thread