When copying the code it gives an error using the WHILE function in C

Asked

Viewed 47 times

0

#include <stdio.h>
#include <string.h>
int main()
{
    int num_key=666;
    do
    {
        puts("\n\n\t*********************************************");
        puts("\t*                                           *");
        puts("\t*     Calculador de temperatura             *");
        puts("\t*                                           *");
        puts("\t*                                           *");
        puts("\t*                                           *");
        puts("\t*          DIGITE SUA SENHA                 *");
        puts("\t*                                           *");
        puts("\t*                                           *");
        puts("\t*                                           *");
        puts("\t*  DIGITE 0 PARA SAIR                       *");
        puts("\t*********************************************");
        scanf("%i",num_key);                    
    }while(num_key!=123);
}

I’m having a problem in that while loop, I can’t find the problem, it all comes running it from the bug and closes. I checked and redacted the logic twice.

  • 1

    You need to pass the address of your variable num_key in the scanf, it may be so scanf("%i", &num_key);

  • Grateful friend, I wrote over 500 lines here already and went to forget it, lack of attention Thank you very much!

  • I published in response.

1 answer

4


An important tip is to enable all warnings displayed by the compiler.

See the warnings of your program:

warnings imagem

See the first Warning, he’s saying he expected a int * instead of a int, as its variable num_key is not a pointer, so it is necessary to use the & to pass the address instead of the value to the function scanf(), so the compiler stops giving warnings.

The second Warning refers to the return of its function main. This function of yours is returning an integer, so it is necessary to assign the

return 0;

or

return 1;

in case of an error in your program, please note that the compiler also alerts you to this.

Your code:

#include <stdio.h>
#include <string.h>

int main(void)
{
    int num_key=666;
    do
    {
        puts("\n\n\t*********************************************");
        puts("\t*                                           *");
        puts("\t*     Calculador de temperatura             *");
        puts("\t*                                           *");
        puts("\t*                                           *");
        puts("\t*                                           *");
        puts("\t*          DIGITE SUA SENHA                 *");
        puts("\t*                                           *");
        puts("\t*                                           *");
        puts("\t*                                           *");
        puts("\t*  DIGITE 0 PARA SAIR                       *");
        puts("\t*********************************************");
        scanf("%i", &num_key);
    }while(num_key!=123);

    return 0;
}
  • thank you! I will include in my code.

Browser other questions tagged

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