2012-04-09 9 views
1

私はC++でプログラムを作成しています。これをコンパイルして呼び出すことができます。 switch-caseステートメントは呼び出されません。私のコードで何かが欠けていますか?case switchステートメントを使用して関数をロードする

//The following program framework is given. 
//Add the programming logic to complete the assignment. 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
using namespace std; 
//function prototypes should be placed here 
//Main must be the first function in the program. Write other functions after it. 
int main() 
{ 
char cInput; 
string strFileName; 
vector<string> vecStudent; 
cout<<"Please enter the data file name (with location): "; 
cin >> strFileName; 
//call a function to read the content of the input file into 
//the vector vecStudent 
while (true) 
{ 
cout<<"----------------------------------------"<<endl; 
cout<<" Student Record - Main Menu "<<endl; 
cout<<"----------------------------------------"<<endl; 
cout<<" Enter 1 to display ALL students"<<endl; 
cout<<" Enter 2 to add a student name"<<endl; 
cout<<" Enter 3 to delete a student name"<<endl; 
cout<<" Enter 4 to SAVE and quit the program"<<endl; 
cout<<"----------------------------------------"<<endl; 
cout<<"Enter menu option: "; 
cin>>cInput; 
switch (cInput) 
{ 
case '1': 
//call function display names 
    void displaynames(); 
break; 
case '2': 
    void addname(); 
//call a function add name 
break; 
case '3': 
    void deletename(); 
//call function delete names 
break; 
case '4': 
    void saveandquit(); 
//call function save and quit 
return 0; 

if(cInput != 1,2,3,4) 
cout<<"invalid input"<<endl; 
break; 
} 
} 
return 0; 
} 

int displaynames() 
{ 
    ifstream inFile; 
    ofstream outFile; 
    string strFileName; 
    string strFName,strLName; 
    vector<string> vecStudent; 
    char line[80]; 

    // open input file 
    inFile.open(strFileName.c_str()); 
    if (inFile.fail()) 
    { 
     cout << " Input file error!" << endl; 
      return -1; 
    } 
    while (inFile>>strFName>>strLName) 
     vecStudent.push_back(strFName+ " "+strLName); 
    inFile.close(); 

    //display the content of the vector 
    for(int i =0; i< vecStudent.size();i++) 
     cout<<vecStudent[i]<<endl; 
return 0; 
} 

int addname() 
{ 
    ifstream inFile; 
    ofstream outFile; 
    string strFileName; 
    string strFName,strLName; 
    vector<string> vecStudent; 
    char line[80]; 
    //add a new name 
    cout << endl<< " Enter a new name(First and Last Name):"; 
    cin>>strFName>>strLName; 
    vecStudent.push_back(strFName+ " "+strLName); 

    // open output file for writing 
    outFile.open(strFileName.c_str()); 
    if (outFile.fail()) 
    { 
     cout<<" Output file error! Student was not added"<<endl; 
     return -1; 
    } 

    //display the content of the vector 
    for(int i=0; i<vecStudent.size(); i++) 
     cout<< vecStudent[i]<<endl; 

    for(int i=0; i<vecStudent.size();i++) 
     outFile<<vecStudent[i]<<endl; 
    outFile.close(); 
return 0; 
} 


int saveandquit() 
{ 
    ifstream inFile; 
    ofstream outFile; 
    string strFileName; 
    string strFName,strLName; 
    vector<string> vecStudent; 
    int i=0; 
    char line[80]; 
    //  open output file for writing 
    outFile.open(strFileName.c_str()); 
    if (outFile.fail()) 
    { 
     cout<<" Output file error!"<<endl; 
     return -1; 
    } 

    //display the content of the vector 
    for(int i=0; i<vecStudent.size(); i++) 
     cout<< vecStudent[i]<<endl; 

    for(int i=0; i<vecStudent.size();i++) 
     outFile<<vecStudent[i]<<endl; 
    outFile.close(); 
    cout << " file saved. enter -1 to quit"; 
    cin>> i; 
    if(i=-1) 


    return 0; 
} 



int deletename() 
{ 
    ifstream inFile; 
    ofstream outFile; 
    string strFileName; 
    string strFName,strLName; 
    vector<string> vecStudent; 
    int namepos = 0; 
    char line[80]; 

    inFile.open(strFileName.c_str()); 
    if (inFile.fail()) 
     cout <<"Input file error!"<<endl; 

    //read the names from the file into the vector 
    while (inFile >> strFName >> strLName) 
     vecStudent.push_back(strFName+" "+strLName); 
    inFile.close(); 

    cout <<"\nEnter the name to be deleted (First name and Last name): "; 
    cin >>strFName >>strLName; 

    int i=0, pos=-1; 
    int size = vecStudent.size(); 
    bool found=false; 
    // use a linear search to find the name in the vecotor of names 

    while (i < size && !found) 
    { 
     if (vecStudent [i] == strFName+" "+strLName) 
     { 
      found = true; 

    cout <<"\nthat name is in the "<<(pos + 1) <<" position in the list\n"; 
    cout <<"Please enter the position in list\n"; 
    cin>> pos; 
    // use an iterator to delete name from vecStudent. vector.erase requires an iterator. used a while loop to find the name and make sure it was in the 
    // vector of strings. then the loop displays the position in the vector that the string is. the program asks the user to enter the number position of the name 
    // from there the user enters the name and the program uses a for loop to find the position and the built in vector.erase to remove the name from the list. 
    for(int i=0; i ==pos; i++) 
    { 
     if(i == pos) 
     { 
      vecStudent.erase (vecStudent.begin()); 
     } 
    } 
    } 
    } 

    return 0; 
} 

答えて

4

関数を間違って呼び出しています。この関数を呼び出すには

case '1': 
//call function display names 
    displaynames(); 
break; 
case '2': 
    addname(); 
//call a function add name 
break; 
case '3': 
    deletename(); 
//call function delete names 
break; 
case '4': 
    saveandquit(); 

でなければなりません、あなただけの関数名と、この場合には、何もないように見える関数パラメータを(必要があります。あなたは現在、それは関数を宣言ではなく、呼び出している必要があり方法機能。

+0

また、元々使用されていた構文は関数宣言と呼ばれ、関数定義が実際に呼び出される前に関数定義がmain()の後に来る必要があります。 –

+0

@Amardeep:彼は、宣言がメインの上にあるというコード・コメントで言及していますが、関数を呼び出す方法を混乱させるようです。 – josephthomas

+0

これらのコメントは定型文のようです。私はプロトタイプが何であるかを知っていれば、間違いを犯さなかったでしょう。 –

5

あなたが実際にちょうどそれらを呼び出す機能の代わりに、を宣言している。

void displaynames(); 

を宣言する。

関数を呼び出します
displaynames(); 

+0

ここで関数を宣言する必要はありますか?私はメインの前にそれらを宣言してコンパイルしませんでした。 – user1319815

+0

@ user1319815:* 'main()'の前に*宣言し、 'switch'-'case'の中で*呼び出す*。 –

2

コードブロック(スイッチ)では、関数を呼び出すのではなく、宣言するだけです。

... 
case '1': 
//call function display names 
    void displaynames(); 
break; 
... 

移動し、その後(前方)ソース・ファイルのトップレベルに宣言(void displaynames();)(あなたがそれらを使用した後の関数を定義しているとして)、および正常な機能のアプリケーションの構文を使用して(displaynames();)にそれらを呼び出します。ネット上somewhereから

「関数の宣言はまた、プロトタイプと呼ばれ、それ は定義し、それを使用するように意図のコンパイラに通知機能のための 定義があります。プロトタイプ に関連付けられたボディ(コード)。

関連する問題