gpt4 book ai didi

c - 代码在输入和输出语句后退出

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:55 25 4
gpt4 key购买 nike

在打印第二个 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]);
}
}
}

}

最佳答案

你需要移动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]);
}
}
}

}

关于c - 代码在输入和输出语句后退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47950333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com