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.
In the constructor of
Robot
, I think the lineint x=0, y=0
is either redundant or misused. when you write the declaration like this, the variablex
andy
are localized to methodRobot()
and have nothing to do thex
andy
who are member ofstruct 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 writeposX=0; posY=0;
. If you mean to initialize member variablex
andy
ofstruct Robot
, just writex=0; y=0; posX=0; poxY=0;
In
main
you seem to use a user input as the length of an array,this line would never compilethis 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 usingnew
ormalloc
or others operators alike. likeRobot *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.In your
do-while
loop, you seem to useabort
, I suggest you useexit
which handles with GC. Though today's OS will do this when a program isabort
ed, it's a good convention to do this on your own. Next let's talk about the loop condition, you usemenuOption != 'q' || menuOption != 'Q'
and this is wrong and redundant. We want to end the loop whenmenuOption
is eitherQ
orq
, which we write asmenuOption=='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 asmenuOption!='Q' && menuOption!='q'
. And you program willabort
whenmenuOption
is eitherQ
orq
, which means the condition will be too late to be checked. So you could just writewhile(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.