如何解决这个代码
问题描述:
请,如何解决这个代码如何解决这个代码
[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
}
}
做
答
其中一个问题是你的主要功能声明:
main()
在C++,main()
函数必须返回类型为int
。 Sin您没有为返回值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);
在这里,你想传递SD2
,lname
,fname
和last
至search()
函数。但是,你的语法是错误的。被调用的函数及其参数不能用分号拆分,因为分号会终止语句。因此,编译器将search
视为变量,而不是函数。这与它后面的语句一起导致错误。你应该改变那些2线:
int index = search(SD2, lname, fname, last); // this is proper syntax to call a function.
此外,您还需要从main()函数里面拿出search()
并把它上面的main()函数。这也是一个错误。
请编辑您的问题以提供[mcve]。 –
当你编写代码时,从小而简单的东西开始,完美地工作,然后一点一点地增加复杂度。尽可能单独开发新功能。在每一步都进行测试,并且不要将代码添加到不工作的代码中。** – Beta
main()函数不能有_void_类型的返回值。 –