在C++中,可以写 if(cin>>"over") 么? 还有怎么用goto实现跳转?(请举例说明) 急!!!!!
答案:3 悬赏:50
解决时间 2021-01-12 05:21
- 提问者网友:雪舞兮
- 2021-01-11 20:31
在C++中,可以写 if(cin>>"over") 么? 还有怎么用goto实现跳转?(请举例说明) 急!!!!!
最佳答案
- 二级知识专家网友:不如潦草
- 2021-01-11 21:47
if(cin>>"over") 么? 还有怎么用goto实现跳转
string s;
cin>>s;
if(s=="over")cout<<"over!"<
int a;
int b;
start:
{ // 注意不能跳过构造函数 。
//在跳转的路径里, 比如start:到goto start之间,不能有变量定义;除非被{}括起来
goto start;
goto end;//goto end:到 end之间,同样不能有变量定义;除非被{}括起来
}
。。。。
end:
}
C++ goto 不是很好用!
因为经常会有这样的代码,到处都是这样的代码;
int a;
string s(a);
问题是这样的:
int b=5;
start:
{
int a=0;
a++;
if(--b)goto start:
a+=10;
goto end;
int n=10;
printf("a=%d",a);//a 到底怎样计算????
}
end:
printf("n=%d",n);//n 到底怎样计算????
string s;
cin>>s;
if(s=="over")cout<<"over!"<
int a;
int b;
start:
{ // 注意不能跳过构造函数 。
//在跳转的路径里, 比如start:到goto start之间,不能有变量定义;除非被{}括起来
goto start;
goto end;//goto end:到 end之间,同样不能有变量定义;除非被{}括起来
}
。。。。
end:
}
C++ goto 不是很好用!
因为经常会有这样的代码,到处都是这样的代码;
int a;
string s(a);
问题是这样的:
int b=5;
start:
{
int a=0;
a++;
if(--b)goto start:
a+=10;
goto end;
int n=10;
printf("a=%d",a);//a 到底怎样计算????
}
end:
printf("n=%d",n);//n 到底怎样计算????
全部回答
- 1楼网友:舍身薄凉客
- 2021-01-12 00:14
cin>>"over"有语法错误,
if(cin>>a)就不会出错,a是一个变量。
C++很少用到goto语句了,用循环语句和break,continue基本就可以代替goto了
if(cin>>a)就不会出错,a是一个变量。
C++很少用到goto语句了,用循环语句和break,continue基本就可以代替goto了
- 2楼网友:北方的南先生
- 2021-01-11 23:06
不能这样写的,你可以这样.
char buf[32];
cin>>buf;
if(strcmp(buf,"over") == 0) //等于0,表示这两个字符串的值相等,否则 不相等.
{
}
//goto 跳转
int main()
{
lop: //这里写一个goto的标志
cout <<"Hello"< goto lop; //这里跳转到lop这个标志,(程序里最好别使用goto)
return 0;
}
char buf[32];
cin>>buf;
if(strcmp(buf,"over") == 0) //等于0,表示这两个字符串的值相等,否则 不相等.
{
}
//goto 跳转
int main()
{
lop: //这里写一个goto的标志
cout <<"Hello"<
return 0;
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯