Home > Software engineering >  How to get in input operators in C?
How to get in input operators in C?

Time:10-17

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);
}

CodePudding user response:

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)

  •  Tags:  
  • c
  • Related