Thread: addition problem

  1. #1
    Jake2k
    Guest

    addition problem

    I'm just learning C and I have this simple code that is suppose to find the sum of two integers.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
       int integer1, integer2, sum;  
       
       printf("Enter the first integer\n");
       scanf("%d", &integer1);
       printf("Enter second integer\n");
       scanf("%d", &integer1);
       sum = integer1 + integer2;
       printf("Sum is %d\n", sum);
             
       getch();
        
    }

    Whenever I add it into the Dev C++ compiler, the output looks something like this...


    Enter the first integer
    5
    Enter second integer
    5
    Sum is 7601673


    Can someone please tell me what I'm not doing to get the answer to be 10? Thanks.


    [edit]Code tags added by Hammer.

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    try passing the reference instead of the variable itslef.

    sum = &integer1 + &integer2 ;
    Monday - what a way to spend a seventh of your life

  3. #3
    Jake2k
    Guest
    That didn't work. It gave me the error message "invalid operands `int *' and `int *' to binary `operator +' ".

    Do you think it could be the compiler settings? This code should work without problems.

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    You could try something like

    Code:
    #include <stdio.h>
    
    int main(void) 
    {
       
    	int num1, num2;
    	
    	printf("Enter num1: ");
    	scanf("%d", &num1);
    	
    	printf("Enter num2: ");
    	scanf("%d", &num2); 
    	
    	printf("\n%d + %d = %d\n", num1, num2, (num1+num2));
    	
    	
    
    return 0;
    }
    Or you might wanna set sum = 0 before using it since it might contain a garbage value.
    Last edited by ronin; 10-12-2002 at 02:30 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by iain
    try passing the reference instead of the variable itslef.

    sum = &integer1 + &integer2 ;
    err... no, don't do that.

    The problem is actually a simple typo. You asked for two values from the user, but stored both in the same variable. Look at the 2 scanf() calls, both have integer1 in them.

    Please use code tags when posting code too. See my signature for further help (or just ask me).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Jake2k
    Guest

    fixed

    Hey, Hammer, thanks. I don't know how or why I did that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. String Addition Problem...
    By Comrade_Yeti in forum C++ Programming
    Replies: 16
    Last Post: 01-15-2007, 04:01 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM