Load functions using a case switch statement

11,043

Solution 1

You are actually just declaring functions instead of calling them.

void displaynames();

Declares a function.

displaynames();

Calls a function.

Solution 2

You are calling the functions wrong. It should be

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

To call a function, you just need the function name and the function parameters (which in this case, seem to be none. The way you currently have it is declaring a function rather than calling a function.

Solution 3

In your code block (switch), you're not calling any functions, but rather just declaring them.

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

Move the (forward) declarations (void displaynames();) to the top level of your source file (as you're defining the functions after using them), and then call them using the normal function application syntax (displaynames();).

From somewhere on the net:

"A declaration for a function is also called a prototype and it informs the compiler of your intent to define and to use it. A definition for a function is the body (code) associated with the prototype."

Share:
11,043
user1319815
Author by

user1319815

Updated on June 04, 2022

Comments

  • user1319815
    user1319815 almost 2 years

    I'm writing a program in C++ and I've been able to get it to compile and start to run how ever when I choose an option the coresponding function that is supposed to be called by a switch-case statement isn't called. Am I missing something in my code?

    //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;
    }
    
  • Amardeep AC9MF
    Amardeep AC9MF about 12 years
    Further, the syntax originally used is called a function declaration and will need to come before the functions are actually called since the function definitions come after the main() where they are used.
  • josephthomas
    josephthomas about 12 years
    @Amardeep: He mentions in his code comments that the declarations are above main, he seems to just be confused on how to call the functions.
  • Amardeep AC9MF
    Amardeep AC9MF about 12 years
    Those comments look like boilerplate. I'm sure if he knew what a prototype was he would not have made the mistake he did.
  • user1319815
    user1319815 about 12 years
    where do I need to declare the function? I tried declaring them before the main and it wouldn't compile.
  • Alok Save
    Alok Save about 12 years
    @user1319815: Declare them before the main() and call them inside your switch-case.