C语言的问题在VC++环境下提示warning C4715: 'question_get' : not all control paths return a value
答案:2 悬赏:10 手机版
解决时间 2021-04-02 06:04
- 提问者网友:却不属于对方
- 2021-04-02 00:56
C语言的问题在VC++环境下提示warning C4715: 'question_get' : not all control paths return a value
最佳答案
- 五星知识达人网友:神也偏爱
- 2021-04-02 01:44
这是两个警告,不影响程序运行。
第一个警告是说,函数question_get存在不return返回值的分支,在这个函数里,也就是一系列的if else之后,剩余的情况(type < 0)没有得到处理,按照你代码的逻辑,最后一个else if(type ==0 || type==5)改成else,把后面的if()去掉就可以了。
第二个警告是说,函数main里,局部变量n定义了却没有被引用,把这个n删掉就可以了。
#include
#include
#include
int question_get();
int type;
void main( void )
{
int answer;
srand( (unsigned)time( NULL ) );
loop:
printf( "请选择要进行测试的题目种类:" );
printf( "
1.加法运算
2.减法运算
3.乘法运算
4.除法运算
5.退出运算
" );
printf(" 请选择(1-5):");
scanf( "%d", &type );
while( 1 )
{
int temp;
int flag;
answer = question_get();
printf( "请回答:
" );
scanf( "%d", &temp );
while( temp!=answer )
{
printf( "
答案错误,重做
" );
scanf( "%d", &temp );
}
printf( "
答案正确,很好
" );
printf( "继续请按1,退出请按0
" );
scanf( "%d", &flag );
while( flag!=0&&flag!=1 )
{
printf( "按其它键无效
" );
scanf( "%d", &flag );
}
if( flag==0 )
break;
goto loop;
}
}
int question_get()
{
int a,b,c;
loop:
if( type==1 )
{
a=rand()%99;
b=99-a;
b=rand()%b;
printf( "%d + %d = ?", a, b );
return(a+b);
}
else if( type==2 )
{
b=rand()%99;
c=99-b;
c=rand()%c;
printf( "%d - %d = ?", b+c, b );
return(c);
}
else if( type==3 )
{
a=rand()%10;
b=50-a;
b=rand()%b;
printf( "%d * %d = ?", a, b );
return(a*b);
}
else if( type==4 )
{
b=rand()%50;
c=100/b;
while( 1 )
{
c=rand()%c;
if( c!=0 )
break;
}
printf( "%d / %d = ?", b*c, b );
return(c);
}
else if( type==5 )
{
printf(" 退出系统
");
system("pause");
exit(0);
}
else
{
printf(" 输入错误,请输入1-5内的数字
");
printf(" 请选择(1-5):");
scanf( "%d", &type );
goto loop;
}
return -1;
}
第一个警告是说,函数question_get存在不return返回值的分支,在这个函数里,也就是一系列的if else之后,剩余的情况(type < 0)没有得到处理,按照你代码的逻辑,最后一个else if(type ==0 || type==5)改成else,把后面的if()去掉就可以了。
第二个警告是说,函数main里,局部变量n定义了却没有被引用,把这个n删掉就可以了。
#include
#include
#include
int question_get();
int type;
void main( void )
{
int answer;
srand( (unsigned)time( NULL ) );
loop:
printf( "请选择要进行测试的题目种类:" );
printf( "
1.加法运算
2.减法运算
3.乘法运算
4.除法运算
5.退出运算
" );
printf(" 请选择(1-5):");
scanf( "%d", &type );
while( 1 )
{
int temp;
int flag;
answer = question_get();
printf( "请回答:
" );
scanf( "%d", &temp );
while( temp!=answer )
{
printf( "
答案错误,重做
" );
scanf( "%d", &temp );
}
printf( "
答案正确,很好
" );
printf( "继续请按1,退出请按0
" );
scanf( "%d", &flag );
while( flag!=0&&flag!=1 )
{
printf( "按其它键无效
" );
scanf( "%d", &flag );
}
if( flag==0 )
break;
goto loop;
}
}
int question_get()
{
int a,b,c;
loop:
if( type==1 )
{
a=rand()%99;
b=99-a;
b=rand()%b;
printf( "%d + %d = ?", a, b );
return(a+b);
}
else if( type==2 )
{
b=rand()%99;
c=99-b;
c=rand()%c;
printf( "%d - %d = ?", b+c, b );
return(c);
}
else if( type==3 )
{
a=rand()%10;
b=50-a;
b=rand()%b;
printf( "%d * %d = ?", a, b );
return(a*b);
}
else if( type==4 )
{
b=rand()%50;
c=100/b;
while( 1 )
{
c=rand()%c;
if( c!=0 )
break;
}
printf( "%d / %d = ?", b*c, b );
return(c);
}
else if( type==5 )
{
printf(" 退出系统
");
system("pause");
exit(0);
}
else
{
printf(" 输入错误,请输入1-5内的数字
");
printf(" 请选择(1-5):");
scanf( "%d", &type );
goto loop;
}
return -1;
}
全部回答
- 1楼网友:行路难
- 2021-04-02 03:12
你这个不是错误,是警告,是提示你你的代码中的n定义了没有使用,你把n删除掉即可去除这个警告。如下:#include
#include
#include
int question_get();
int type;
int main()
{
int answer;
srand( (unsigned)time( NULL ) );
loop:
printf( "请选择要进行测试的题目种类:" );
printf( " 1.加法运算 2.减法运算 3.乘法运算 4.除法运算 5.退出运算 " );
printf(" 请选择(1-5):");
scanf( "%d", &type );
while( 1 )
{
int temp;
int flag;
answer = question_get();
printf( "请回答: " );
scanf( "%d", &temp );
while( temp!=answer )
{
printf( " 答案错误,重做 " );
scanf( "%d", &temp );
}
printf( " 答案正确,很好 " );
printf( "继续请按1,退出请按0 " );
scanf( "%d", &flag );
while( flag!=0&&flag!=1 )
{
printf( "按其它键无效 " );
scanf( "%d", &flag );
}
if( flag==0 )
break;
goto loop;
}
return 0;
}
int question_get()
{
int a,b,c;
loop:
if( type==1 )
{
a=rand()%99;
b=99-a;
b=rand()%b;
printf( "%d + %d = ?", a, b );
return(a+b);
}
else if( type==2 )
{
b=rand()%99;
c=99-b;
c=rand()%c;
printf( "%d - %d = ?", b+c, b );
return(c);
}
else if( type==3 )
{
a=rand()%10;
b=50-a;
b=rand()%b;
printf( "%d * %d = ?", a, b );
return(a*b);
}
else if( type==4 )
{
b=rand()%50;
c=100/b;
while( 1 )
{
c=rand()%c;
if( c!=0 )
break;
}
printf( "%d / %d = ?", b*c, b );
return(c);
}
else if( type==5 )
{
printf(" 退出系统 ");
system("pause");
exit(0);
}
else if( type==0||type>5 )
{
printf(" 输入错误,请输入1-5内的数字 ");
printf(" 请选择(1-5):");
scanf( "%d", &type );
goto loop;
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯