• 0

C/C++


Question

Your misison, should you choose to accept it, is to build the following.

-When the program starts, it'll ask the user to enter either a 1, 2, or a 3.

-Based on this input, one of 3 functions should execute, then the program exits.

#include <iostream.h>
void func1(void);
void func2(void);
void func3(void);
void main(void)
{
    int input;
    cout << "Enter 1, 2, or 3: ";
    cin >> input;
    //At this point your code will call one of the 3 functions.
    return;
}
void func1(void)
{
    cout << "\nFunction 1 Executed\n";
}
void func2(void)
{
    cout << "\nFunction 2 Executed\n";
}
void func3(void)
{
    cout << "\nFunction 3 Executed\n";
}

Sounds simple right? Now here's the catch, you aren't allowed to use any conditional statements, ie if, switch, ect. No condition of any varible or memory address can ever be checked. No inline assembly. No additional headers. No external code. No return values for any function. No parameters for any function.

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

That's a tough one, but the only way you'll make it work is if you do an array of those functions.

You'll need to put:

typedef void (*VoidPtrFunc)();

then do a VoidPtrFunc array with each of the entries pointing to the different functions, the just do array();

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.