注册 登录
编程论坛 C++教室

求大神指教error C2059: syntax error : ';'怎么办

小小旋风 发布于 2015-11-04 10:37, 760 次点击
#include<iostream.h>
int main()
{int a,b,sum;
a=2;
b=1;
sum=0;
   while(a<=1000)
   {
       while(b<a)
       {
           if(a%b==0)
           sum+=b;
           else
               goto;
           
           b++;
       }
       if(sum==a)
      cout<<a<<" ";
       else
           goto;
          a++;
   }
return 0;
}
这错在哪啊?
4 回复
#2
rjsp2015-11-04 11:18
“这错在哪啊?” ------ 编译肯定告诉你了呀,是“goto;”这一句报 error C2059: syntax error : ';'
#3
wengbin2015-11-04 11:23
你这程序问题挺多的,#include<iostream>就好,不要.h,你那个goto语句有问题,要用goto得先有个标识符,比如先定义好next然后才能用goto next;来实现,这样以来,程序走到goto next;时,会回到你定义next处往下走。还有,你没有写using namespace std;一句,你的cout能编译得过去?
#include<iostream>
int main()
{
    int a=0,i=0;
    next:
    {
        a = a + 1;
        i++;
    }
    if(i<100)
        {
            goto next;
        }
    std::cout<<a<<std::endl;
    return 0;
}
如上代码,每次程序走到goto next;时,会重新从执行a = a + 1;i++;及if判断语句块直到i=100时,if判断为假,直接执行cout打印a。

[此贴子已经被作者于2015-11-4 11:51编辑过]

#4
小小旋风2015-11-04 12:06
回复 楼主 小小旋风
谢谢版主
#5
小小旋风2015-11-04 12:07
回复 2楼 rjsp
谢谢wengbin的详细解答
1