Simple calculator program in C / C ++
I am trying to get this program to make it clear to me that when I do a task of addition, subtraction, multiplication, or division, it will give me the answer. However it doesn't work, can anyone please help.
int main ()
{
int choice;
float a, b;
float sum;
float difference;
float product;
float quotiont;
printf("This program adds, subtracts, multiplies, and divides.\n");
printf("**************\n");
printf("* Calculator *\n");
printf("**************\n");
printf("Enter an expression: ");
scanf("%f %f", &a, &b);
scanf("%f %f %f %f", &sum, &difference, &product, "iont);
sum = a + b;
difference = a - b;
product = a * b;
quotiont = a / b;
if(a + b)
printf("Answer = %f\n", &sum);
else if(a - b)
printf("Answer = %f\n", &difference);
else if(a * b)
printf("Answer = %f\n", &product);
else if(a / b)
printf("Answer = %f\n", "iont);
else
printf("Error");
}
What are you trying to accomplish with this line?
scanf("%f %f %f %f", &sum, &difference, &product, "iont);
What it does is take four numbers from the user and load them into four variables, respectively. Right after this line, you are assigning new values to these four variables, so there is no point in loading them with the values in this line of code.
Also, what's the point of the next statement if
? (And all instructions else if
)
if(a + b)
printf("Answer = %f\n", &sum);
This will only display the answer if the sum is a
and is b
nonzero. If the expression inside the parentheses after the "if" is zero, it will not execute the code below. If it evaluates to a nonzero value, it will execute the code.
Another problem with the above line is that you are passing a pointer to a variable sum
to the printf () function instead of the actual value of the variable sum
. '& sum' returns the memory address of the variable, but 'sum' returns the actual value of the variable. So it should look like this:
printf("Answer = %f\n", sum);
I noticed that you defined a variable choice
at the top of your program, but never used it. Because of this and your operator chaining else if
, I am assuming that you want to give the user a choice: add, subtract, multiply, or divide.
To do this, I would define choice
a char instead of an int and ask the user to enter one of these four characters to be assigned to the variable choice
: ' +
', ' -
', ' *
' or ' /
'.
To define choice
as char, write this:
char choice;
Then ask the user to enter a choice like this:
scanf("%c", &choice);
This takes one character from the user and assigns it choice
.
Finally, change your statements if
to something like this:
if (choice == '+')
printf("Answer = %f\n", sum);
else if (choice == '-')
printf("Answer = %f\n", difference);
else
printf("Error: invalid choice.\n");
You can also use an operator switch
for this.
source to share
You have a spelling factor.
Actually, don't pass the address of your printf arguments. You only need to do this for scanf. For example, printf ("Answer =% f \ n", quotient);
Uh, and that's the whole if ... else if ... thing at the end is just awkward. Get it out.
And why are you scanning the results of your calculations? Take this too.
source to share
#include<stdio.h>
#include<conio.h>
int main ()
{
int choice;
float a, b;
float sum;
float difference;
float product;
float quotiont;
printf("This program adds, subtracts, multiplies, and divides.\n");
printf("**************\n");
printf("* Calculator *\n");
printf("**************\n");
printf("Enter thee value of a: ");
scanf("%f",&a);
printf("Enter the value of y:");
scanf("%f",&b);
sum=a+b;
if (sum = a + b);
printf("sum is %f",sum);
difference = a - b;
if(difference=a-b)
printf("\n difference is %f",difference);
product = a * b;
if(product=a*b)
printf("\n product is %f",product);
quotient = a / b;
if(quotient=a/b)
printf("\n quotient is %f",quotient);
return(main());
}
source to share