Thread: sum problem

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21

    sum problem

    Code:
    #include <stdio.h>
    main()
    {
    int a,b,c;
    c=a+b;
    printf("give first number:");
    scanf("%d",&a);
    printf("give second number:");
    scanf("%d",&b);
    printf("the result is:");
    printf("%d",c);
    }
    but when i give for first number 4 and for second number 7 my result is not 11.
    the problem is propably the last printf.what is the correct syntax for valid result??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The order you do things in C is important. The value of c is calculated before the input of a and b, so what value do you think c will have?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    Code:
    #include <stdio.h>
    main()
    {
    char operator;
    int a,b,c;
    printf("give the operator:");
    scanf("operator");
    if (operator='+')
    {
         printf("give first number:");
         scanf("%d",&a);
         printf("give second number:");
         scanf("%d",&b);
         c=a+b;
         printf("the result is:");
         printf("%d",c);
         printf("\n");
    }
    else if (operator='-')
    {
         printf("give first number:");
         scanf("%d",&a);
         printf("give second number:");
         scanf("%d",&b);
         c=a-b;
         printf("the result is:");
         printf("%d",c);
         printf("\n");
    }
    else if (operator='*')
    {
         printf("give first number:");
         scanf("%d",&a);
         printf("give second number:");
         scanf("%d",&b);
         c=a*b;
         printf("the result is:");
         printf("%d",c);
         printf("\n");
    }
    else if (operator='/')
    {
         printf("give first number:");
         scanf("%d",&a);
         printf("give second number:");
         scanf("%d",&b);
         c=a/b;
         printf("the result is:");
         printf("%d",c);
         printf("\n");
    }
       }
    run it and plz tell me what's going wrong because is difficult for me to explain the problem in english.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    Code:
    scanf("operator");
    I think you made some typos here.

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    we have writen scanf("operator"); too.what is wrong???

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    Look at your other usages of scanf(). See how they are assigning a value to a variable? Now look at the usage I quoted. What is it doing?

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sk8harddiefast View Post
    Code:
    #include <stdio.h>
    main()
    {
    char operator;
    int a,b,c;
    printf("give the operator:");
    scanf("operator");
    if (operator='+')
    {
         printf("give first number:");
         scanf("%d",&a);
         printf("give second number:");
         scanf("%d",&b);
         c=a+b;
         printf("the result is:");
         printf("%d",c);
         printf("\n");
    }
    else if (operator='-')
    {
         printf("give first number:");
         scanf("%d",&a);
         printf("give second number:");
         scanf("%d",&b);
         c=a-b;
         printf("the result is:");
         printf("%d",c);
         printf("\n");
    }
    else if (operator='*')
    {
         printf("give first number:");
         scanf("%d",&a);
         printf("give second number:");
         scanf("%d",&b);
         c=a*b;
         printf("the result is:");
         printf("%d",c);
         printf("\n");
    }
    else if (operator='/')
    {
         printf("give first number:");
         scanf("%d",&a);
         printf("give second number:");
         scanf("%d",&b);
         c=a/b;
         printf("the result is:");
         printf("%d",c);
         printf("\n");
    }
       }
    run it and plz tell me what's going wrong because is difficult for me to explain the problem in english.
    I would llike to suggest a lot of things in your code:
    1. scanf takes the address of the variable and syntax is similar to printf.
    2. You are asking the user to input a and b in each case. It would have been better to ask for a and b before asking for the opeartor. This way you would have saved time in typing the code.
    3. Instead of if and else if, I would suggest to use switch-case(if you have studied it) here.
    4. return int explicitly from main.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  2. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 05:43 PM
  3. ?? with trying to sum numbers
    By Babs21 in forum C Programming
    Replies: 1
    Last Post: 09-16-2007, 03:58 PM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM