-1

i'm quite new in programming and i'm learning functions and stuff like that... I was trying to create this little program that gives the result of 2 numbers based on the operator gave by input but idk why the output is always 0. Can someone help me please?? Thank you a lot. Here's the code:

#include <stdio.h>

void Numeri(float n1, float n2){
    printf("Inserisci il valore dei due numeri: \n");
    printf("[1] ");
    scanf("%f", &n1);
    printf("[2] ");
    scanf("%f", &n2);
}

float Segno(char segno, float n1, float n2, float res){
    
    printf("Inserisci il segno dell'operazione: ");
    scanf("%c", &segno);
    segno=getchar();
    
    switch (segno){
        case '+':
            res=n1+n2;
            printf("%f", res);
            break;
        case '-':
            res=n1-n2;
            printf("%f", res);
            break;
        case '*':
            res=n1*n2;
            printf("%f", res);
            break;
        case '/':
            res=n1/n2;
            printf("%f", res);
            break;
        default:
            printf("Riprova: ");
            scanf("%c", &segno);
    }
}

int main(){
    float numero1, numero2, risultato;
    char operazione;
    
    Numeri(numero1, numero2);
    Segno(operazione, numero1, numero2, risultato);
}
Face._.
  • 21
  • 3

1 Answers1

1

function Numeri is receiving parameters by values, change to pass a pointer instead.

/* prototype*/
void Numeri(float* n1, float* n2);

and call in main:

Numeri(&numero1, &numero2);

(I'm leaving some implementation stuff intentionally)

Max
  • 3,128
  • 1
  • 24
  • 24
  • After watching some tutorials and working on the code i have finally understood thank you a lot!! – Face._. Oct 17 '21 at 00:58
  • 1
    @Face._. don't watch tutorials. Most of them are bad or wrong in many ways. Find a good book instead [The Definitive C Book Guide and List](https://stackoverflow.com/q/562303/995714) – phuclv Oct 17 '21 at 03:38
  • Oh ok i'll do it thanks :) – Face._. Oct 17 '21 at 11:16