【问题标题】:Reading multiple lines of input and save ints into array读取多行输入并将整数保存到数组中
【发布时间】:2017-10-27 18:10:06
【问题描述】:

我正在尝试将一些 ints 保存到从用户输入读取的数组中。我不知道每行上ints 的数量,只知道行数,这也是该行上ints 的最大数量。例如,如果这是 5,那么用户应该输入 5 行 ints 并且每行最多包含 5 个元素。值将是正数。我做错了什么?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int n;
    scanf("%d",&n);
    int array_row[n];
    int i=0;
    int noEnter=n;
    //My idea is when in getchar() there is a enter it means that the user wants to go to the next line so decrement noEnter with 1 and also store a value -1 which tells me that that was the end of the line
    while(noEnter!=0){
        int c;
        if(scanf("%d",&c)==1){
            array_row[i]=c;
            i++;
            continue;
        }
        char d=getchar();
        if(d=='\n'){
            array_row[i]=-1;
            i++;
            noEnter--;
            continue;
        }
    }



    for(int i=0;i<n*n;i++){
        printf("%d ",array_row[i]);
    }

    return 0;
}

输入示例:

5
4
4 35 65
4 32
2 222 4 5 6
4

输出:

4 -1 4 35 65 -1 4 32 -1 2 222 4 5 6 -1 4 -1

【问题讨论】:

    标签: c scanf


    【解决方案1】:

    scanf 不会像您期望的那样停在\n。它正在读取整数..结果我猜你的程序甚至没有结束。读取一行并将其分成整数。这样你就可以得到整数并相应地处理它们。

    由于事先不知道输入的整数数量,您可以标记strtok 行,然后将每个数字从字符串转换为int

    此外,您的输入不符合给定的输出。您在第一行给出了输入 5,但它从未出现在前几个数字的输出中。

    #define LINE 100
    ..
    
    int len=0;
    char line[LINE];
    char*word;
    while(noEnter<5){
        fgets(line,LINE,stdin);
        word=strtok(line," \n");
        if(word == NULL) 
             break;
        while(word != NULL){
            int p=atoi(word);
            array_row[len++]=p;
            word=strtok(NULL," \n");
        }
        noEnter++;
        array_row[len++]=-1;
    }
    

    scanf 被执行 读取输入直到第一个非空白字符(保留 未读),或直到无法读取更多字符。

    在这里,您甚至没有机会在到达该行之前使用 \ngetchar()\nscanf() 消耗和丢弃。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define LINE 50
    int main(int argc, char *argv[]) {
        int n=5;
        int array_row[LINE];
        int len=0;
        int noEnter=3;
        char line[LINE];
        char*word;
        while(noEnter<5){
    
            fgets(line,LINE,stdin);
            word=strtok(line," \n");
            if(word == NULL)
               break;
            while(word != NULL){
                int p=atoi(word);
                array_row[len++]=p;
                word=strtok(NULL," \n");
            }
            noEnter++;
            array_row[len++]=-1;
        }
        for(int i=0;i<len;i++){
           printf("%d ",array_row[i]);
        }
    
        return 0;
    }
    

    【讨论】:

    • 我对 scanf 的了解是,如果它成功读取数字并将其存储在变量中,那么它应该返回 1。这是我的第一个条件。如果不是,那么我认为它不是一个数字,所以我的第二个 if 应该处理这个问题。如果我在一行4 5\n 在我的脑海中是首先它读取 4 将其存储在数组中然后继续,它会看到空白并且什么都不做,因为我的任何条件都会处理它,看到 5 并且做同样的事情和以前一样,然后它会看到应该用 getchar() 捕获的 \n。
    • 是的。你是对的;我的错。在我的问题中,我有一个应该从键盘读取的变量,并且我对 n=5 进行了硬编码。这是程序应该读取的行数,也是一行中int 的最大数量
    • @AndreiMădălinOancă.:检查答案
    • 编辑:后来解决了。我的坏处是没有给数组足够的空间
    【解决方案2】:

    scanf 的一个主要问题是它对于读取换行符(相对于其他空格)的位置很重要的数据没有用处。 Scanf 只是在空白处打断东西,空格、制表符和换行符之间没有区别,所以 5 个数字的行和 5 个数字的行之间没有区别。

    因此,如果您关心换行符,通常需要使用fgets 将行读入缓冲区,然后使用sscanf 解析该行中的数字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      相关资源
      最近更新 更多