C language - Error when using loop scanf to obtain integer and string type data

Asked

Viewed 85 times

0

At first I was trying to make a binary tree that receives values of the intiro and string type (name and surname). But I’ll just put a bug to fix first.

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

int main(void){
    
    int key, key2;
    char name[50], name2[50]; 

    printf("digite a chave: ");
    scanf("%d", &key);
    printf("digite o nome: ");
    scanf("%50[^\n]s", name);
    
    printf("digite a chave: ");
    scanf("%d", &key2);
    printf("digite o nome: ");
    scanf("%50[^\n]s", name2);
    
    return 0;
}

The code compiles, but at the moment food with the data occurs this error:

digite a chave: 12

digite o nome: digite a chave:

Soon after it closes.
I could use the for, but I did so to make it easier to visualize.

1 answer

1

Good afternoon!

It is good to remember that '\n' is the character line break, inserted when pressing the ENTER key. Thus, it is good to remember that the variables key and key2 are of the type whole, then they won’t keep a character.
That way, when you write you read key, for example, with scanf("%50[^\n]", key); you are reading 50 characters at most, (or) until you reach a '\n'.
As whole variables do not read '\n', this character is there on the line for the next reading of characters or strings. And when key will save its value, soon comes across '\n', which you determined to be the read stop. Then it follows the algorithm, asking for the next key.

To solve the problem, you have two modes:
•You can create a garbage variable, type character, to read that '\n', after reading a key. •Or you can enter the following code, to read the name and surname: scanf("\n%50[^\n]s", name);, that already breaks line, nullifying that '\n' remaining and releasing a clean line, for the string key read normally.


If you are not going to continue without a repeating structure in if code, I will leave the two forms of solutions here. If you are going to use a for, while or another type of loop, I believe that with the explanation given you can solve the problem in your code, now.

//Utilizando uma variável lixo
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void){
    
    int key, key2;
    char name[50], name2[50];
    char trash;

    printf("digite a chave: ");
    scanf("%d", &key);
    scanf("%c", &trash);
    printf("digite o nome: ");
    scanf("\n%50[^\n]s", name);
    
    printf("digite a chave: ");
    scanf("%d", &key2);
    scanf("%c", &trash);
    printf("digite o nome: ");
    scanf("\n%50[^\n]s", name2);
    
    return 0;
}
//Utilizando a quebra de linha direto no scanf()
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void){
    
    int key, key2;
    char name[50], name2[50]; 

    printf("digite a chave: ");
    scanf("%d", &key);
    printf("digite o nome: ");
    scanf("\n%50[^\n]s", name);
    
    printf("digite a chave: ");
    scanf("%d", &key2);
    printf("digite o nome: ");
    scanf("\n%50[^\n]s", name2);
    
    return 0;
}

I hope I helped. Good studies!

Browser other questions tagged

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