# simple calculator problem...

• 09-26-2011
yeohwl91
simple calculator problem...
below is my calculator programming.I want to make it show "maths error" when any number divided by 0, but i get any number +,-,*,/ by 0 also get maths error.I need u guys to correct my program,please.
Code:

```#include <stdio.h> int main() {     float num1, num2;     char operation;     while (1) {         scanf("%f%c%f", &num1, &operation, &num2);         if (operation == '+')             printf("%f\n", num1+num2);         if (operation == '-')             printf("%f\n", num1-num2);         if(operation == '*')             printf("%f\n", num1*num2);         if (num2 == 0)         {             printf ("maths error\n");         }         else if (operation == '/')             printf("%f\n", num1/num2);     }     return 0; }```
• 09-26-2011
laserlight
Think: this maths error only applies if operation is equal to '/'.
• 09-26-2011
iceaway
You need to look at how the if statements are evaluated. The way your code looks now, it will always check if num2 is equal to zero, even if you are using the +, - or * operator.

What you want to do is probably something like:
Code:

```if (operation == '+') {     /* do + stuff */ } else if (...) {     /* .... */ } else if (operation == '/') {     /* check for division by zero */ }```
If you don't want all those if-statements lining up, you could look into using a switch-statement instead for checking the operator.
• 09-26-2011
yeohwl91
iceaway, thanks so much!i got it!
Code:

```#include <stdio.h> int main() {     float num1, num2;     char operation;     while (1) {         scanf("%f%c%f", &num1, &operation, &num2);         if (operation == '+')             printf("%f\n", num1+num2);         else if (operation == '-')             printf("%f\n", num1-num2);         else if(operation == '*')             printf("%f\n", num1*num2);         else if (num2 == 0)         {             printf ("maths error\n");         }         else if (operation == '/')             printf("%f\n", num1/num2);     }     return 0; }```
now i want my calculator to show in 2 decimal place, what command should i put?
• 09-26-2011
laserlight
Although that it logically correct, it would be easier to understand if you wrote it in this way:
Code:

```if (operation == '+')     printf("%f\n", num1+num2); else if (operation == '-')     printf("%f\n", num1-num2); else if(operation == '*')     printf("%f\n", num1*num2); else if (operation == '/') {     if (num2 == 0)     {         printf ("maths error\n");     }     else     {         printf("%f\n", num1/num2);     } }```
Quote:

Originally Posted by yeohwl91
now i want my calculator to show in 2 decimal place, what command should i put?

Read up on printf, in particular, how to modify the format string to format such numbers.
• 09-26-2011
Rodaxoleaux
• 09-26-2011
yeohwl91
help me to check whether my flowchart have problem?
thanks...

Code:

```#include <stdio.h> int main() {     float num1, num2;     char operation;     while (1) {         printf ("Enter Calculation:\n");         scanf("%f%c%f", &num1, &operation, &num2);         if (operation == '+')             printf("%.2f%c%.2f=%.2f\n",num1,operation,num2, num1+num2);         else if (operation == '-')             printf("%.2f%c%.2f=%.2f\n",num1,operation,num2, num1-num2);         else if(operation == '*')             printf("%.2f%c%.2f=%.2f\n",num1,operation,num2, num1*num2);         else if (num2 == 0)         {             printf ("maths error\n");         }         else if (operation == '/')             printf("%.2f%c%.2f=%.2f\n",num1,operation,num2, num1/num2);     }     return 0; }```
• 09-26-2011
Can't, it's too small to read.
• 09-26-2011
yeohwl91
i have no idea how to make it bigger, because i just print screen from the microsoft word. please download below attachment and help me check.

Attachment 10913
• 09-26-2011
yeohwl91
i not really understand what is pseudo code, but i try to write it. Help me check whether correct or not, thanks a lot.
Code:

```input (mathematical expression) if calculation is not division then go to the calculating and round off value into 2 decimal places if calculation is division then check whether denominator is zero else if denominator is zero then show "maths error" else if denominator is not zero then go to the calculating and round off value into 2 decimal places if end output (show answer)```
• 09-26-2011
Your attachment attempt #2 doesn't work "Invalid attachment".

Your pseudo code should have indentation (like your code should have), on all subordinate lines of code.

The amount that a line is indented from a previous line should never by more than 2 to 5 spaces. (and spaces work more reliably on the board, then tabs do).

code tags - use 'em always for code and pseudo code. highlight your code, and in the advanced editor, click on the # icon near the upper right hand corner of the editor.

[code ]
Code:

``` if(you don't have code tags and indentation)   printf("Your code will be hard to read/study\n");```
[/code ]

Code tags are just like the what you see above EXCEPT they don't have a space after the e.
• 09-26-2011
yeohwl91
Code:

```scanf (mathematical expression) if (calculation is not division)then (go to the calculating and round off value into 2 decimal places) if (calculation is division) then (check whether denominator is zero) else if (denominator is zero) then (show "maths error") else if (denominator is not zero) then (go to the calculating and round off value into 2 decimal places) if end printf (show answer)```
• 09-26-2011
Quote:

Originally Posted by yeohwl91
Code:

```scanf (mathematical expression) if (calculation is not division)then (go to the calculating and round off value into 2 decimal places) if (calculation is division) then (check whether denominator is zero) else if (denominator is zero) then (show "maths error") else if (denominator is not zero) then (go to the calculating and round off value into 2 decimal places) if end printf (show answer)```

The above isn't pseudo code for your program. Just paraphrase your actual program, line by line.

Don't rearrange the order of the logic.
• 09-27-2011
yeohwl91
Code:

```print out Enter Calculation scan the maths expression if operation is addition then print out number 1+number 2=answer else if operation is substraction then print out number 1-number 2=answer else if operation is multiplication then print out number 1*number 2=answer else if number 2 is zero then print maths error else if operation is division then print out number 1/number 2=answer```

How to write pseudo code for "#include <stdio.h>, int main (), float, char operation,while (1) and return 0?
• 09-27-2011
AndrewHunter
Quote:

Originally Posted by yeohwl91
How to write pseudo code for "#include <stdio.h>, int main (), float, char operation,while (1) and return 0?

This is getting old. C Made Easy Tutorials. Start reading.