Calculator Help!

This is a discussion on Calculator Help! within the C Programming forums, part of the General Programming Boards category; I made a simple adding calculator in C and when I use it the sum is off by like 500000. ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    12

    Post Calculator Help!

    I made a simple adding calculator in C and when I use it the sum is off by like 500000. Here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int a,b,sum;
      printf ("please type two numbers:\n");
      sum=a+b;
      scanf ("%d,%d",&a,&b);
      printf ("sum=%d",sum);
      system("PAUSE");	
      return 0;
    }
    BTW:Im using dev-c++.
    Last edited by jdude; 10-01-2004 at 10:14 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shouldn't you call scanf before you sum up your values? Oh, and go back and read the Announcements, and then fix your post and all future posts.

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

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    12
    I did what you said and it still doesnt work.

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    fixed code :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int a,b,sum;
      printf ("please type two numbers:\n");
      scanf ("%d%d",&a,&b);
      sum = a + b;
      printf ("sum=%d \n\n",sum);
      system("PAUSE");	
      return 0;
    }
    as quzah said , you should call scanf before summing your values. You've also seperated the two %d's in scanf with a comma.
    Last edited by Brain Cell; 10-01-2004 at 10:47 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    12

    Thumbs up

    thanks,that fixed it.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21