2017-03-07 29 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 
    } 

} 
を確認したにも関わらず、私のコードの問題だかわからない

+1

あなたの質問を[編集]して[mcve]を提供してください。 –

+0

コードを書くときは、小さくてシンプルなものから始めて、完全に機能してから、少しずつ複雑なものを追加してください。新しい機能を可能な限り分離して開発する。 ** – Beta

+0

main()関数は_void_型の戻り値を持つことはできません。 –

答えて

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); 

、あなたがSD2lnameを渡したい、fnameをし、 lastsearch()機能です。ただし、構文が間違っています。セミコロンでセミコロンで終了するため、呼び出されたときの関数とそのパラメータはセミコロンで分割できません。したがって、コンパイラは関数ではなく変数としてsearchを参照します。これに続くステートメントとともにエラーが発生します。あなたはにそれらの2行を変更する必要があります。

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

また、あなたは、main()関数内からsearch()を取り出し、main()関数の上にそれを配置する必要があります。これもエラーの原因となります。

関連する問題