Calculator is not capturing the second number

Asked

Viewed 78 times

1

I have a code that I wrote, it takes two numbers and sums, subtracts, multiplies and divides. However, when incrementing the questions in the code and then running it reads only the number of the first question.

Where am I wrong? Since apparently the code has no error.

error while compiling:

código compilado,numero 2 não é requisitado

follows the code:


#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main()
{

setlocale(LC_ALL, "Portuguese");
printf("===============\n");
printf("CALCULADORA 1.0");
printf("\n===============\n");

  int num1, num2, soma, subtracao, multi, div;

  soma = num1 + num2;
  subtracao = num1-num2;
  multi = num1 * num2;
  div = num1 / num2;

  printf("Digite Dois Números a serem calculados:\n");
  printf("Primeiro Número:\n");

  scanf("Primeiro Número:%i\n",&num1);

  printf("Segundo Número:\n");
  scanf("Segundo Número:%i\n",&num2);
  printf("A soma é :%i\n",soma);
  printf("A subtração é : %i \n",subtracao);
  printf("O produto é:%i \n",multi);
  printf("A divisão é :%i \n",div);

}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

1

The biggest problem is that you are putting the text in the formatter of scanf(), there is only to have a data input pattern and not a text, including because the text was already written.

There are other problems. The operations are being performed before the data is read so the calculations will occur with data already existing in the memory, even because you didn’t initialize the variables. To tell the truth most variables shouldn’t even exist.

I improved on other things, paid attention to every detail of the code and took unnecessary things, at least for the current code. It was much simpler, no?

#include <stdio.h>

int main() {
    printf("===============\n");
    printf("CALCULADORA 1.0");
    printf("\n===============\n");
    printf("Digite Dois Números a serem calculados:\n");
    printf("Primeiro Número: ");
    int num1, num2;
    scanf("%d", &num1);
    printf("\nSegundo Número: ");
    scanf("%d", &num2);
    printf("\nA soma é: %d\n", num1 + num2);
    printf("A subtração é: %d\n", num1 - num2);
    printf("O produto é: %d\n", num1 * num2);
    printf("A divisão é: %d\n", num1 / num2);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thanks for your time and help Maniero. I noticed that you used(%d) instead of (%i). Don’t need to use %i then? since %d also covers the content of %i?! How can I be inserting text? (maybe I should study more to find out, I started studying yesterday, you know?! Thank you again, God bless you! Good afternoon!

  • https://answall.com/a/152401/101. You’re all set there, you don’t have to do anything else, follow the flow line by line.

  • @Bruno Poggian could you mark the answer as accepted? It would help the community not to enter the question since it is answered.

Browser other questions tagged

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