-1
Good evening, I am doing a function exercise, where I type two numbers and choose which operation I want to do, 1 it applies the sum function and 2 the subtraction function, but my algorithm is not working and I would like to know what I am doing wrong =s follows the algorithm
#include <stdio.h>
#include <stdlib.h>
int somanumero(int numero1, int numero2){
int result;
result = numero1 +numero2;
return result;
}
int subnumero (int numero1, int numero2){
int result;
result = numero1 - numero2;
return result;
}
int main (){
int n1,n2,resp,result;
result = 0;
printf ("\nDigite o primeiro numero: ");
scanf ("%d", &n1);
printf ("\nDigite o segunto numero: ");
scanf ("%d", &n2);
printf ("\nQual operação gostaria de fazer. Soma [1] Sub [2]");
scanf ("%d", resp);
if (resp = 1){
result = somanumero(n1,n2);
}
else {
result = subnumero (n1,n2);
}
printf ("O resultado da equação é: %d", result);
}
Instead of
if (resp = 1)
, has to beif (resp == 1)
- the operator=
assigns the value, and since 1 is considered "true", it always enters theif
. Already the operator==
makes the comparison correctly– hkotsubo