It's really easy, but why can't I type it right in the array?

Asked 2 years ago, Updated 2 years ago, 39 views

Sorry for easy questions....

#include <stdio.h>
int bs[200]={0}, gs[200]={0};
int n, k;
int i, j;
int main(void)
{
    scanf("%d", &n);
    scanf("%d",&k);
    printf("%d", n);
    for(i=1;i<=n;i++){
        scanf("%d\n", &gs[i]);
    }
    for(i=1; i<=n; i++){
        printf("%d %d\n", gs[i], i);
    }
    return 0;
}

array

2022-09-21 14:45

1 Answers

Beginners make a lot of mistakes, so read the scanf function help carefully.

http://itguru.tistory.com/32

The link above explains it well.

If you insert a number and type enter, not only the number is stored in the buffer, but the enter key is also in the buffer.

In other words, if it is a consecutive scanf, the first number and the second number enter the value of the value.

To prevent this, you can empty the buffer.

Please call getchar after scanf.


2022-09-21 14:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.