You can analyze my C calculator and help me finish the project

Asked

Viewed 136 times

-1

#include <stdio.h>
int main (){
	float n1=0;
	char operacao;
	float n2=0;
	printf("*CALCULADORA\n");
	printf("n1:");
	scanf("%f", & n1);
	printf("operacao:");
	scanf("%c", operacao);
	printf("n2:");
	scanf("%f", n2);
	// operacao de adicao
	switch(operacao){
		case '+':
			printf("R:%f+%f=%f\n", n1, n2, n1+n2);
			break;
			//operacao de subtracao
			switch(operacao){
		case '-':
			printf("R:%f-%f=%f\n", n1, n2, n1-n2);
			break;
		}//operacao de multiplicao
			switch(operacao){
		case '*':
			printf("R:%f*%f=%f\n", n1, n2, n1*n2);
			break;
		}//operacao de divisão
		case '/':
		if(n2==0){
			printf("ne");
		}else{
			printf("R:%f/%f=%f\n", n1, n2, n1/n2);
		}		
	break;
	default:
		printf("operacao inv");
		return 0;
	}
	
}

inserir a descrição da imagem aqui'cause it’s static, 'cause it didn’t show up N2. Help me finish, please

1 answer

2

Here: scanf("%c", operation); missed & before variable operation; idem in: scanf("%f", N2);. There is no sense in these switches before each case. Review the syntax of the switch/case command.

#include <stdio.h>
int main (){
    float n1=0;
    char operacao;
    float n2=0;
    printf("*CALCULADORA\n");
    printf("n1:");
    scanf("%f", &n1);
    printf("operacao:");
    scanf("%c", &operacao);
    printf("n2:");
    scanf("%f", &n2);
    // operacao de adicao
    switch(operacao){
        case '+':
            printf("R:%f+%f=%f\n", n1, n2, n1+n2);
            break;
            //operacao de subtracao
        case '-':
            printf("R:%f-%f=%f\n", n1, n2, n1-n2);
            break;
            //operacao de multiplicao
        case '*':
            printf("R:%f*%f=%f\n", n1, n2, n1*n2);
            break;
            //operacao de divisão
        case '/':
            if(n2==0){
                printf("ne");
            }else{
                printf("R:%f/%f=%f\n", n1, n2, n1/n2);
            }       
            break;
        default:
            printf("operacao inv");
    }
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.