簡體   English   中英

我正在嘗試在另一個 function 中調用 void 函數,但我不知道如何正確放置它們

[英]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 = '\0';

    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;

}

我認為您正在尋找的是function 指針,例如:

#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 = '\0';

    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;
}

但是,除非“函數指針”是您目前正在學習的實際主題,否則這是一個非常值得懷疑的設計,即使對於簡單的家庭作業也是如此。 直接調用函數會更簡潔,更容易閱讀/管理,例如:

#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 = '\0';

    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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM