Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

c++ - I'm trying to call void functions in another function, but I don't know how to properly place them

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

// these should have all of my declared functions

int start(char x, char y, char z);
void pim();
void pmm();
void login();
void createa();

//my assignment wants me to have only one function used

int main()
{
    start(pim(), login(), createa());

    return 0;
}

//This should be the start of the code

void pim()
{
    cout << "Please select a letter from the menu below:" << endl;
    cout << "l - Login" << endl;
    cout << "c - Create New Account" << endl;
    cout << "q - Quit" << endl;
}

// this is to post a menu after you make the first choice above

void pmm()
{
    cout << "d - Deposit Money" << endl;
    cout << "w - Withdraw Money" << endl;
    cout << "r - Request Balance" << endl;
    cout << "q - Quit" << endl;
}

//this is to login to the account

void login()
{
    cout << "Okay, you're in" << endl;
}

// this is to create an account

void createa()
{
    int id = 0;
    int password = 0;

    cout << "create an ID of two numbers" << endl;
    cin >> id;

    cout << "Make a password of 4 numbers" << endl;
    cin >> password;
}

// Starts and is basically the entire project

int start(char x, char y, char z)
{
    char pim = x;
    cout << pim;

    int choice = 0;
    char select = '';

    cout << "Enter the choice that you want:";
    cin >> choice;

    if(select == 'l')
    {
        choice = 1;  
    }
    else if( select == 'c')
    {
        choice = 2;
    }
    else
    {
        choice = 3;
    }

    switch (choice)
    {
        case 1:
            cout << y << endl;
            break;
        case 2:
            cout << z << endl;
            break;
        case 3:
            exit(0);
            break;
        default:
            cout << "This is not a command" << endl;
    }

    return 0;

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think what you are looking for is function pointers, eg:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

// these should have all of my declared functions

int start(void (*x)(), void (*y)(), void (*z)());
void pim();
void pmm();
void login();
void createa();

//my assignment wants me to have only one function used

int main()
{
    start(&pim, &login, &createa);

    return 0;
}

//This should be the start of the code

void pim()
{
    cout << "Please select a letter from the menu below:" << endl;
    cout << "l - Login" << endl;
    cout << "c - Create New Account" << endl;
    cout << "q - Quit" << endl;
}

// this is to post a menu after you make the first choice above

void pmm()
{
    cout << "d - Deposit Money" << endl;
    cout << "w - Withdraw Money" << endl;
    cout << "r - Request Balance" << endl;
    cout << "q - Quit" << endl;
}

//this is to login to the account

void login()
{
    cout << "Okay, you're in" << endl;
}

// this is to create an account

void createa()
{
    int id = 0;
    int password = 0;

    cout << "create an ID of two numbers" << endl;
    cin >> id;

    cout << "Make a password of 4 numbers" << endl;
    cin >> password;
}

// Starts and is basically the entire project

int start(void (*x)(), void (*y)(), void (*z)())
{
    x();

    char choice = '';

    cout << "Enter the choice that you want:";
    cin >> choice;

    switch (choice)
    {
        case 'l':
            y();
            break;
        case 'c':
            z();
            break;
        case 'q':
            exit(0);
            break;
        default:
            cout << "This is not a command" << endl;
    }

    return 0;
}

However, unless "function pointers" is the actual topic you are studying at the moment, then this is a very questionable design, even for a simple homework assignment. It would be cleaner and easier to read/manage to just call the functions directly instead, eg:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

// these should have all of my declared functions

int start();
void pim();
void pmm();
void login();
void createa();

//my assignment wants me to have only one function used

int main()
{
    start();

    return 0;
}

//This should be the start of the code

void pim()
{
    cout << "Please select a letter from the menu below:" << endl;
    cout << "l - Login" << endl;
    cout << "c - Create New Account" << endl;
    cout << "q - Quit" << endl;
}

// this is to post a menu after you make the first choice above

void pmm()
{
    cout << "d - Deposit Money" << endl;
    cout << "w - Withdraw Money" << endl;
    cout << "r - Request Balance" << endl;
    cout << "q - Quit" << endl;
}

//this is to login to the account

void login()
{
    cout << "Okay, you're in" << endl;
}

// this is to create an account

void createa()
{
    int id = 0;
    int password = 0;

    cout << "create an ID of two numbers" << endl;
    cin >> id;

    cout << "Make a password of 4 numbers" << endl;
    cin >> password;
}

// Starts and is basically the entire project

int start()
{
    pim();

    char choice = '';

    cout << "Enter the choice that you want:";
    cin >> choice;

    switch (choice)
    {
        case 'l':
            login();
            break;
        case 'c':
            createa();
            break;
        case 'q':
            exit(0);
            break;
        default:
            cout << "This is not a command" << endl;
    }

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...