Thread: Getting float result to appear

1. Getting float result to appear

Hi guys,

I am trying to program the following type calculator:

Welcome to <John Doe’>s Handy Calculator

2. Subtraction
3. Multiplication
4. Division
5. Exit

What would you like to do? 3

Please enter two numbers to multiply separate by a space: 24 6
Result of multiplying 24.00 and 6.00 is 96.00.
Press any key to continue …
..........................................

I am stuck on the "Result....." line. First off I can't get the result to appear as a float (or at all for that matter). I am sure it is something basic I am doing incorrectly. Also I can't figure out how to place the "and" between the 2 numbers in the printf() statement.
I am currently testing it under Case 1 in the code as you can see.

You don't have to do it for me, just a push in the right direction would be appreciated Thanks

Code:
```#include <stdio.h>

int main()

{
int value;
int num1,num2;
float result;

num1=0;
num2=0;
result=0;

printf("Welcome to xxxxxxx's handy calculator\n");
printf("      2. Subtraction\n");
printf("      3. Multiplication\n");
printf("      4. Division\n");
printf("      5. Exit\n");
printf("What would you like to do?");
scanf("%d",&value);

if(value>5 || value<1)
{
printf("You have entered an incorrect number");
}

switch (value)
{
case 1:
scanf("%d %d",&num1,&num2);
printf("Result of adding %d+%d is: %2.2f",num1, num2, result);
break;
case 2:
printf("Please enter two numbers to subtract separate by a space: ");
break;
case 3:
printf("Please enter two numbers to multiply separate by a space: ");
break;
case 4:
printf("Please enter two numbers to divide separate by a space: ");
break;

}

return 0;
}```

2. after looking over the assignment again, it appears the scanf() input should allow the use of int or float type numbers. Hmmm, looks like I need to clean that part up as well....

3. Code:
```float num1;
float num2;

case 1:
scanf("%f %f",&num1,&num2);
printf("Result of adding %6.2f and %6.2f is: %7.2f",num1, num2, num1 + num2);
break;```

4. Thank you Tater,

I actually figured it out before I saw your response, though I did it slightly different but it appears to compile fine. I realized I needed to assign my value as Int, but my num1 and num2 as float....

Code:
```float num1,num2;

case 1:
scanf("%f %f",&num1,&num2);
printf("The result is %.2f\n", num1+num2);
break;```

5. It's not a huge issue in this program, but it would be a good idea to get into the habit of explicitly defining all your cases as well as a default case to catch error. When you start doing more complicated programs it will make it easier for you to debug, and for other people to understand.

Code:
```case 5:
break;      //explicit do-nothing case for the "exit" option
default:
printf( "You're not supposed to be able to get here!!\n";
break;```

6. I advise against that in this instance. Only add a default case either when you actually need it, OR when it would be catastrophic if it were to ever not hit one of the other cases. In the above program nothing bad would happen as a result of not being equal to one of the provided cases.
If you were to have something like this:
Code:
```Foo *foo; // may or may not initialise this...
switch (bar)
{
case 1: foo = new foobar(); break;
case 2: foo = new barfoo(); break;
}
foo->doMyFoo(); // Boom!```
Then a default case which asserts (and in the case of C++ perhaps also throws) would be a good idea, and is sufficient.

Every line of code you have has the potential to contain one or more bugs. The less unnecessary code you have, the more important code you can see at once on your screen which is more helpful to comprehend the code.