3 Replies - 1452 Views - Last Post: 18 November 2012 - 11:40 PM Rate Topic: -----

#1 dok300  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-October 12

Basic C calculator

Posted 18 November 2012 - 01:43 PM

Hi

I'm trying to create a simple calculator in C.

So far this is what I've got.


#include<stdio.h>
#include<stdlib.h>

int main(void)

{

char ch;
int a,b;


printf ("Would you like to add,multiply,
subtract or divide? Press,a,m,s or d for your choice");
ch = getchar();

if ('%c'= a) 
{
printf("enter two numbers to add");
scanf("%d%d",a,B)/>;
add_total = a+b;
printf("The answer is %d",add_total);
}

if('%c'= m) 
{
printf("enter two numbers to multiply");
scanf("%d%d",a,B)/>;
mult_total = a*b;
printf("The answer is %d", mult_total;);
}

if
('%c'= s){
printf("enter two numbers to subtract");
scanf("%d%d",a,B)/>;
sub_total = a-b;
printf("The answer is %d",sub_total);
}

if ('%c'= d) {
printf("enter two numbers to divide");
scanf("%d%d",a,B)/>;
div_total = a/b;
printf("The answer is %d",div_total);

}
else{
printf("Not a valid selection");
}
return (0);

}


I am fully aware that there is probably a much easier one line way of doing all this but i want to learn what is wrong with this code.( Of course this is not to say any alternative solutions are not welcomed!)

Any input appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Basic C calculator

#2 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Basic C calculator

Posted 18 November 2012 - 02:07 PM

Lines akin to this one:
if('%c'= m)
What does that even mean? It shouldn't compile, and if it didn't, then you should post complete compile log. ;)
Was This Post Helpful? 2
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Basic C calculator

Posted 18 November 2012 - 03:46 PM

And another that needs to read the manual for scanf.
Was This Post Helpful? 1
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Basic C calculator

Posted 18 November 2012 - 11:40 PM

Ideally,for a basic calculator, you might want to use a switch statement.So basically what you may need are 2 variables for the numbers,and of course, the operator.

So you go about this in the following way:

Ask the user to enter 2 numbers.
Ask the user to provide an operator for these 2 numbers and save it in a char variable say op.
switch(op)
{
case '+' : //code for addition and of course, break;
case '-' : //code for subtraction and of course, break;
}


similarly for the other operators like *,/ and %(modulo)

P.S : As my esteemed DIC members have suggested, you need to look into the syntax of scanf()


regards,
Raghav

This post has been edited by raghav.naganathan: 18 November 2012 - 11:43 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1