tags:

views:

3763

answers:

2

I am trying to get this program to give me an out put that when I do an addition, subtraction, multiplication, or division problem it will give me the answer. However, it is not working can anyone 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, &quotiont);  

  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", &quotiont);  
  else  
      printf("Error");  
}
A: 

You've misspelled quotient.

Actually, don't pass the address of your args to printf. You only need to do that for scanf. Do, e.g., printf("Answer = %f\n", quotient);

Uh, and that whole if...else if... thing at the end is just wonky. Take it out.

And why are you scanfing for the results of your calculations? Take that out too.

bog
+10  A: 

What are you trying to accomplish with this line?

scanf("%f %f %f %f", &sum, &difference, &product, &quotiont);

What this does is takes four numbers from the user and loads them into the four variables, respectively. Right after this line you assign new values to these four variables, so there is no point in loading them with values in this line of code.

Also, what is the point of the following if statement? (And all of the else if statements)

if(a + b)
    printf("Answer = %f\n", &sum);

This will only print the answer if the sum of a and b is non-zero. If the expression inside the brackets after the 'if' evaluates to zero, it will not execute the code underneath. If it evaluates to a non-zero value, it will execute the code.

Another problem with the above line is that you are passing a pointer to the sum variable to the printf() function instead of the actual value of the sum variable. '&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 choice variable at the top of your program, but never used it. Because of that and your chain of else if statements, I'm assuming you want to give the user a choice of whether to add, subtract, multiply, or divide.

To do this, I would define choice as a char (character) instead of an int, and would get the user to type in one of these four characters to be assigned to the choice variable: '+', '-', '*', or '/'.

To define choice as a char, write this:

char choice;

Then get the user to input a choice like this:

scanf("%c", &choice);

This takes a single character from the user and assigns it to choice.

Finally, change your if statements 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 may also want to use a switch statement for this.

yjerem