2017-03-07 1 views
-4

하십시오,이 코드이 코드를 수정하는 방법

를 해결하는 방법
[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() 

에서 : 문제의

+1

[mcve]를 제공하기 위해 질문을 편집하십시오. –

+0

코드를 작성할 때 완벽하게 작동하는 작고 간단한 것으로 시작한 다음 한 번에 조금씩 복잡성을 추가하십시오. 가능한 한 격리 된 새로운 기능을 개발하십시오. 모든 단계에서 테스트하고 ** 작동하지 않는 코드에 ** 추가하지 마십시오 ** – Beta

+0

main() 함수는 _void_ 유형의 반환 값을 가질 수 없습니다. –

답변

0

하나는 주요 기능의 당신의 선언에 C++의 경우 main() 함수 의 반환 유형이 int이어야합니다. 반환 값이 main() 인 데이터 형식을 지정하지 않은 Sin 반환 데이터 형식을 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, fnamelast ~ search() 기능. 그러나 구문이 잘못되었습니다. 세미콜론이 명령문을 종료하기 때문에 함수와 매개 변수를 호출 할 때 세미콜론으로 분할 할 수 없습니다. 따라서 컴파일러는 search을 변수가 아니라 함수로 간주합니다. 이것은 다음에 나오는 명령문과 함께 오류를 발생시킵니다.

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

는 또한, 당신은 main() 함수 내부에서 search()을 꺼내 main() 함수 위에 배치해야합니다 당신은에 사람들이 줄을 변경해야합니다. 또한 오류가 발생합니다.

관련 문제