0

I have a project that asks the user how many robots they want to create, the user then names the robots and has the option to move the robots in one of 4 directions along the X/Y axis.

The program allows the user to move one robot in one direction per turn: up, down, left, or right. If the user chooses up, they will move up one unit. If the user continues to choose one direction consecutively, they will be able to move farther in that direction. For example, if a user moves up, they will move up one unit. If the user moves up again, they will move up 2 units. If the user moved up for the third time, they will move up 3 units. Users can do this a maximum of 4 times in a row, meaning that they can move a maximum of 4 units per turn, even if the user goes in one direction for 4, 7, 18, etc times in a row. This is pattern is broken and starts again if the user chooses a different direction.

So far I have only tried test cases but it doesn't seem like my speed variable is increasing. I tried to make it to where if userDirection is the same as lastCommand, a variable called speed will increase by one, in theory, increasing each time the user moves the robot in the same direction. However, this is not the case. speed is only increasing to 1 for some reason.

I want to test the code by only going up. Moving one robot only going up, it should show (0,1), then (0,3), then (0,6), then (0,10), (0,14), (0,18) and so on, first increasing by one, then two, then three, and maxing out at moving 4 units per turn. However, what I am finding is that the code goes from (0,1) to (0,3), to (0,5), (0,7), (0,9), and so on. Maxing out at only moving 2 units per turn.

I need to know why speed is not updating even though it has ++ attached to it. If there is any other advice you can give me for my code, it would be greatly appreciated. Thank you!

#include <iostream>

#include <vector>

#include <string>

using namespace std;

struct userRobot {
  string name;
  int Xvalue = 0;
  int Yvalue = 0;
  int distance = 0;
  char lastCommand;
};

void MenuFunction() {
  cout << "Please select one of the following:" << endl;
  cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}

void MoveFunction(userRobot RobotArray[], int NumberOfRobots) {
  cout << endl << "Which robot would you like to move?" << endl << endl;

  string robotName;
  cin >> robotName;

  for (int k = 0; k < NumberOfRobots; k++) {
    if (robotName == RobotArray[k].name) {
      cout << endl << "In what direction would you like to move " << RobotArray[k].name << "?" << endl;
      cout << "Select: u - up / d - down / l - left / r - right" << endl << endl;
      char userDirection;
      cin >> userDirection;
      int speed = 0;

      if (userDirection == 'u' || userDirection == 'U') {
        if (userDirection == RobotArray[k].lastCommand) {
          speed++;
          if(speed > 4)
          {
              speed = 4;
          }
          RobotArray[k].Yvalue += speed;
        }
        RobotArray[k].Yvalue++;
        RobotArray[k].distance++;
        cout << endl << RobotArray[k].name << "'s position has been updated to ";
        cout << "(" << RobotArray[k].Xvalue << "," << RobotArray[k].Yvalue << ")" << endl << endl;
        RobotArray[k].lastCommand = 'u';
      } else if (userDirection == 'd' || userDirection == 'D') {
        RobotArray[k].Yvalue--;
        RobotArray[k].distance++;
        cout << endl << RobotArray[k].name << "'s position has been updated to ";
        cout << "(" << RobotArray[k].Xvalue << "," << RobotArray[k].Yvalue << ")" << endl << endl;
      } else if (userDirection == 'l' || userDirection == 'L') {
        RobotArray[k].Xvalue--;
        RobotArray[k].distance++;
        cout << endl << RobotArray[k].name << "'s position has been updated to ";
        cout << "(" << RobotArray[k].Xvalue << "," << RobotArray[k].Yvalue << ")" << endl << endl;
      } else if (userDirection == 'r' || userDirection == 'R') {
        RobotArray[k].Xvalue++;
        RobotArray[k].distance++;
        cout << endl << RobotArray[k].name << "'s position has been updated to ";
        cout << "(" << RobotArray[k].Xvalue << "," << RobotArray[k].Yvalue << ")" << endl << endl;
      } else {
        cout << endl << "Sorry, that is not a valid direction, please input either 'u', 'd', 'l', or 'r'" << endl;
        cout << "Returning to main menu..." << endl << endl;
      }

      return;
    }
  }

  cout << endl << "Sorry, we couldn't find that robot. Kindly check your spelling and try again." << endl;
  cout << "Returning to main menu..." << endl << endl;
}

int main() {
  cout << "Enter the number of robots" << endl << endl;

  int NumberOfRobots;
  cin >> NumberOfRobots;

  vector < userRobot > RobotArray(NumberOfRobots);

  cout << endl << "Enter their name(s)" << endl << endl;
  for (int i = 0; i < NumberOfRobots; i++) {
    cin >> RobotArray[i].name;
  }

  cout << endl;

  cout << "Welcome to MultiRobo Guider!" << endl;
  MenuFunction();
  char input;
  cin >> input;

  while (input != 'q' && input != 'Q') {
    if (input == 'm' || input == 'M') {
      MoveFunction(RobotArray.data(), NumberOfRobots);
      MenuFunction();
      cin >> input;
    } else if (input == 'd' || input == 'D') {
      for (int j = 0; j < NumberOfRobots; j++) {
        cout << endl << RobotArray[j].name << " has moved a total of " << RobotArray[j].distance << " time(s)";
        cout << endl << RobotArray[j].name << "'s position is (" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl;
      }

      cout << endl << endl;
      MenuFunction();
      cin >> input;
    } else {
      cout << endl << "Sorry, that is not a valid menu input, please input either 'm', 'd', or 'q'" << endl << endl;
      MenuFunction();
      cin >> input;
    }
  }
  cout << endl << "Goodbye, and thank you for using MultiRobo Guider!";
}
  • What did your debugger say? – lorro Sep 28 '22 at 07:33
  • 9
    `int speed = 0;` is inside the loop. It it incremented at most once. – 463035818_is_not_an_ai Sep 28 '22 at 07:36
  • If robots have a speed (which I think is what you are saying) then that needs to be stored in the robot array, not in a variable local to your loop. As said above, you set the speed to zero, then you increment it at most once, so clearly it's going to have a value of at most one. – john Sep 28 '22 at 07:37
  • @lorro I'm sorry, this is only my first semester of Computer Science, I dont think I know how to work the debugger on OnlineGDB. But I am getting no error codes. Sorry if this doesn't help :( from what I can tell the debugger is not saying anything – ashley prevost Sep 28 '22 at 07:37
  • @ashleyprevost Learning to use a debugger should be a high priority. I'm slightly alarmed that the only compiler you have access to is an online one. Is that really true? – john Sep 28 '22 at 07:39
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Sep 28 '22 at 09:06

1 Answers1

0
struct userRobot {
  string name;
  int Xvalue = 0;
  int Yvalue = 0;
  int distance = 0;
  char lastCommand;
  int speed = 0; 
};

To increase the speed variable you have to make speed variable in the structure and don't initialize its value in the for loop.

For the problem with 'u' .

if (userDirection == 'u' || userDirection == 'U') {
        if (userDirection == RobotArray[k].lastCommand) {
          speed++;
          if(speed > 4)
          {
              speed = 4;
          }
          RobotArray[k].Yvalue += speed;
        }
        RobotArray[k].Yvalue++;
        RobotArray[k].distance++;
        cout << endl << RobotArray[k].name << "'s position has been updated to ";
        cout << "(" << RobotArray[k].Xvalue << "," << RobotArray[k].Yvalue << ")" << endl << endl;
        RobotArray[k].lastCommand = 'u';
      }

You have added Robot[k].Yvalue 2 times in the if loop.

RobotArray[k].lastCommand = 'u'; as you have declared this command only during the if loop of 'u' case. the if loop will always run after first time. which will increase the value of speed to 1 and RobotArray[k].Yvalue += speed; this statement will commence and RobotArray[k].Yvalue++; this as well increasing value of y axis twice.

to make the code more easier you can make a seperate function for this

        RobotArray[k].distance++;
        cout << endl << RobotArray[k].name << "'s position has been updated to ";
        cout << "(" << RobotArray[k].Xvalue << "," << 
        RobotArray[k].Yvalue << ")" << endl << endl;
        RobotArray[k].lastCommand = 'u';

Passing args of array of struct, k and UserDirection

  • if the code is run **again** the speed variable reintialize to zero in the for loop and if pressed 'u' **again**. if loop makes the speed to 1 and will increase the value of y axis by 2 – Moksh Bansal Sep 28 '22 at 09:45