我在C代码中得到了这个错误。我不知道我做错了什么。如果我评论这段代码,我的程序就能工作了。这段代码在int ().中
if(argc!=2 && strcmp(argv[0],"selection-sort")==0 && strcmp(argv[1],"input.txt")==0 && strcmp(argv[2],"output.txt")==0)
{
printf("The command line arguments are correct.\n");
}
else
{
printf("The command line arguments are wrong.I am exiting.\n");
break;
}发布于 2012-08-22 19:38:14
它的外观,我认为你不是在一个循环,但只是检查主要的args。您可能想要类似return 1或exit(1)之类的东西,而不是休息。
发布于 2012-08-22 19:42:49
首先,确保包括所需的头文件:
#include <stdio.h>
#include <stdlib.h>break命令用于退出循环,您不是在一个循环中,而是在一个else语句中,您没有什么可中断的。在传递after语句后,代码流将正常执行。如果您想在could语句中退出程序,您可以这样做:
else
{
printf("The command line arguments are wrong.I am exiting.\n");
return 1; //exit program with status 1 to indicate a non normal exit
}或者,如果您想在显示该消息后继续该程序,则只需执行以下操作:
else printf("The command line arguments are wrong.I am exiting.\n");
//more code here您只使用这样的中断循环:
while(foo) //while foo is true
{
break; //exit the loop
}发布于 2012-08-22 19:40:55
标题中的错误信息说明了一切:break只能用于退出循环或防止案例失败。MSDN报价:
break语句终止最近的do、for、switch或while语句的执行。
若要保留函数,请使用return。
https://stackoverflow.com/questions/12080220
复制相似问题