Getting Started in Programming

Asked

Viewed 75 times

1

I worked out a very simple algorithm, but I’m not able to do the same.

Follows:

#include <stdio.h>

int num1, num2, soma, multiplicacao;

int main(){
    printf("informe primeiro número: ");
    scanf("%d", &num1);

    printf("informe segundo número: ");
    scanf("%d", &num2);

    soma = num1 + num2;
    multiplicacao = soma * num1;

    printf("o resultado é %d", multiplicacao);
}

The problem I’m facing is that the console does not open so that I can add the values, when open does not bring me the result, only appears the CMD to add the variables num1 and num2 and then closes.

And bring me the information "Info: Nothing to build for C programs"

I’m using Eclipse as an IDE.

  • uses the system("pause") at the end of the function (needs the library #include <stdlib.h>)

  • an alternative function is to use the getChar();, Then he doesn’t even need a new library

1 answer

0

Your CMD is just running what is being ordered and closing the application as soon as it finishes reading the information. Basically he doesn’t know that it should remain open for you to read the code.

A solution widely used to solve this type of problem is using the system("pause");. That way, your code would look like this:

#include <stdio.h>
#include <stdlib.h>

int num1, num2, soma, multiplicacao;

int main(){
    printf("informe primeiro número: ");
    scanf("%d", &num1);

    printf("informe segundo número: ");
    scanf("%d", &num2);

    soma = num1 + num2;
    multiplicacao = soma * num1;

    printf("o resultado é %d", multiplicacao);
    system("pause");
}

Although this solution already solves your problem, in some forums it is common to find people who are against this command, one of the main reasons is why you need to import a new library to the project.

An alternative command to this is the getChar(); , where it is waiting for "an empty scanf", then it does not close the console until you press enter (and do not need an additional library). In the second solution, you would only need to change the last two lines, so:

  printf("o resultado é %d", multiplicacao);
  getChar();

Browser other questions tagged

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