Text looks full of junk after typed

Asked

Viewed 117 times

0

I started creating a data collection and usage application with the following code:

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

int main() {
    int idade = 0;
    char nome[20];

    printf("Hello! How old are you? ");
    scanf("%d", &idade);
    while (idade == 0) {
        printf("\nAge cannot be 0 nor be decimal. Re-enter your age: ");
        scanf("%d", &idade);
    }
    printf("And what's your name? (write up to 20 characters, only 1st name) ");
    scanf("%[^\n]s", nome);
    printf("\nHello %s, aged %d!\n", nome, idade);

    return 0;
}

But, after compiling with GCC, the output is:

Hello! How old are you? 123
And what's your name? (write up to 20 characters, only 1st name) 
Hello �����dU��m�, aged 123!

What I have to change?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

5

The code has some problems. I’ll solve some, but it’s still not robust code because it doesn’t test other problems, I won’t go into those issues.

One problem is that to read text with white space you need to use fgets(), and needs to treat the line change that must be switched to a terminator. Which leads to another problem: if you want 20 characters you need to reserve 21 bytes of buffer. And the scanf() need to receive a pattern with the new line to leave no dirt on buffer.

At least this is a solution:

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

int main() {
    int idade = 0;
    printf("Hello! How old are you? ");
    scanf("%d\n", &idade);
    while (idade == 0) {
        printf("\nAge cannot be 0 nor be decimal. Re-enter your age: ");
        scanf("%d", &idade);
    }
    char nome[21];
    fgets(nome, 20, stdin);
    nome[strcspn(nome, "\n")] = 0;
    printf("\nHello %s, aged %d!\n", nome, idade);
}

Behold working in the ideone. And in the replit. Also put on the Github for future reference.

It is possible to use a trick with the scanf(), but understand that in real codes this function is rarely used for free data entry:

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

int main() {
    int idade = 0;
    printf("Hello! How old are you? ");
    scanf("%d\n", &idade);
    while (idade == 0) {
        printf("\nAge cannot be 0 nor be decimal. Re-enter your age: ");
        scanf("%d", &idade);
    }
    char nome[21];
    scanf(" %[^\n]s", nome);
    printf("\nHello %s, aged %d!\n", nome, idade);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

3

Problem

This problem is very common for those who are starting or are not very much on the subject, and is related to the form with the scanf with %d works.

If you repair the scanf with %d does not consume line break, and so allows you to do something like:

int x, y, z;
scanf("%d", &x);
scanf("%d", &y);
scanf("%d", &z);

And then enter the values separated by space, like this:

10 20 30

It’s important to realize that you just pushed Enter once but managed to make 3 readings, for the simple reason that they do not consume you line break.

See this example in Ideone

So in your code, first read age:

scanf("%d", &idade);

But the Enter was in the input buffer and when will read the nome with:

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

The first and only thing he gets is Enter, because reading with %[^\n] it’s till I catch \n.

Solution

The solution is to consume the line break. It has at least two simple ways to do it:

1

Hello, just add this line before the name scanf setbuf(stdin, NULL);

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

int main() {

int idade = 0;
char nome[20];

printf("Hello! How old are you? ");
scanf("%d", &idade);
while (idade == 0) {
    printf("\nAge cannot be 0 nor be decimal. Re-enter your age: ");
    scanf("%d", &idade);
}
printf("And what's your name? (write up to 20 characters, only 1st name) ");
setbuf(stdin, NULL);    
scanf("%[^\n]s", nome);
printf("\nHello %s, aged %d!\n", nome, idade);

return 0;

}

  • 2

    No, not enough: https://ideone.com/MavRiS

  • @Strange maniero, I do not have that mistake, probably by running on a windows

  • Yes, it is something platform dependent, so it should not be used.

  • I don’t understand, I took the test and it went well https://ideone.com/uomFIx

  • You are not entering the input, which is normal. You cannot use something that depends on how the data is entered. You saw that the test does not work, no use adapting the test to work, have to adapt the code to work.

  • There’s no way I can find the solution

  • I’ll do it then.

Show 2 more comments

Browser other questions tagged

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