Thread: program for calculation

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    program for calculation

    Code:
    #include <stdio.h>
    #include <string.h>
    int add(int a, int b){
     int ans;
     ans=a+b;
     return ans;
    }
    int mul(int a, int b){
     int ans;
     ans=a*b;
     return ans;
    }
    int sub(int a, int b){
     int ans;
     ans=a-b;
     return ans;
    }
    int div(int a, int b){
     int ans;
     ans=a/b;
     return ans;
    }
    void main(){
     char choice[20];
     int ans;
     int a,b;
     scanf("%s",&choice);
     if(choice=="add")
     {
      scanf("%d %d",&a,&b);
      ans=add(a,b);
      printf("%d",ans);
     }
     else if(choice=="sub")
     {
      scanf("%d %d",&a,&b);
      ans=sub(a,b);
      printf("%d",ans);
     }
     else if(choice=="mul")
     {
      scanf("%d %d",&a,&b);
      ans=mul(a,b);
      printf("%d",ans);
     }
     else if(choice=="div")
     {
      scanf("%d %d",&a,&b);
      ans=div(a,b);
      printf("%d",ans);
     }
     else
      printf("Wrong input");
    }
    i don't know what the problem is.thanks for help

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The first problem is that you didn't bother to indent and space your code in any meaningful way.

    The second problem is that you cannot compare strings across the == sign in C. C doesn't know about strings, so you have to use library functions such as strcmp() to do these things.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Thanks for you help but i don't know how to use strcmp() to do it. i know the function of strcmp() which is used to comparing the number of char in the strings. can you give me some ideas?

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by tommytmh View Post
    Thanks for you help but i don't know how to use strcmp() to do it. i know the function of strcmp() which is used to comparing the number of char in the strings. can you give me some ideas?
    strcmp

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by tommytmh View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    int add(int a, int b){
     int ans;
     ans=a+b;
     return ans;
    }
    int mul(int a, int b){
     int ans;
     ans=a*b;
     return ans;
    }
    int sub(int a, int b){
     int ans;
     ans=a-b;
     return ans;
    }
    int div(int a, int b){
     int ans;
     ans=a/b;
     return ans;
    }
    void main(){
     char choice[20];
     int ans;
     int a,b;
     scanf("%s",&choice);
     if(choice=="add")
     {
      scanf("%d %d",&a,&b);
      ans=add(a,b);
      printf("%d",ans);
     }
     else if(choice=="sub")
     {
      scanf("%d %d",&a,&b);
      ans=sub(a,b);
      printf("%d",ans);
     }
     else if(choice=="mul")
     {
      scanf("%d %d",&a,&b);
      ans=mul(a,b);
      printf("%d",ans);
     }
     else if(choice=="div")
     {
      scanf("%d %d",&a,&b);
      ans=div(a,b);
      printf("%d",ans);
     }
     else
      printf("Wrong input");
    }
    i don't know what the problem is.thanks for help
    Use strcmp() for comparing strings.
    You are comparing addresses of the strings instead of strings.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Code:
    #include <stdio.h>
    #include <string.h>
    int add(int a, int b){
     int ans;
     ans=a+b;
     return ans;
    }
    int mul(int a, int b){
     int ans;
     ans=a*b;
     return ans;
    }
    int sub(int a, int b){
     int ans;
     ans=a-b;
     return ans;
    }
    int div(int a, int b){
     int ans;
     ans=a/b;
     return ans;
    }
    void main(){
     char choice[20];
     char addd[20]="add",subb[20]="sub",mull[20]="mul",divv[20]="div";
     int ans;
     int a,b;
     scanf("%s",&choice);
     if(strcmp(choice,add)==0)
     {
      scanf("%d %d",&a,&b);
      ans=add(a,b);
      printf("%d",ans);
     }
    Do you mean by this?Thanks

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tommytmh View Post
    Thanks for you help but i don't know how to use strcmp() to do it. i know the function of strcmp() which is used to comparing the number of char in the strings. can you give me some ideas?
    Yep... look up strcmp() in your compiler's library documentation. (If you don't have it get it.)

    There you will find a full description of what it does, what it returns and which headers and libraries you need to use it.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Like this...

    Code:
     scanf("%s",&choice);
     if(strcmp(choice,"add") == 0)
     {
      printf("Enter numbers to add : "); 
      scanf("%d %d",&a,&b);
      ans=add(a,b);
      printf("%d",ans);
     }
    You don't need an array of strings, just compare to the string literal.

    Again... you need to look this stuff up in your compiler's library documentation.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    i've checked the library. if the two strings are exactly equal to each other , it will return 0. Thank you for help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculation in C Program
    By Jpeg6 in forum C Programming
    Replies: 12
    Last Post: 09-17-2010, 11:06 AM
  2. Need help with basic calculation program.
    By StateofMind in forum C Programming
    Replies: 18
    Last Post: 03-06-2009, 01:44 AM
  3. help with pay calculation program
    By shugstone in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 09:38 AM
  4. infix calculation program????
    By yescha in forum C Programming
    Replies: 0
    Last Post: 11-27-2001, 04:32 AM