0

I am currently coding a program that will allow a user to input a direction and move a robot along a coordinate plane. I am new to C++, so a few of the errors I am getting are confusing me. Could someone please explain my errors?

When I call function init_room, it says there are too few arguments. The same thing happens when I call init_robot. They both have pointer parameters. How would I go about fixing this issue?

When I call function move in the main function, it says the expression must be a modifiable value. What does this mean?

Thanks for the help!

    #include <iostream>

using namespace std;

typedef struct room {
    int x;
    int y;
};

typedef struct robot {
    room *current_room;
    int current_x;
    int current_y;
    int model;
};

void init_robot(robot *r) {
    r->current_room->x;
    r->current_room->y;
    r->current_x = 0;
    r->current_y = 0;
    //Assign model number
    r->model;
}

void init_room(room *r) {
    cin >> r->x;
    cin >> r->y;

}

char move(robot *r, int direction) {
        if (direction == 'N' || direction == 'n')
        {
            r->current_y + 1;
        }
        else if (direction == 'S' || direction == 's')
        {
            r->current_y - 1;
        }
        else if (direction == 'E' || direction == 'e')
        {
            r->current_x + 1;
        }
        else if (direction == 'W' || direction == 'w')
        {
            r->current_x - 1;
        }
        else
        {
            direction = 'x';
            return direction;
        }

    int main() {
        char direction;
        char restart;

        cout << "What size would you like the room to be?";

        room rr;

        init_room();

        robot r; initrobot();

        while (true) {

                cout << "Your robot is in a room with the dimensions (" << rr.x << "," << rr.yy << "). at location (" << r.current_x << "," << r.current_y << ")." << endl;
                cout << "What direction would you like to move? N (North), E (east), S(South), or W(West)?";
                do {
                cin >> direction;

                move(direction) = direction;
                if (direction = 'x') {
                    cout << "Invalid direction. Please enter N, E, S, or W.";
                    }
                while (direction == 'x');

                cout << "Current position is" << endl;

    //          if (r.current_x <= rr.x && r.current_y <= rr.y)
        //      {
            //      cout << "Error, your robot is out of the room bounds. Your robot has exited the room. Would you like to enter another room? Y or N?";
                    cin >> restart;
            //} while (restart == 'Y' || restart == 'y');
                    }

            system("pause");
            return 0;
        }

1 Answer 1

2

The functions init_room and init_robot both take a single parameter each. So, your calls to them should be like

init_room(&rr);
init_robot(&r);

move returns an rvalue of type char, and rvalues cannot be modified. It can only be used as an input to some other expression. I expect you do not know what an rvalue is and it would be a good idea for you to research this as it is an important concept in C++. (Quick version: an rvalue is what might be on the right side of an = and is basically read-only.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you both for the help! @GaryMakin I will definitely do some research into what an rvalue is. Thanks for the tip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.