Problem with printf and scanf - My code does not print the correct information

Asked

Viewed 624 times

0

I have this code here that is not reading the characters correctly when printing the data filled by the user on the screen. Can you help me??

//Question 1) Make an algorithm that reads a user’s name, id, age, gender, address, phone number, and mobile phone. //Print user data on screen. (Value 0.1 point)

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

main(){

     char nome [50], sexo [50], end [50]; 
     int rg, idade, telefone, celular;

     printf("Nome: ");     scanf("%s",nome);
     printf("RG: ");       scanf("%d",&rg);
     printf("Idade: ");    scanf("%d",&idade);
     printf("Sexo: ");     scanf("%s",sexo);
     printf("Telefone: "); scanf("%d",&telefone);
     printf("Celular: ");  scanf("%d",&celular);
     printf("Endereco: "); scanf("%s",end);

     printf("\nNome:%s",nome);
     printf("\nRG:%d",&rg);
     printf("\nIdade:%d",&idade);
     printf("\nsexo:%s",sexo);
     printf("\nTelefone:%s",&telefone);
     printf("\nCelular:%d",&celular);
     printf("\nEndereco:%s",end);
  • This code is language C, and not C#.

  • 1

    You use the pattern &variável in function scanf, because it needs to change the value of the variable, but in the function printf you use only variavel, and not &variavel.

1 answer

1

Come on: on RG, on age, on phone and on mobile you are using the & before variable (at the time of printing). Follow the corrected code:

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

void main(){
 char nome [50], sexo [50], end [50];
 int rg, idade, telefone, celular;

 printf("Nome: ");
 scanf("%s",nome);
 printf("RG: ");
 scanf("%d",&rg);
 printf("Idade: ");
 scanf("%d",&idade);
 printf("Sexo: ");
 scanf("%s",sexo);
 printf("Telefone: ");
 scanf("%d",&telefone);
 printf("Celular: ");
 scanf("%d",&celular);
 printf("Endereco: ");
 scanf("%s",end);

 printf("\nNome:%s",nome);
 printf("\nRG:%d", rg);
 printf("\nIdade:%d", idade);
 printf("\nsexo:%s", sexo);
 printf("\nTelefone:%d", telefone);
 printf("\nCelular:%d", celular);
 printf("\nEndereco:%s\n",end);
}

Browser other questions tagged

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