-3
I created a calculator:
include stdio.h
include stdlib.h
include locale.h
int main(void){
system("color 0a");
float num1, num2;
char op;
setlocale(LC_ALL, "portuguese");
while(1){
printf("\t\t\tCALCULADORA DO BARALHO\n");
printf("Faça seus Cálculos: ");
scanf("%f%c%f", &num1,&op,&num2);
switch (op){
case '+':
printf ("\nO Resultado é= %.1f\n", num1+num2);
break;
case '-':
printf ("\nO Resultado é= %.1f\n", num1-num2);
break;
case '*':
printf ("\nO Resultado é= %.1f\n", num1*num2);
break;
case '/':
printf ("\nO Resultado é= %.1f\n", num1/num2);
break;
default:
system("cls");
printf("\n(There something wrong DUDE!!! Try again)\n\n");
break;
}
if(num1!=0 && num1!=1 && num1!=2 && num1!=3 && num1!=4 && num1!=5 && num1!=6 && num1!=7 && num1!=8 && num1!=9){
system("cls");
printf("(There something wrong DUDE!!! Try again)\n\n");
system("color 0c");
system("pause");
break;
}
else{
}
if(num2!=0 && num2!=1 && num2!=2 && num2!=3 && num2!=4 && num2!=5 && num2!=6 && num2!=7 && num2!=8 && num2!=9){
system("cls");
printf("(There something wrong DUDE!!! Try again)\n\n");
system("color 0c");
system("pause");
break;
}
else{
}
printf("\t\t\t\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n!!!!LUCAS PRODUÇÕES E DESENVOLVIMENTO!!!!\n;-;");
system ("pause");
system ("cls");
}
//use ctrl+c para fechar o programa.
return 0;
}
However it presents the following problems:
- Once one of the error conditions is reached the established loop by while(1) terminates and the program closes. I want it to go back to the beginning.
- After the first calculation works, if the second one goes forward to one of the error conditions the program suffers a bug and not to do nothing else.
If you want to move to another iteration, change the
break;
forcontinue;
. If you want to check if an integer is not in the range [0.9]if (num1 < 0 && num1 > 9) {/* Seu código aqui.*/}
– Augusto Vasques
I want to check if the number is float, in case n wants the program to signal an error and go back to the beginning, to the loop.
– Lucas Silva Dias
In C you can’t do that. The C language does not pass type information in Runtime.
– Augusto Vasques
I just want q n bug when I type a character, just send an error msg and go back to the program.
– Lucas Silva Dias
For this you have to check the result of
scanf
. In case of successscanf
returns the number of completed arguments, in case of failure it returns 0 or an error code (negative number). Documentation ofscanf
– Augusto Vasques