Help About a simple calculator pls!!

This is a discussion on Help About a simple calculator pls!! within the C Programming forums, part of the General Programming Boards category; hello all!! I really need a little help! My problem is that I am writing a simple calculator which is ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    Help About a simple calculator pls!!

    hello all!! I really need a little help! My problem is that I am writing a simple calculator which is actually my homework and it must do calculation like 2*4+5 is 13..that is there is no precedence..But I dont know how I can write +5 after first equ( 2*4)..My program just gives 8 as a result..Pls help me!! what ı wrote::
    Code:
        int main(){
        int a,b,sum,c;
        char ch;
        printf("enter your application\n");
        scanf("%d%c%d",&a,&ch,&b);
        while(ch!='!' ){
            if(ch=='+')
            sum=a+b;
            if(ch=='-')
            sum=a-b;
            if(ch=='/')
            sum=a/b; 
            if(ch=='*') 
            sum=a*b;
          
             scanf("%c%d",&ch,&c);  
                 printf("%d",sum);
        anykey();
        return 0;
    }
    Last edited by Salem; 11-14-2007 at 04:10 PM. Reason: Code tags go around, you guessed it, just the CODE!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Read the first int, then everything after that is 'operation' and 'operand' pairs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Why don't you assign sum = a before the while() loop? Then you can keep a running total each time through the loop:
    Code:
            if(ch=='+')
                sum += b;
            if(ch=='-')
                sum -= b;
            if(ch=='/')
                sum /= b; 
            if(ch=='*') 
                sum *= b;

  4. #4
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
            if(ch=='+')
                sum += b;
            if(ch=='-')
                sum -= b;
            if(ch=='/')
                sum /= b; 
            if(ch=='*') 
                sum *= b;
    Consider this. If you want an addition ( the first if ); the others if are still going to be checked for truthfulness. If else or switch are better alternatives.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    1
    Are you also considering in your program when the input of the user is not a single digit? what if it isdouble digit or more?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by aaron_88 View Post
    hello all!! I really need a little help! My problem is that I am writing a simple calculator which is actually my homework and it must do calculation like 2*4+5 is 13..that is there is no precedence..But I dont know how I can write +5 after first equ( 2*4)..My program just gives 8 as a result..Pls help me!! what ı wrote::
    Code:
        int main(){
        int a,b,sum,c;
        char ch;
        printf("enter your application\n");
        scanf("%d%c%d",&a,&ch,&b);
        while(ch!='!' ){
            if(ch=='+')
               sum=a+b;
            if(ch=='-')
               sum=a-b;
            if(ch=='/')
               sum=a/b; 
            if(ch=='*') 
               sum=a*b;
          
             scanf("%c%d",&ch,&c);  
                 printf("%d",sum);
        } /* maybe here? */
        anykey();
        return 0;
    }
    I've highlighted the curly brace problem (a typo I suspect).
    Your program logic doesn't DO anything if int c is added in. It just prints up the same sum
    it had before.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    Consider a switch instead of that if.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    I would probably read the entire line in, and just increment through it until you find a +,-,/,* sign then take everything before that as number1. Store the sign. Read to the next sign(to get number2), do your math and store that as number1. Repeat.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

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