代码在输入和输出语句之后退出

问题描述 投票:0回答:3

打印完第二个printf后,代码退出并显示Run Failed并表示退出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]);
           }
       }
   }

}
c
3个回答
2
投票

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

正如其他人提到的,你在test > testcases也有一个错字,它应该是<

并且您应该在输入尺寸后阅读额外的字符,以阅读换行符。否则它将在输入缓冲区中的维度之后留下换行符,并且在读取内容时将读取该第一个%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]);
            }
        }
    }

}

2
投票

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

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;
}

在这里,如果您需要比此更大的阵列大小,请进行动态内存分配。这将满足阵列中更大的内存需求。


0
投票

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

  1. 使用malloc将内存分配给n_m_matrix
  2. 在for循环中正确的逻辑错误test>testcasetest<testcase
  3. 将以下代码放在for循环中 printf("Enter the lines and length of the test cases "); scanf("%d%d",&n_lines,&m_length);
© www.soinside.com 2019 - 2024. All rights reserved.