I can’t make the scanf work

Asked

Viewed 207 times

0

I’m trying to learn C and today I made a very simple program that requests basic personal data and then it needs to print this data on the screen.

I put 5 items to be requested and he only requests half of them and the other half appears directly on the screen, without requesting anything. Like half is just one printf, You know? It doesn’t spin the part scanf.

I’ve already looked at all the code and I can’t find the error.

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

void main () {

    setlocale(LC_ALL, "portuguese");

    char nome;
    float media;
    int tel, id, mes;

    printf("Digite a primeira letra do seu nome: ");
    scanf("%c", &nome);

    printf("Digite seu telefone: ");
    scanf("%i", &tel);

    printf("Digite sua média: ");
    scanf("%2.2f", &media);

    printf("Digite o mês do seu nascimento: ");
    scanf("%i", &mes);

    printf("Digite sua idade: ");
    scanf("%i", &id);
}
  • 1

    "Pretend that here have the includes" it was not easier to put the includes?

  • it was bad Anderson, I couldn’t make it

  • 1

    Try to leave: scanf("%f", &media);

  • sbrubes ready, I changed it and typed as whole. Thank you very much

1 answer

0

It is not possible to use %2.2f in the scanf, only in prints to display.

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

    char nome;
    float media;
    int tel, id, mes;

    printf("Digite a primeira letra do seu nome: ");
    scanf("%c", &nome);

    printf("Digite seu telefone: ");
    scanf("%i", &tel);

    printf("Digite sua média: ");
    scanf("%f", &media);

    printf("Digite o mês do seu nascimento: ");
    scanf("%i", &mes);

    printf("Digite sua idade: ");
    scanf("%i", &id);
}
  • now I understand better, thank you very much!

Browser other questions tagged

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