在 C 中存储和迭代用户输入

Storing and Iterating through user inputs in C

下面的程序要求用户输入 10 个整数并将用户输入存储在相应的 10 个整数数组中。输入完成后,它应该遍历已经存在的用户输入并将它们打印出来。我想通过使用像 strlen() 这样的函数来遍历 storedinputs 中的存储值,但是存储的值不是字符串而是整数。我怎么能做出这种事

int main(void) {
    int storedinputs[10] = {0};
    int input;

    for(int i = 0; i < 10; i++) {
        printf("\nPlayer input:");
        scanf("%d", &input);
        storedinputs[i] = input;

        for(int s = 0; s < strlen(storedinputs); s++) {
            printf("Inputs %d", storedinputs);
        }
    }

    return 0;
}

预期输出:

Player input: 1
Inputs: 1
Player input: 3
Inputs: 1 3
Player input: 60
Inputs: 1 3 60

您不能使用函数 strlen(),因为 storedinputs 是一个整数数组。如果您想在每次插入新值时打印存储的值,您应该按照 Weather Vane 在评论中的建议编辑 for 循环条件,如下所示:

for(int s = 0; s <= i; s++)

变量i表示包含最后添加的数字的单元格的编号。

这是包含错误检查的固定代码。

  1. 需要跳过多余的输入(while(fgetc(stdin) != '\n');)
  2. scanf 错误检查能力不足。使用 fgetsstrtol.
  3. 的组合
  4. 在内部循环中,您需要在达到外部循环计数器时终止
  5. 你不应该计算无效输入(因此,我用 while 循环替换了外部 for 循环)
  6. 您不应在数组中存储无效输入
  7. 您需要在打印时为存储的输入建立索引

--

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

// In contrast to #define this provides a nice means for grouping constants
// and it can be changed into a typedef, later on
// Furthermore, it's a compiler aware constant
enum {INPUT_COUNT = 10, MAX_INPUT_LENGTH = 20};

int main(void) {
    int input_count = 0;                    // The count of valid inputs
    char input[MAX_INPUT_LENGTH + 1] = {0}; // The string buf for one input
    int converted_input;                    // Result of integer conversion
    int storedinputs[INPUT_COUNT] = {0};    // Array with valid results
    char *endptr;                           // Needed for strtol error checking

    while(input_count < 10) {               // Loop over 10 valid inputs
        printf("\nPlayer input (Enter single integer value):");
        fgets(input, sizeof(input), stdin);
        printf("Got input: %s\n", input);   // input contains '\n', already

        // If input was longer than MAX_INPUT_LENGTH, get rid of remains
        if (input[strlen(input) - 1] != '\n') {
            while(fgetc(stdin) != '\n');
        }

        // Reset errno, so strtol returns a fresh value
        errno = 0;
        converted_input = strtol(input, &endptr, 10);

        // The errno is set if input is out of range
        if (errno) {
            perror("Conversion error");
            continue;
        }

        // Here we have invalid characters present in the input
        if ( (endptr == input) || (*endptr != '\n') ) {
            printf("Please enter single integer!\n");
            continue;
        }

        // Everything fine, so we can store result
        storedinputs[input_count] = converted_input;

        // Now we output all valid inputs, up to now
        printf("Inputs: ");

        for(int s = 0; s <= input_count; s++) {
            printf("%d ", storedinputs[s]);
        }

        printf("\n");

        ++input_count;
    }

    return 0;
}

输出:

Player input (Enter single integer value):12
Got input: 12

Inputs: 12 

Player input (Enter single integer value):13 
Got input: 13

Inputs: 12 13 

Player input (Enter single integer value):14
Got input: 14

Inputs: 12 13 14 

Player input (Enter single integer value):1324567543245678654324567
Got input: 13245675432456786543
Conversion error: Numerical result out of range

Player input (Enter single integer value):asdc
Got input: asdc

Please enter single integer!

Player input (Enter single integer value):
Got input: 

Please enter single integer!

Player input (Enter single integer value):12 12 12
Got input: 12 12 12

Please enter single integer!

Player input (Enter single integer value):1
Got input: 1

Inputs: 12 13 14 1 

Player input (Enter single integer value):2
Got input: 2

Inputs: 12 13 14 1 2 

Player input (Enter single integer value):3
Got input: 3

Inputs: 12 13 14 1 2 3 

Player input (Enter single integer value):4
Got input: 4

Inputs: 12 13 14 1 2 3 4 

Player input (Enter single integer value):5
Got input: 5

Inputs: 12 13 14 1 2 3 4 5 

Player input (Enter single integer value):67
Got input: 67

Inputs: 12 13 14 1 2 3 4 5 67 

Player input (Enter single integer value):7
Got input: 7

Inputs: 12 13 14 1 2 3 4 5 67 7 

OK,已完成10次输入