c ++编译错误/语法错误?错误原因未知

时间:2013-01-04 09:07:48

标签: visual-c++

所以我真的不知道我的代码有什么问题。任何建议都会有帮助。查看我评论的代码部分(朝向中间)。计算机给我一个错误,说预期“;”。支架有问题,或者我搞砸了其他地方,但却找不到它。

//Experiment2
//Creating functions and recalling them.
#include <iostream>
using namespace std;

void a()
{
cout<<"You try running but trip and fall and the crazy man kills you!!!!                     HAAHAHAHHAHA.";
}


void b()
{
cout<<"You stop drop and roll and the crazy man is confused by this and leaves you alone!";
}

void c()
{
cout<<"you try fighting the man but end up losing sorry!";
}




int main()
{
int a;
int b;
int c;
int d;
a=1;
b=2;
c=3;

cout<< "Once upon a time you was walking to school,\n";
cout<< " when all of the sudden some crazy guy comes running at you!!!!"<<endl;
cout<< " (This is the begining of your interactive story)"<<endl;
cout<< "Enter in the number according to what you want to do.\n";
cout<< " 1: run away, 2:stop drop and roll, or 3: fight the man."<<endl;
cin>>d;
void checkd()
//i dont know whats wrong with the bracket! the computer gives me an error saying expected a";"
{
    if(d==1)

    {
        void a();
    }

    if(d==2)

    {
        void b();
    }

    if(d==3)

    {
        void c();
    }
}
}

1 个答案:

答案 0 :(得分:1)

您无法在其他功能中定义功能。您在checkd()函数中定义了函数main

将函数体移到main之外,只需将main中的函数调用为:

checkd(d);

或许,您还希望函数采用需要比较的参数。

另外,

void a();

不调用函数a()它只是声明函数,调用你需要的函数:

a();

void checkd(int d)

{
    if(d==1)

    {
        a();
    }

    if(d==2)

    {
        b();
    }

    if(d==3)
    {
        c();
    }
}
int main()
{
    ....
    ....
    cout<< " 1: run away, 2:stop drop and roll, or 3: fight the man."<<endl;
    cin>>d;
    checkd();

    return 0;
}