2012-04-09 70 views
1

我正在用C++編寫一個程序,並且我已經能夠讓它編譯並開始運行,當我選擇一個選項時應該調用coresponding函數不會調用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()之後。 – 2012-04-09 16:46:41

+0

@Amardeep:他在代碼註釋中提到聲明高於主要,他似乎只是在如何調用函數時感到困惑。 – josephthomas 2012-04-09 16:48:21

+0

這些評論看起來像樣板。我確定他是否知道原型是什麼,他不會犯他犯的錯誤。 – 2012-04-09 16:49:43

5

你實際上只是聲明功能,而不是調用他們。

void displaynames(); 

聲明函數。

displaynames(); 

調用功能。

+0

我需要在哪裏聲明該功能?我試圖在main之前聲明它們,它不會編譯。 – user1319815 2012-04-09 18:17:51

+0

@ user1319815:*在'main()'之前聲明*它們並*在你的'switch'-'case'中調用它們。 – 2012-04-09 18:19:54

2

在你的代碼塊(switch)中,你沒有調用任何函數,而只是聲明它們。

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

移動(向前)聲明(void displaynames();)到源文件的頂部水平(你定義使用後的功能),然後使用正常功能應用語法打電話給他們(displaynames();) 。

somewhere在網上:

「一個函數聲明也被稱爲原型,它 通知你的意圖的編譯器定義和使用它 定義一個函數。與原型相關的主體(代碼)「。