简体   繁体   English

C:使用scanf读取多个值并将其保存到数组中

[英]C: Reading multiple values with scanf and saving them into an array

I found an answer to the first part of my question (how to read multiple values with scanf) but it doesn't seem to work for me (I think it's because of putting the values into an array and maybe also because I'm checking if the values given are 6 ints for sure): 我找到了问题第一部分的答案(如何使用scanf读取多个值),但它似乎对我不起作用(我认为这是因为将值放入数组中,也可能是因为我正在检查如果给定的值肯定是6个整数):

I am writing a program that stores co-ordinates of 4 triangles in an array. 我正在编写一个将4个三角形的坐标存储在数组中的程序。 Each line has 6 values and stores co-ordinates of one triangle. 每行有6个值,并存储一个三角形的坐标。 I want to read 6 co-ordinates at one time and do this operation for 4 triangles separately. 我想一次读取6个坐标,并分别对4个三角形进行此操作。

int tab[4][6];

for (int i = 0; i < 4; i++){
    while (scanf("%d %d %d %d %d %d", &tab[i][0], &tab[i][1], &tab[i][2], &tab[i][3], &tab[i][4], &tab[i][5]) != 6){
        printf("Error, try again: ");
        while (getchar() != '\n'){}
    }
}

So for example if first triangle's co-ordinates are (2,1), (5,6), (2,7), then I want to type in: "2 1 5 6 2 7" and as a result I want it to fill the first line of the array with the said numbers in the order I typed them in. 因此,例如,如果第一个三角形的坐标为(2,1),(5,6),(2,7),那么我想输入:“ 2 1 5 6 2 7”,因此我想要它按照我输入的顺序,用上述数字填充数组的第一行。

Obviously it doesn't work, the program stops working (not finishes the work, it stops) after the first line is given. 显然,它不起作用,在给出第一行后,程序停止了工作(未完成工作,它停止了)。

I get this error after debugging (after giving first line): "Unhandled exception at 0x0FDCC28C (msvcr120d.dll) in xxx.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC." 调试后(给出第一行后),我得到此错误:“ xxx.exe中0x0FDCC28C(msvcr120d.dll)的未处理异常:0xC0000005:写入文件0xCCCCCCCC的访问冲突。”

How to fix it? 如何解决?

You need to subtract the pointer i when detecting input error like this for example -> 例如,在检测到输入错误时,您需要减去指针i->

#include <stdio.h>
int main(int argc, const char * argv[]) {
    int tab[4][6];
    for (int i = 0; i < 4; i++){
        printf("Enter 6 values \n");
        int retVal=scanf("%d %d %d %d %d %d", &tab[i][0], &tab[i][1], &tab[i][2], &tab[i][3], &tab[i][4], &tab[i][5]);
        if (retVal == 6) {
            printf("You did enter -> %d %d %d %d %d %d\n",tab[i][0],tab[i][1],tab[i][2],tab[i][3],tab[i][4],tab[i][5]);
        } else {
            printf("Error entering values.. (Enter numbers). \n");
            while (getchar() != '\n'){}
            i--;
        }
    }
    return 0;
}

Unclear why OP's code failed without posting input used and prior code. 不清楚为什么OP的代码失败而不发布使用的输入和先前的代码。


How to fix it? 如何解决?

Use fgets() to read a line of user input. 使用fgets()读取一行用户输入。 Avoid mixing scanf() with fgets() in prior code. 避免在先前的代码中将scanf()fgets() 混合使用。 Then parse the buffer. 然后解析缓冲区。 Use " %n" at the end to look for success and extra text. 最后使用" %n"查找成功和其他文本。

int tab[4][6];
char buf[6*12 * 2];  // Use a buffer twice expected max needs

for (int i = 0; i < 4; i++) {
  while (1) {
    if (fgets(buf, size  buf, stdin) == NULL) {
      return "Failed to read enough data"; // Handle end-of-file in some fashion
    }
    int n = 0;
    sscanf(buf, "%d%d%d%d%d%d %n", 
        &tab[i][0], &tab[i][1], &tab[i][2], &tab[i][3], &tab[i][4], &tab[i][5], &n);
    if (n > 0 && buf[n] == 0) {
      break;  // Success!
    }
    printf("Error - bad input, try again: ");
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM