Thread: Array of functions?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Denver
    Posts
    2

    Array of functions?

    Normally I can find answers just by searching, this time however I think the question is a little too simple. I need to build an array of functions (pointers I think).

    Code:
    #include <iostream>              
    #include <stdlib.h>
    #include <fstream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctype.h>
    
    void func1();
    void func2();
    void func3();
    
    int main(){
    
    using namespace std;  
    
     //I don't know whow to make the next line work..
    functions[3] = {func1, func2, func3};
    
    int slct;
    while(slct != 9)
    {
        system("cls");
        cout << "\t\t Menu:\n\n\t 1) Function 1\n\t 2) Function 2\n\t " <<
                "3) Function 3\n\n 9) Exit\n\n Please enter your selection: ";
        switch(slct)
        {
            case 1:
            case 2:
            case 3:
            slct --;
            functions[slct]();
            break;
            
            case 9:
            cout << "Thank you!";
            break;
            
            default:
            cout << "I'm sorry that is not a valid choice.";
            break;
        }
    
    }
    return 0;
    }
    func1(){
    cout << "This is func1.";
    }
    
    func2(){
    cout << "This is func2.";
    }
    
    func3(){
    cout << "This is func3.";
    }
    Ok, so I'm not the sharpest knife... Got to learn somehow.
    Thanks

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    You can't have an array of functions, only an array of pointers to functions.

    Just so you know...you didn't fill out the cases in your switch statement.

    Also, system() is the devil because it's system dependent, slow, and unreliable.

    And your cout statement that's all one line with a bunch of '\n's is kind of nasty looking. You might want to use the preferred 'endl' and seperate those output commands into lines of their own for better readability.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    The long way
    Code:
        void (*functions[3])(void) = {func1, func2, func3};
    The easy way (use a typedef to simplify)
    Code:
        typedef void (*fnptr)(void);
        fnptr functions[3] = {func1, func2, func3};
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Denver
    Posts
    2
    Thanks all. Yea, I wasn't too worried about the cout,,, just trying to get the idea down. Question: If not system() how??? I use "cls" a lot, and "pause" more than I should. I know I can pause the program with a simple function, but I don't know another way to clear the screen.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    More on function pointers: http://www.newty.de/fpt/index.html

    More on clearing the screen: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM