简体   繁体   English

数组输入获得的条目比应有的多

[英]Array input is getting one more entry than should be

My code isn't wrong but here's the problem .我的代码没有错,但这就是问题所在 As you can see, the last element shouldn't appear, although it doesn't get counted it's annoying.正如你所看到的,最后一个元素不应该出现,虽然它没有被计算在内,但很烦人。

Here's my code:这是我的代码:

int mat[3][4] = {0};
int i, j;

for(i = 0; i < 3; i++){
    for(j = 0; j < 4; j++){
        printf("insert the element [%d][%d]", i, j);
        scanf("%d ", &mat[i][j]);
    }
}

printf("\nmatrix: \n");
for(i = 0; i < 3; i++){
    for (j = 0; j < 4; j++){
        printf("%3d ", mat[i][j]);
    }
    printf("\n");
}

Check here: You faced the problem because of white space in scanf()在这里检查:由于 scanf() 中的空白,您遇到了问题

What actually happens when you ask scanf to read numeric data, it first skips any white space it finds, and then it reads characters until that character cannot form part of a number.当您要求 scanf 读取数字数据时实际发生了什么,它首先跳过它找到的任何空格,然后读取字符,直到该字符不能构成数字的一部分。

#include <stdio.h>
    
int main()
{
    int mat[3][4] = {0};
    int i, j;

    for(i = 0; i < 3; i++){
       for(j = 0; j < 4; j++){
           printf("insert the element [%d][%d] ", i, j);
        scanf("%d", &mat[i][j]);
        }
    }

    printf("\nmatrix: \n");
    for(i = 0; i < 3; i++){
        for (j = 0; j < 4; j++){
            printf("%3d", mat[i][j]);
        }
    printf("\n");
    }

    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C程序数组需要的输入过多 - C program array takes more input than it should 输入数组函数返回多个值 - Input array fuction that return more than one value 输入行末尾的空格导致 while 循环到 go 比它应该多一次 - Space at the end of input line causes while loop to go one more time than it should 为什么我的C程序要求的输入多于应该的? 额外的输入甚至没有做任何事情 - Why is my C program asking for one more input than it should? And the extra input doesn't even do anything C中有一种方法可以在数组的不同位置从输入中插入多个数字? - There is way in C to insert a number of more than one digit from input in different positions of an array? 在不知道要输入的数量的情况下,逐行读取多个数组的输入 - Reading input for more than one array, line by line,without knowing the number of inputs to be given printf打印数组的多个元素 - printf printing more than one element of an array 将多个字符串分别放入数组 - putting more than one string in an array individually C:for循环中的Scanf函数运行时间比预期时间长 - C: Scanf function in for loops runs one more time than it should 我怎么知道用户输入的字符是否超过1个? 如果他输入多个字符,我想将其存储在字符数组中 - How can I know if user enters more than 1 character as input? And if he enters more than one character I want to store that in character array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM