Like Tree1Likes
  • 1 Post By mike65535

need help with calculator code for c

This is a discussion on need help with calculator code for c within the C Programming forums, part of the General Programming Boards category; I need to create a calculator that will read the operator first followed by a space the first number and ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    10

    need help with calculator code for c

    I need to create a calculator that will read the operator first followed by a space the first number and then another space the second number. So the user input should look like this
    + 8 8
    then output it like this
    8 + 8 = 16
    This is what i have so far, I was wondering if i was on the right track and if not some tips please.

    insert
    Code:
    #Include <stdio>
    void main (void){
    
    int sum, num1, num2, i;
    double symbol;
    
    
    
    printf("Enter Operator, Number 1, Number 2:\n");
    scanf("%d",&symbol, &num1, &num2);
    
    
    if (symbol = * ) {                   
    	for (i = 0; i < num1 ; i++){
    		sum = sum + num2;
    }
    	printf("%d", num1, * , num2, = , sum); 
    }
    
    else if (symbol = -){
    	num1 - num2 = sum;
    
    	printf("%d", num1, - , num2, =, sum);
    }
    
    else if (symbol = +){
    	num1 + num2 = sum;
    
    	printf("%d", num1, +, num2, =, sum);
    
    }
    
    
    
    
    else{
    printf("Selection is Invalid");

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I think you may want to look up how C operators such as * + - / etc work in your c texts.
    You have your assignments backwards and it's going to produce a ton of compiler errors.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    char symbol;
    scanf("%c %d %d", &symbol, &num1, &num2);
    if (symbol == '*')
    sum = num1 + num2

    Lots of other minor issues.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Lots of other minor issues.
    Are we on a roll today, or what... twice now we've answered within a minute of eachother...

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by CommonTater View Post
    Are we on a roll today,
    If you were butter maybe. ;-)
    CommonTater likes this.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by CommonTater View Post
    Are we on a roll today, or what... twice now we've answered within a minute of eachother...
    Slow day at work.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Slow day at work.
    Currently between projects...

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    okay so i changed somethings around...will this work or do i have to make the options cases?

    insert
    Code:
    #Include <stdio>
    void main (void){
    
    int sum, num1, num2, i;
    char symbol;
    
    
    
    printf("Enter Operator, Number 1, Number 2:\n");
    scanf("%c %d %d",&symbol, &num1, &num2);
    
    
    if (symbol== '*' ) {                   
    	for (i = 0; i < num1 ; i++){
    		sum = sum + num2;
    }
    	printf("%d", num1, * , num2, = , sum); 
    }
    
    else if (symbol== '-'){
    	num1 - num2 = sum;
    
    	printf("%d", num1, - , num2, =, sum);
    }
    
    else if (symbol== '+'){
    	num1 + num2 = sum;
    
    	printf("%d", num1, +, num2, =, sum);
    
    }
    else if (symbol == '/'){
    
    }
    
    else if (symbol== 'e'){
    
    }
    
    
    
    
    else{
    printf("Selection is Invalid"); 
    
    }
    
    
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First of all... have you tried compiling your code? What errors did you get?

    C is case sensitive... #Include will not work... #include will.
    C knows how to multiply... for your multiply function just use sum = num1 * num2;

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Code:
    num1 + num2 = sum;
    There's no way you've compiled with stuff like this.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    Code:
    num1 + num2 = sum;
    There's no way you've compiled with stuff like this.
    That was sort of my point... he's gonna get a whole mess of errors and hopefully he'll climb into the books and learn some about C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator
    By TonyP in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 10:14 PM
  2. calculator
    By denny in forum Game Programming
    Replies: 5
    Last Post: 06-25-2002, 09:44 AM
  3. A C++ calculator
    By smokeybear in forum C Programming
    Replies: 8
    Last Post: 06-06-2002, 07:22 AM
  4. IP calculator
    By shoots in forum C Programming
    Replies: 1
    Last Post: 04-11-2002, 11:56 PM
  5. code for hexadecimal calculator
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-20-2002, 08:53 PM

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