首页 > 解决方案 > 'go' 未在此范围内声明(C++)

问题描述

我尝试过声明但仍然错误,我尝试了其他几种方法但仍然错误,你能帮帮我吗?对不起,我是编程 的新手,没有在这个范围内声明我该怎么办?

这段代码

#include <iostream>
#include <stdio.h>
#include <conio.h>
void push (void);
void pop (void);
void gotoxy(int x, int y);

int x, top;
int s [5], N=5;

main()
{
    char pilih;
    char barloop;
      system ("cls");
      gotoxy ( 25,7 ); puts ("coba stack");  ;
      gotoxy ( 25,10 ); puts ("1.  push");
      gotoxy ( 25,13 ); puts ("2.  pop");   
      gotoxy ( 25,16 ); puts ("3.  exit");
      gotoxy ( 25,19 );  printf("Pilih :");
      scanf (" %x  " , &pilih);
      switch(pilih)
      {
        case 1: printf ("\n masukkan data x=;"),
                scanf (" ");    push(); getch();  break;
        case 2: pop (); getch(); break;
        case 3: exit(0);
      }
     go to char barloop;
    }

void pop (void)
{
    if  (top > 0)
    {
        
    }
    else { printf("\n\r stack kosong"); }
    }

谢谢你

标签: c++compiler-errorsscopestack

解决方案


  • 首先,go toC++ 中没有关键字。它goto没有空间。

  • 其次,您的goto句子在语法上不正确。goto语法如下所示:

dothisagian: // this is a statement label
// code
goto dothisagian;

它不会神奇地跳到你写的那一行。它跳转到语句标签。

  • 第三,你必须写出返回值main()是什么。

因此,您的代码应如下所示:

int main() // main returns an int
{
doThisAgain:
    char barloop;
    // code
    goto doThisAgain;
}
  • 最后一点:除非必要,否则不应真正goto在 C++ 中使用。您应该改用三种类型的循环之一。

推荐阅读