Home > Blockchain >  How do I call an int function from a void function - USING A STRUCT?
How do I call an int function from a void function - USING A STRUCT?

Time:09-28

I am trying to run a program that calls my findRobot() function through the moveRobot() function. How do I call it?

I have spent quite some time working my parameters and nothing has seem to work. Feel free to leave any suggestions please.

struct Robot{
    int x = 0, y = 0;
    string name;
    char lastCommand;
    int curSpeed, totDist;
    int posX, posY;

    //declares starting
    Robot(){
        int x = 0, y = 0;
        posX = x;
        posY = y;
    }
};

// prototypes
void moveRobot(Robot &r, char d);
int findRobot(Robot robotList[], string &input, int &numBots);

int main() {
    // initializing struct
    Robot r;
    int numBots = 0;
    cout << "Welcome\n";
    cout << "How many robots?\n";
    cin >> numBots;

    // using an array to store robots names in struct
    // added after numBots 4 size
    Robot robotList[numBots];
    cout << "Please name each robot\n";

    // iteraties over each element at the size of numBots
    // allows user to enter 'numBots' # of names

    for(int i = 0; i < numBots; i  ){
        cin >> robotList[i].name;
    }
    cout << robotList[2].name;

    // menu option
    // outside to avoid scope issues
    char menuOption;
    do{
        cout << "M- Move one robot\n";
        cout << "D- print the distance each robot has moved\n";
        cout << "Q- quit the program \n";
        cin >> menuOption;

        switch(menuOption){
            case 'm':
            case 'M':
                moveRobot(r,'d');
                break;
            // case 'd':
            // case 'D':
                //  moveRobot(r);
                break;
            case 'q':
            case 'Q':
                cout << "Terminating Program." << endl;
                abort();
                break;
        }
    }
    while(menuOption != 'q' || menuOption != 'Q');

    return 0;
}

void moveRobot(Robot &r, char d){       
    findRobot(robotList, input, numBots);

    /*
    Deleted this function to save space --
    it is working, and my issue is how to
    call the moveRobot function from here
    */
}

/*
the point of this function is to search my array
once I create this I can then call it from my move robot function
*/

int findRobot(Robot robotList[], string &input, int &numBots){
    string userInput;
    cin >> userInput;

    if(userInput == robotList[numBots].name){
        cout << "match\n";
    }
}

CodePudding user response:

Despite the comments which suggest you to redesign your code, I would like to point some problems in it.

  1. In the constructor of Robot, I think the line int x=0, y=0 is either redundant or misused. when you write the declaration like this, the variable x and y are localized to method Robot() and have nothing to do the x and y who are member of struct Robot. If this is your intention, you really have a bad code style which will easily make others confused; also in that sense, that line is redundant as you could simply write posX=0; posY=0;. If you mean to initialize member variable x and y of struct Robot, just write x=0; y=0; posX=0; poxY=0;

  2. In main you seem to use a user input as the length of an array, this line would never compile this is not part of the C standard (see Nathan's comment below) and you may have a good lecture when writing this. A dynamic duration storage object should be declared by using new or malloc or others operators alike. like Robot *pRobotList=new Robot[numBots]; If you indeed don't want to handle with pointer, use some STL container then. As a side note, you have not checked whether the input is valid, this should be considered if you redesign your code.

  3. In your do-while loop, you seem to use abort, I suggest you use exit which handles with GC. Though today's OS will do this when a program is aborted, it's a good convention to do this on your own. Next let's talk about the loop condition, you use menuOption != 'q' || menuOption != 'Q' and this is wrong and redundant. We want to end the loop when menuOption is either Q or q, which we write as menuOption=='Q' || menuOption=='q' and we denote it as P. So the condition where loop continues should be !P. By applying distribute law, !P should be written as menuOption!='Q' && menuOption!='q'. And you program will abort when menuOption is either Q or q, which means the condition will be too late to be checked. So you could just write while(true) in this case.


As for you question, you could call a function whatever its return type is in another function. Your problem is that you just could not provide the arguments your findRobot need in your moveRobot function. Usually this means your design is bad and to avoid unnecessary parameter explosion you should carefully redesign your code.

AFAIK, you want to find the robot before moving it, so why call it in your moveRobot function. You should first let user input a robot name and you call findRobot then, which return the index of the robot to be found (in your way, int as return type) or just a reference to the robot (in your way, Robot & as parameter type); both are OK. So it should be like this:

#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;

struct Robot
{
    string name;
    char lastCommand;
    int curSpeed, totDist;
    int posX, posY;

    Robot() {
        posX = 0;
        posY = 0;
    }
};

void moveRobot(Robot &r, char d);
int findRobot(Robot robotList[], int &numBots);

int main() {
    int numBots = 0;
    cout << "Welcome\n";
    cout << "How many robots?\n";
    cin >> numBots;

    Robot *pRobotList = new Robot[numBots];
    cout << "Please name each robot\n";

    for (int i = 0; i < numBots; i  ) {
        cin >> pRobotList[i].name;
    }

    char menuOption;
    string botName;
    do {
        cout << "M- Move one robot\n";
        cout << "D- print the distance each robot has moved\n";
        cout << "Q- quit the program \n";
        cin >> menuOption;

        switch (menuOption) {
            case 'm':
            case 'M':
                {
                    int botIndex = findRobot(pRobotList, numBots);
                    moveRobot(pRobotList[botIndex], 'd');
                    break;
                }
            case 'd':
            case 'D':
                //  moveRobot(r);
                break;
            case 'q':
            case 'Q':
                cout << "Terminating Program.\n";
                exit(0);
                break;
        }
    } while (true);

    return 0;
}

void moveRobot(Robot &r, char d) {
    /* Move your robot */
}

int findRobot(Robot robotList[], int &numBots) {
    string userInput;
    cin >> userInput;

    for (int i = 0; i < numBots;   i) {
        if (robotList[i].name == userInput)
            return i;
    }

    return -1; // You should add error handling code yourself!
}

Of course, the code above should never be seen as a good design, but just one possible way to implement what the OP wants to.

  • Related