Data entry being skipped after algorithm build

Asked

Viewed 63 times

-2

My code is quite simple and its functionality is as follows::

  • to) Request name of User
  • b) Request age of User
  • c) Display the same

However, after the algorithm is compiled and run, when entering with the first data (user name) the executable "pula" to next data entry (user age).

Below is the source code

int main(){
    char name;
    int age;

    printf("Seja bem-vindo ao nosso sistema. \n\n");
    printf("Qual o seu nome?");
    scanf(" %s", name);

    printf("Qual é a sua idade?");
    scanf(" %d", &age);

    return 0;   
}
  • 1

    Good as you read and does nothing with what was read so the program just terminates normally. Also you set your name variable as a single character and not as a string you would expect for a name.

2 answers

0

To be able to store more than one character you need to create and start an array with a predetermined size. (That’s for you, because it’s simple and you’re a beginner)

So before I give you the answer to your algorithm, you should study the following basic contents to develop in c language:

  • Introduction to C Language
  • Operators and Variables
  • Function
  • Basic I/O functions (input/output)
  • Conditional Structures, Loops and Flow Control
  • Blocks and Functions
  • Inline instructions and the C compiler
  • Vectors / Matrices
  • String definition and manipulation
  • String definition
  • Pointers / Parameters by reference
  • Operators and special types

Now.. Answering your question...

Your algorithm should look like this:

#include<stdio.h>
int main(){
    char name[0];
    int age;


    printf("Entre com o seu Nome: ");
    gets(name);

    printf("Entre com a sua Idade: ");
    scanf("%d", &age);

    printf("\nNOME: %s\n", name);
    printf("IDADE: %d\n", age);

    return 0;
}

To level up your career as a developer, I suggest studying the topics mentioned and in your case as you are starting, below I leave a link where it contains from basic to advanced so you understand what I did and learn to c language:

DOCUMENTATION: LEARNING TO PROGRAM IN C

0

To enter a word and need to create a char vector, because in C there is no String type

char nome[100];

and to enter the name use gets() instead of scanf()

gets(name);

Browser other questions tagged

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