Questions about strings

-2

I have the proposal to print on a theater ticket, the date, time, part name (to be typed and scanned by scanf), and the customer's seat number

// declaração da variável.

char PecaTeatral[255];
// pulando para a leitura da string 

switch(n){      
case 1:

printf("\nNome da peca teatral\n");

scanf("%s", PecaTeatral);

printf("DATA : %s HORA: %s\nPeca : %s \t\t numero da poltrona: %d", __DATE__, __TIME__, PecaTeatral, ingresso_inicial); 

break;

// imprimi o ingresso do cliente, apos vendido

...

The difficulty in all this, how can I print a text, because the current code stores a word,

    
asked by anonymous 17.10.2018 / 08:50

1 answer

1

You can use scanf or fgets , the most "famous", and which perfectly serve to read a sentence, instead of a word.

  • Scanf : You must add [^\n] , to accept all types of characters, minus the enter button. You should also use the maximum number of characters that you want to read %51[^\n] , will read the maximum 51 characters in this example.

Ex: scanf("%254[^\n]s", PecaTeatral);

  • fgets : This example is easiest and most recommended as it can read a sentence and limit characters without doing great tricks.

Ex: fgets(PecaTeatral,254, STDIN);

It is possible to solve the problem with the gets function, but this function is extremely dangerous. Case set as:

char PecaTeatral[10]; and the user type more than 9 caracteres the program will give bug,

    
17.10.2018 / 09:59