By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,869 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,869 IT Pros & Developers. It's quick & easy.

I want to build a calculator with Global variables that are also functions

P: 1
I am attempting to build a calculator that does the 4 basic operations: Addition, Subtraction, Multiplication and Division.

Two caveats:

#1 it must be done with global variables that also work as functions.
#2 the user needs to be able to pick the operation.

With case which is what the teacher suggested to me it throws:
[Error] case label does not reduce to an integer constant

and after some googling I end finding that I should retort to if, using that gives me a different problem, I can't input values. I can select what mathematical operation I need to do, but not what values to input.

So, in order, with case:

//Calculadora ISUM
//Seleccione la operacion
// 1) Suma
// 2) Resta
// 3) Multiplicacion
// 4) Division


suma(int n1, int n2)

{ int operacion;

printf("Ingrese la operacion que desea realizar");
scanf("%i", & operacion);

printf("Ingrese el primer valor");
scanf("%i", & n1);

printf("Ingrese el segundo valor");
scanf("%i", & n2);


case suma:
printf("La suma de n1 %i + n2 %i es %i");

printf("Ingrese Suma, Resta, Prod o Div");

return 0;


Now for the if:


int adherir(int salida1, int salida2)

printf("%i", salida1);

printf("%i", salida2);

return salida1 + salida2;
return salida1 - salida2;
return salida1 * salida2;
return salida1 / salida2;


int main()

int operacion, n1, n2, suma, resta, producto, division, resultado;

printf("Ingrese la clase de operacion que desee llevar a cabo: ");

scanf("%i", & operacion);

switch (operacion)

printf("Ingrese el primer valor: ");

scanf("%i", & n1);

printf("Ingrese el segundo valor: ");

scanf("%i", & n2);

if (operacion=suma)

{ n1+n2; }

if (operacion=resta)


if (operacion=producto)


if (operacion=division)


resultado=adherir(n1, n2);

printf ("El Resultado de su operacion es: %i ", resultado);

return 0;


To make it clearer on the if (in case this is a mess and no one gets it)

I am attempting to write: IF Operacion is suma proceed to ask for n1 and n2 then add n1 to n2 and print the result.

Replace suma with resta, producto, etc.

adherir is there because the entire program freaks out if I change the word (bloodshed dev C++ 5.11)

A friend helped me and pointed the errors, the if one is now 99% functional, problem is, that it won't print the return, well either is not printing the return or not carrying the operations:


float suma (float n1, float n2)

{return n1+n2;}

float resta (float n1, float n2)

{ return n1-n2;}

float producto (float n1, float n2)

{return n1*n2;}

float division (float n1, float n2)

{ return n1/n2;}

int main()

float n1, n2, tot;
char operacion;

printf("Ingrese el signo correspondiente a la operacion que desee llevar a cabo: ");

scanf("%c", & operacion);

printf("Ingrese el primer valor: ");

scanf("%f", & n1);

printf("Ingrese el segundo valor: ");

scanf("%f", & n2);

switch (operacion)

if (operacion=='+')

{ n1+n2==tot; }

else if (operacion== '-')


else if (operacion== '*')


else if (operacion=='/')


printf ("El resultado de la operacion es : %.2f \n ",tot);

return 0;

Oct 25 '17 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 9,197
Global variables?? Please read this:
Oct 26 '17 #2

Expert 100+
P: 2,404
Important question: are you using C or C++? I will assume you are using C.

Let's start with just a short excerpt from your "case" code.
Expand|Select|Wrap|Line Numbers
  1. char operacion;
  2. <snip>
  3. scanf("%i", & operacion);
  4. <snip>
  5. switch(operacion)
  6. {
  7.   case suma:
  8.     printf("La suma de n1 %i + n2 %i es %i");
  9.     break;
  10.   default:
  11.     printf("Ingrese Suma, Resta, Prod o Div");
  12. }
<snip> means there was code here that I didn't list.
  1. Line 1. Type char should only be used for characters. Use signed char or unsigned char for numbers. I can't tell if you intend this to be a character or a number.
  2. Lines 1 and 3. Variable operacion is declared as a char but the scanf() %i format specifier is for an int. scanf() is not smart enough to notice the discrepancy. It will try to store an int value (typically 4 bytes) in a char variable (only 1 byte). The result will be memory corruption. You need to either change the type of operacion or the format specifier.
  3. Line 5. The switch argument must be an int. Your char variable will be promoted to an int. That is probably ok.
  4. Line 7. The case argument must be a number. Not a variable with a numeric value -- it must be a literal number. suma is not a literal number. You ought to get a compiler error here.
  5. Line 8. This format string contains three instances of %i. Each %i requires an additional int argument to printf(). printf() is not smart enough to notice the additional arguments are missing. It will do something crazy.

Maybe you intend for the user to type the string suma to indicate the addition operation. If so, then ...
  • operacion must be a char array rather than a single char variable. The array must be large enough to hold anything that the user might type (plus a terminating null). There will be memory corruption if the user types more characters than the array can hold.
  • Use the %s format specifier to tell scanf() to read a string.
  • Don't use switch/case to test the operacion string. Instead use an if/else if/else cascade that uses strcmp() to determine what string was entered.
  • You should use toupper() or tolower() to convert the entered string to a uniform case if you want the comparison to be case-insensitive. That is, if you want suma or Suma or SUMA to all be valid.
I don't know what "it must be done with global variables that also work as functions" means. Function names inhabit the same namespace as global variables. The language will not permit you to have a function and variable with the same name.
Oct 26 '17 #3

Expert 100+
P: 2,404
Let's look at a short excerpt of your "if" code
Expand|Select|Wrap|Line Numbers
  1. int operacion, n1, n2, suma, <snip>, resultado;
  3. printf("Ingrese la clase de operacion que desee llevar a cabo: ");
  4. scanf("%i", & operacion);
  6. switch (operacion)
  8. printf("Ingrese el primer valor: ");
  9. scanf("%i", & n1);
  11. printf("Ingrese el segundo valor: ");
  12. scanf("%i", & n2);
  14. if (operacion=suma)
  15.   { n1+n2; }
  1. Lines 4,9,12. Good! The pointer arguments are consistent with the format specifiers. Both are ints.
  2. Line 6. Delete this line.
  3. Line 14. suma is an uninitialized variable. It could have any value.
  4. Line 14. This assigns the [uninitialized] value of suma to operacion and then compare that value to zero. You want operacion==suma to compare the [uninitialized] value of suma to the value of operacion. Notice this means that the user has to enter some particular numeric code when they want to add.
  5. Line 15. This adds n1 to n2 and then throws away the result. You need to assign the sum to a variable if you want to use it later. For example: resultado=n1+n2;
Oct 26 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.