7 Replies - 8599 Views - Last Post: 04 February 2011 - 07:56 PM Rate Topic: -----

#1 mohd zahirin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-February 11

Basic Calculator in C programming

Posted 04 February 2011 - 12:31 AM

My assignment is to do C programming for basic calculator which includes arithmetic operation(+,-,*,/).By using if...else statement.Here is my code that I tried to do but still have mistakes.
int main()
{
int num1,num2;
printf("enter first number");
scanf("%d",&num1);
printf("enter second number");
scanf("%d",&num2);
printf("choose arithmetic operation[+,-,*,/]");
scanf(???);
so, what should I put in scanf..??& also what are the if...else statement that I should do..??

Is This A Good Question/Topic? 0
  • +

Replies To: Basic Calculator in C programming

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Basic Calculator in C programming

Posted 04 February 2011 - 12:41 AM

What you need to do is declare a char variable and after asking what arithmetic operation is needed, that char variable should hold the symbol. For example:

char operation;
printf("What arithmetic operation is needed?")
scanf("%c", &operation );



The if-else statement on the other hand will check what operation is needed to be done.

if(operation=='+')
{
add here
}
else if(operation=='-')
{
subtract here
}
...



I'm not sure if you can save the operators into a char variable, but if that doesn't work, make your printf say "Press (1) for addition..." then on the if-else statement substitute 1 for +.

That's it.
Was This Post Helpful? 2
  • +
  • -

#3 mohd zahirin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-February 11

Re: Basic Calculator in C programming

Posted 04 February 2011 - 12:51 AM

View PostfromTheSprawl, on 04 February 2011 - 12:41 AM, said:

What you need to do is declare a char variable and after asking what arithmetic operation is needed, that char variable should hold the symbol. For example:

char operation;
printf("What arithmetic operation is needed?")
scanf("%c", &operation );



The if-else statement on the other hand will check what operation is needed to be done.

if(operation=='+')
{
add here
}
else if(operation=='-')
{
subtract here
}
...



I'm not sure if you can save the operators into a char variable, but if that doesn't work, make your printf say "Press (1) for addition..." then on the if-else statement substitute 1 for +.

That's it.


first of all, thanks for replying my post..may I ask something..??
why should I put { and } brackets at if...else statement before I don't finished my programming..??Usually, I put this { and this } at first & last...
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Basic Calculator in C programming

Posted 04 February 2011 - 01:04 AM

Delimiters(the brackets) are not just used to know where the program starts and ends. It can be also used with if statements, functions and for loops. It is very helpful in grouping the processes you want to do if a certain condition is met. If I try to write it like this:


if(a==B)/>
printf...
else if(a==c)
printf...
printf...
printf...



you would be confused on what lines of code will execute on what condition. By using curly braces you are given a sense of organization by knowing what lines of code are grouped. :)
Was This Post Helpful? 1
  • +
  • -

#5 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Basic Calculator in C programming

Posted 04 February 2011 - 01:19 AM

Anyway I tried coding it myself, and it turns out that if you use
scanf("%c", &operation );


something goes wrong. Use this instead:
scanf("%s", &operation );


I changed the %c with %s.

Anyway, did it work?

---

Anyone please check out this code, I took up C eons ago and when I run this program, the x value becomes 0.

What gives?
Spoiler

This post has been edited by fromTheSprawl: 04 February 2011 - 01:22 AM

Was This Post Helpful? 0
  • +
  • -

#6 mohd zahirin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-February 11

Re: Basic Calculator in C programming

Posted 04 February 2011 - 04:25 AM

View PostfromTheSprawl, on 04 February 2011 - 01:19 AM, said:

Anyway I tried coding it myself, and it turns out that if you use
scanf("%c", &operation );


something goes wrong. Use this instead:
scanf("%s", &operation );


I changed the %c with %s.

Anyway, did it work?

---

Anyone please check out this code, I took up C eons ago and when I run this program, the x value becomes 0.

What gives?
Spoiler


sorry, what different actually between %c and %s..??
%c from my lecturer notes means a character..%s refer to..??
this actually my next semester's subject..I take it because it does not has pre-require subject..so, it diffuclt to me to understand this subject because I miss my 1'st class..hope, you will help me to understand it...
Was This Post Helpful? 0
  • +
  • -

#7 mohd zahirin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-February 11

Re: Basic Calculator in C programming

Posted 04 February 2011 - 11:10 AM

yeah, right..this time the program turn to be zero(0) for num2 right..??

This post has been edited by mohd zahirin: 04 February 2011 - 11:11 AM

Was This Post Helpful? 0
  • +
  • -

#8 kaye143  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 08-January 11

Re: Basic Calculator in C programming

Posted 04 February 2011 - 07:56 PM

View Postmohd zahirin, on 04 February 2011 - 04:25 AM, said:

View PostfromTheSprawl, on 04 February 2011 - 01:19 AM, said:

Anyway I tried coding it myself, and it turns out that if you use
scanf("%c", &operation );


something goes wrong. Use this instead:
scanf("%s", &operation );


I changed the %c with %s.

Anyway, did it work?

---

Anyone please check out this code, I took up C eons ago and when I run this program, the x value becomes 0.

What gives?
Spoiler


sorry, what different actually between %c and %s..??
%c from my lecturer notes means a character..%s refer to..??
this actually my next semester's subject..I take it because it does not has pre-require subject..so, it diffuclt to me to understand this subject because I miss my 1'st class..hope, you will help me to understand it...

%s is string that accepts variable with letters and numbers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1