如何修复此代码

时间:2017-03-07 02:47:39

标签: c++

请,如何修复此代码

[Error] a function-definition is not allowed here before '}' token

[Error] expected '}' at the end of input

即使我已经检查过编译器错误,我也不知道我的代码有什么问题

#include<iostream>

using namespace std;

struct name_type
{
    string first,middle,last;
};

struct SD
{
    name_type name;
    float grade;
};

const int MAX_SIZE = 35;

int isFull(int last) {
    if(last == MAX_SIZE - 1) {
        return(1);
    }
    else {
        return(0);
    }
}
int isEmpty(int last) {
    if(last < 0) {
        return(1); 
    }
    else {
        return(0);
    }
}


main()
{
    SD SD2[MAX_SIZE];
    int last = -1;

    if(isEmpty(last)) 
    {
        cout << "List is empty\n";
    }

    for (int a=0; a <35; a++)
    {
        cout << "Enter first name:.....";
        cin >> SD2[a].name.first;
        cout << "Enter middle name:....";
        cin >> SD2[a].name.middle;
        cout << "Enter last name:......";
        cin >> SD2[a].name.last;
        cout << "Enter your grade:.....";
        cin >> SD2[a].grade;
        cout << '\n';
    }
    system("cls");
    cout << "1 - Add";
    cout << "2 - Delete";
    cout << "3 - Search";
    cout << "4 - Print";
    cout << "5 - Exit";

    string lname, fname;
    int choice, search;
    cin >> choice;

    if(choice == 3) {
        cin >> fname;
        cin >> lname;
        int index = search;
        (SD2, lname, fname, last);

        if (index > 0) {
            cout << "ERROR\n";
        }
        else {
            cout << "The grade of " << lname << "," << fname << "is " << SD2[index].grade;
        }
    }

    int search(SD list [], string search_lname, string search_fname, int last) {
        int index;
        if(isEmpty(last)==1) {
            cout << "\nThe list is Empty!";
        }
        else {
            index = 0;
            while(index!= last+1 && list[index].name.first != search_fname && list[index].name.last != search_lname) {
                ++index;
            }
            if(index != last + 1) {
                cout << "\nItem Requested is Item" << index + 1 << ".";
                return index;
            }
            else {
                cout << "\n Item Does Not Exist.";
            }

        }
        return -1; // list is empty or search item does not exist
    }

}

1 个答案:

答案 0 :(得分:0)

其中一个问题在于您声明主要功能:

main()

在c ++中,main()函数必须的返回类型为int。你还没有为main()的返回值指定任何数据类型,它将返回数据类型设置为void,这会在main()之前产生错误。要了解和了解有关C ++ main()的更多信息,请访问以下链接Main Function

要对此进行排序,请将上面的代码行更改为:

int main() // notice that the return type here is int. This is required in c++

另一件事:在这些方面:

int index = search;
(SD2, lname, fname, last);

在此处,您希望将SD2lnamefnamelast传递给search()功能。但是,您的语法错误。调用时函数及其参数不能用分号分割,因为分号终止语句。因此,编译器将search视为变量,而不是函数。这与它后面的语句一起导致错误。您应该将这两行更改为:

int index = search(SD2, lname, fname, last); // this is proper syntax to call a function.

此外,您需要从main()函数中取出search()并将其放在main()函数上方。这也导致了错误。