【问题标题】:Storing and Iterating through user inputs in C在 C 中通过用户输入存储和迭代
【发布时间】:2021-05-21 19:38:19
【问题描述】:

下面的程序要求用户输入 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

【问题讨论】:

  • 对于每个i,您都有准确的i+1 存储输入。
  • 你不能申请strlen(storedinputs)。您传递了错误的类型。内循环可以是for(int s = 0; s &lt;= i; s++)

标签: c function for-loop input user-input


【解决方案1】:

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

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

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

【讨论】:

  • 这是对的,但不幸的是不是原始代码的唯一问题。例如,缺少错误检查,并且在内部循环中数组没有被索引。
【解决方案2】:

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

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

--

#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 

好的,已经输入了10个

【讨论】:

    猜你喜欢
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 2010-11-21
    • 2017-04-28
    • 1970-01-01
    相关资源
    最近更新 更多