Scanf does not work even forcing to ignore the spaces

Asked

Viewed 68 times

-2

I’m trying to make a simple program to start messing with C.

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

int main(void)
{
    
    char nome[100];
    char dataNascimento[10];
    char telefone[11];
    char endereco[200];
    char email[100];
    char nomeAnimal[100];
    char especie[100];
    char raca[100];
    
    setlocale(LC_ALL, "Portuguese");

    printf("Nome: \n"); 
    scanf(" %s", &nome);
    
    printf("Data de Nascimento: \n");   
    scanf(" %s", &dataNascimento);
    
    printf("Endereço: \n"); 
    scanf(" %s", &endereco);
    
    printf("E-mail: \n");   
    scanf(" %s", &email);
    
    printf("Telefone: \n"); 
    scanf(" %s", &telefone);
    
    printf("Nome Animal: \n");  
    scanf(" %s", &nomeAnimal);
    
    printf("Espécie: \n");  
    scanf(" %s", &especie);
    
    printf("Raça: \n"); 
    scanf(" %s", &raca);           
    
    printf("Cliente Cadastrado Com Sucesso!");  
    
    return 0;
}

When I enter a word in the name line, the program works:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

But when I insert two words, the scanf between Date of Birth and Address does not work:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • An observation that was not commented on in the answers is about the &. It is not necessary in the scanf when the goal is to read a string / vetor, because the vector variable already points to the address, that is, if we have vetor[10] then vetor == &vetor[0].

2 answers

2

This is quite simple, the problem is that you are working with strings(array of characters) and are using scanf to receive input, and the problem is the definition of scanf:

scanf(): You will read all the input on the line until you find one space, newline or End of file(EOF)

Then usually it will be recommended to use gets() because it is specifically for this, unlike scanf() which is for general use:

gets(): It will read all the input until it finds either a newline or the end of the file, considering spaces as part of the string

But the main point, as mentioned here is that both the use of scanf and gets are not ideal for long-term programs, as both do not have an overflow buffer, the alternative with this protection would be fgets(), which can be used as in the example:

#include <stdio.h>
int main()
{
    char str[30];
    printf("Input:\n");
    fgets(str, 10, stdin);
    // vai receber apenas os primeiros 10 caracteres digitados (você coloca o numero que precisar)
    printf("Output: %s\n", str);
    return 0;
}

ex: Input: 1234567891011121314151617181920

Output: 123456789

However, if you still want or need to use the scanf a lot, you can create an exception within this scanf to ignore spaces as a stop condition using, %[^\n]s, would look something like this:

scanf("%[^\n]s", str); 

1

The problem is occurring because the function scanf reads the input until you find a space. So, when typing two separate words, it’s like you’re giving the program 2 values.

For you to be able to store more than one word in the same variable you can use the function fgets in place. Ex.:

printf("Nome: \n");
fgets(nome,100,stdin);

Where:

  • name = the variable where the value will be stored
  • 100 = the maximum number of characters that will be stored
  • stdin = the input device (usually the keyboard)

Now, after performing this substitution, you can store names with more than one word (separated by spaces) in a single variable.

Browser other questions tagged

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