【问题标题】:The Code exits after the input and output statements代码在输入和输出语句后退出
【发布时间】:2017-12-23 06:29:14
【问题描述】:

在打印第二个 printf 后,代码退出并说运行失败并说在级别 2 上退出(无论输入是什么)

void main()
{
    int testcases;
    int n_lines ,m_length;
    int test, lines, length;
    char n_m_matrix[n_lines][m_length];
    printf("Enter the no. of Test Cases ");
    scanf("%d",&testcases);
    printf("%d",testcases);  
    for(test=0;test>testcases;test++)
    {
         printf("Enter the lines and length of the test cases ");
         scanf("%d%d",&n_lines,&m_length);
        for(lines=0;lines<n_lines;lines++)
        {
            for(length=0;length<m_length;length++)
           {
               scanf("%c",&n_m_matrix[lines][length]);
           }
       }
   }

}

【问题讨论】:

  • 使用int main() 而不是void main()。这是古老的
  • test &lt; testcases?

标签: c


【解决方案1】:

您需要将n_m_matrix 的声明移动到循环中,以便在您输入保存维度的变量之后。

正如其他人所说,您在test &gt; testcases 中也有一个错字,应该是&lt;

并且您应该在输入尺寸后读取一个额外的字符,以读取换行符。否则它将在输入缓冲区中的尺寸之后留下换行符,并且在读取内容时这将被读取为第一个 %c 输入。

您也可以考虑使用fgets() 来读取每一行,而不是逐个字符地读取。按照您编写的方式,如果每行末尾都有换行符,它们将被插入到数组中。目前尚不清楚是否需要这样做。如果是,请确保将它们包含在行长输入中。

int main()
{
    int testcases;
    int n_lines ,m_length;
    int test, lines, length;
    printf("Enter the no. of Test Cases ");
    scanf("%d",&testcases);
    printf("%d\n",testcases);  
    for(test=0; test < testcases; test++)
    {
        printf("Enter the lines and length of the test cases ");
        scanf("%d%d",&n_lines,&m_length);
        getc(); // Skip over the newline
        char n_m_matrix[n_lines][m_length];
        for(lines=0;lines<n_lines;lines++)
        {
            for(length=0;length<m_length;length++)
            {
                scanf("%c",&n_m_matrix[lines][length]);
            }
        }
    }

}

【讨论】:

  • %d%d 后面的空格是可以的if the input comes from a file,但是如果是打字就不行。此外,最里面的一对循环会将换行符视为存储在数组中的有效字符,这可能会也可能不会。
  • @JonathanLeffler 谢谢,我感觉这有点不对劲。我在其余输入中添加了一些关于换行符的内容。
【解决方案2】:

为了消除几个问题之间的混淆,第一个在代码中调用未定义行为的问题是,使用未初始化的变量作为 VLA 的大小。

另外,for 循环逻辑是错误的,字符输入可能会在处理方式上产生一些问题。 (使用\n 输入时)。

正确的代码应该是(考虑到您不希望将空格输入到数组中。因为如果您这样做了,那么还有其他更好的选择,例如 fgets 等)。

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 1024
int main(void)
{
    int testcases;
    int n_lines ,m_length;
    int test, lines, length;

    printf("Enter the no. of Test Cases ");
    if( scanf("%d",&testcases) != 1){
        fprintf(stderr, "%s\n","Error in input" );
        exit(1);
    }
    if( testcases <= 0 ){
        fprintf(stderr,"%s\n","Enter nonzero integer for testcases");
        exit(1);
    }
    printf("%d",testcases);  
    for(test = 0; test < testcases; test++)
    {
        printf("Enter the lines and length of the test cases ");
        if(scanf("%d%d",&n_lines,&m_length)!=1){
            fprintf(stderr, "%s\n","Error in input" );
            exit(1);           
        }
        if( n_lines <= 0 || m_length <= 0 || !(n_lines <= MAXSIZE && m_length <= MAXSIZE)){
            fprintf(stderr, "%s\n","Error in input n_lines and m_length" );
            exit(1);           
        }

        char n_m_matrix[n_lines][m_length];
        for(lines = 0; lines < n_lines; lines++)
        {
            for(length = 0; length < m_length; length++)
            {
                if( scanf(" %c",&n_m_matrix[lines][length]) != 1)
                {
                    fprintf(stderr, "%s\n","Eror in input" );
                    exit(1);
                }

            }
        }
        // work with VLA here.
    }
    return 0;
}

如果您需要比这更大的数组大小,请使用动态内存分配。这将满足数组中更大的内存需求。

【讨论】:

  • 除了了解问题空间之外,没有确定限制的好方法。但是你确实需要限制 VLA 的大小,否则你会毁掉你的筹码而没有恢复的机会。到那时,动态内存分配就变得明智了。
  • @JonathanLeffler.:我明白了..是的,这就是动态内存分配有用的地方。
【解决方案3】:

您需要使用动态内存分配,malloc 方法。矩阵n_m_matrix 维度是根据用户输入动态决定的。

  1. 使用 malloc 将内存分配给 n_m_matrix
  2. 在 for 循环中纠正逻辑错误 test&gt;testcasetest&lt;testcase
  3. 将下面的代码移出 for 循环
    printf("Enter the lines and length of the test cases "); scanf("%d%d",&n_lines,&m_length);

【讨论】:

  • 你不需要使用malloc,你只需要在设置变量后声明数组。
  • @Barmar n_linesm_length 的值动态变化(基于用户输入),因此无法进行静态内存分配。 Refer this QA
  • 您可以简单地将数组的声明移动到for 循环内,使其位于输入之后。
  • 如果您的编译器支持 VLA 功能,这是可能的。这意味着它必须是 C99 或 C11 编译器,当然 MCVS 不是(它支持 C90,有一些但不是全部 C99/C11 扩展,AFAIK,它不支持 VLA)。
猜你喜欢
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 2015-05-08
  • 2018-12-04
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多