김아연
김아연

Reputation: 1

In this calculator program, only zero gets displayed as output

#include <stdio.h>
void calculation(int x, int y, int z);
main() {
  calculation(15, 5, 1);
}

void calculation(int x, int y, int z) {

  if (z == 1) {
    printf("%d", add(x, y));
  }
  if (z == 2) {
    printf("%d", mul(x, y));
  }
  if (z == 3) {
    printf("%d", div(x, y));
  }
  if (z == 4) {
    printf("%d", sub(x, y));
  }
}

int mul(int x, int y) {
  x * y;
}

int add(int x, int y) {
  x + y;
}

int div(int x, int y) {
  x / y;
}

int sub(int x, int y) {
  x - y;
}

int gugudan(int x) {
  for (int i = 1; i <= 9; i++) {
    printf("%d*%d=%d\n", x, i, x * i);
  } 
}

This is the code I want to run by multiplying x with y from the source code that makes the calculator. I'm trying to print the output, but even with different inputs, the result keeps displaying as zero. What's wrong with x,y,z? Or was it wrong somewhere else?

Upvotes: 0

Views: 165

Answers (3)

Rohan Bari
Rohan Bari

Reputation: 7726

A very much simplified version of your code:

#include <stdio.h>

int calculate(int, int, short int);

int main(void) {
    int result = calculate(15, 5, 1);

    printf("%d\n", result);

    return 0;
}

int calculate(int x, int y, short int op) {
    switch (op) {
        case 1:  return (x + y); break;
        case 2:  return (x - y); break;
        case 3:  return (x * y); break;
        case 4:  return (x / y); break;
        default: break;
    }
}

Example Output

20    // calculate(15, 5, 1)

Although you just forgot to use return statements, but you should declare the scopes of function(s) first and definitions after the main().

Upvotes: 0

zeroSpook
zeroSpook

Reputation: 54

The functions add, mul, div, sub need to return an integer(int) type value.

In the mul function, replace:

int mul(int x, int y) {

   x * y;
 }

with

int mul(int x, int y) 
{ 
  return x * y;
}

In the add function, replace:

int add(int x, int y) {

   x + y;
 }

with

int add(int x, int y) 
{ 
   return x + y;
}

In the div function, replace:

int div(int x, int y) {

   x / y;
 }

with

int div(int x, int y) 
{
   return x / y;
}

In the sub function, replace:

int sub(int x, int y) {

   x - y;
 }

with

int sub(int x, int y) 
 { 
   return x - y;
 }

Also, add the function prototypes for add, sub, mul, div functions before using them inside the calculation functions or else define them before the same.

Upvotes: 0

campescassiano
campescassiano

Reputation: 831

Check the functions you are declaring, for example, the mul one. You declared it to return an int, but inside your function, you are not returning anything.

To fix your problem, you need to return the value of the function, which is expected to be an int.

So, here is a snipped of what you should do to return the value from a function (i.e., the mul function):

int mul(int x, int y) {

   return x * y;
 }

So, you should fix this missing return on the other functions you developed as well and your code should work fine.

Upvotes: 1

Related Questions