lundi 27 mai 2019

Pass a function with multiple parameters as an argument

I would like to call a function with multiple parameters as an argument of another function. The assignment asks to pass the counted value(which just so happens to be the same value that is returned from the type integer read_Events function ) as an argument of another function. Although I could just call the function and store it and pass it, I figured it might be a little cleaner to pass the whole function as an argument.

This is for a project for a previous class that I am revising for learning purposes. So far I've tried to store the address of function 2 into a pointer and pass the pointer storing the address of function 2 as an argument of function 1. I've also tried to pass the entirety of function 2 an argument of function 1.

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
struct Date {
    int month;
    int day;
    int year;
};
struct Time {
     int hour;
     int minute;
};
struct Event {
    char desc[80];
    Date date;
    Time time;
};

int read_Events(Event* Eptr[50], int MAX);
void display(Event* Eptr[50],int(*read_events)(Event , int )) //1. can i 
do this?;
int main() {
    Event* Eptr[50];
     int *Fptr = &read_Events;//#2 Is this possible? (function address 
to pass as arugement in display function)

    _getch();
    return 0;
}

int read_Events(Event* Eptr[50], int MAX) {
    bool repeat = true;
    char ans;
    char slash;
    char colon;
    int i = 0;

    while (repeat && i < MAX) {
        cout << "\nWould you like to add a new event [y/n]? ";
        cin >> ans;

        if (ans == 'Y' || 'y') {
            Eptr[i] = new Event;
            repeat = true;

            cout << "\nEnter description: ";
            cin.clear();
            cin.ignore(256, '\n');
            cin.getline(Eptr[i]->desc, sizeof(Eptr[i]->desc));

            cout << "\nEnter Date(MM/DD/YYYY): ";
            cin >> Eptr[i]->date.month >> slash >> Eptr[i]->date.day >> 
slash >> Eptr[i]->date.year;

            cout << "\nEnter time(HH:MM): ";
            cin >> Eptr[i]->time.hour >> colon >> Eptr[i]->time.minute;
        }
        else {
            repeat = false;
            return i;
        }
            i++;
        }
        return i; //returning number of events
}

It seems as if what I have done so far is not syntactically correct.

Aucun commentaire:

Enregistrer un commentaire