Question:
Why wont the following 'C' code work?
Hamish
2013-06-25 02:07:19 UTC
Hello, I have recently begun learning the 'C' programming language and tried (and failed so far) to create a basic calculator program..
What is wrong with the following code and are there any helpful tips or anything people can help me with??

#include

int math_function(int x, int y);

int main()
{
int x, y;
char sign;
printf("Choose:\n");
printf("a) Addition\n");
printf("b) Subtraction\n");
printf("c) Division\n");
printf("d) Multiplication\n");
scanf("%c", &sign);

printf("Enter two numbers:\n");
scanf("%d%d", &x, &y);

/*Print out their numbers inside the math_function*/
printf("%d\n", math_function(x, y));
return;
}
char sign;
int math_function(int x, int y)
{
switch(sign)
{
case 'a':
return x + y;
break;
case 'b':
return x - y;
break;
case 'c':
return x / y;
break;
case 'd':
return x * y;
break;
/*Default statement*/
default:
printf("You entered an invalid choice\n");
}
}


Thanks!
Five answers:
2013-06-25 07:06:02 UTC
Hi,



You've done ok, but just need a couple of errors repaired.



As the previous person mentioned, you really need to use global variables:

  These are declared along with your #include's before int main()



You also needed to remove the math_function you left up there with the #includes



Your switch statement could be left as is, but I've made a modification there in the code below, to remove the redundant break's (redundant because you broke anyway using the return), and changed the default: to break; only to avoid printing your Error message with a stray decimal value appearing at end of message. This is dealt with in the main() using an if () else.



Both main() and math_function() do need a return 0 also, as you'll see below.



#include

// Global Variables useable in main and function

char sign;

int x, y;



int main() {

  printf("Choose:\n");

  printf("a) Addition\n");

  printf("b) Subtraction\n");

  printf("c) Division\n");

  printf("d) Multiplication\n");

  scanf("%c", &sign);



  printf("Enter two numbers:\n");

  scanf("%d%d", &x, &y);



  /*Print out their numbers inside the math_function*/

  /* This if else allows the math_funtion to print the values for a, b, c or d choices

   * but any other choice just print the error message so that value does not

   * stray to right side of message

   */

  if (sign == 'a' || sign == 'b' || sign == 'c' || sign == 'd')

    printf("%d\n", math_function(x, y));

  else

    printf("'%c' is an invalid choice ", sign);

  return 0;

}



int math_function(int x, int y) {

  switch(sign) {

    case 'a':

      return x + y;

    case 'b':

      return x - y;

    case 'c':

      return x / y;

    case 'd':

      return x * y;

    default:

      break;

  }

  return 0;

}
roger
2013-06-25 07:50:47 UTC
I fixed some things....

untested code follows.....



#include



int math_function(int , int ,int );



int main()

{

int x, y;

char operator;

printf("Choose:\n");// better just to let the user punch in the operator rather than abcd ...

printf("+ Addition\n");

printf("- Subtraction\n");

printf("/ Division\n");

printf("* Multiplication\n");

scanf("%c", &operator);



printf("Enter two numbers:\n");

scanf(" %d %d", &x, &y);// the spaces tell scanf to ignore white space twixt the numbers



/*Print out their values returned from the math_function*/

printf("%d\n", math_function(x, y, operator));// better practice to pass the operator here rather than as a global

return 0;

}

// char sign; do not redefine sign as a global -- pass the operator into the function -- better practice

int math_function(int x, int y, int op) // believe it or not '+' is an int (integer) constant in C and has the sizeof an int

{

switch(op)

{

case '+':

return x + y;

break;

case '-':

return x - y;

break;

case '/':

if(y!=0)

return x / y;

else

printf("you cannot do that !");

break;

case '*':

return x * y;

break;

/*Default statement*/

default:

printf("You entered an invalid choice\n");

}

return 0;// you still need a return value here :-)

}
tylor
2016-11-09 03:11:01 UTC
rubbish would not propose random. It merely skill that the outcomes are not predicted via the familiar, and *might* no longer be the comparable from run to run. In a common implementation, interior of reach variables are saved on the stack and what's in an uninitialized variable is in spite of became left on the stack from some in the previous use. as a effect, it would have been the equipment code that parses the command line arguments in the previous calling your important() function, or another run-time initialization.
venus
2013-06-25 02:47:31 UTC
First of all define your 'sign' variable as global.

main function: You should return a value (eg: return 0;)

math_function: You should return a value.



Also, your code could be improved a lot.
2013-06-25 02:12:56 UTC
u missed a colon


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...