Home > front end >  Error: expected primary-expression before ']' token. Need assitance with function definiti
Error: expected primary-expression before ']' token. Need assitance with function definiti

Time:09-28

This is my first semester of computer science, and I need help with my first project. So far, it's still a mess, and I am mainly doing test cases to ensure basic things like my functions work.

The goal of the project is to ask the user how many robots they want to make, name them, then they can use the robot's name (their unique identifier) to move the robots along an X-Y axis, which is really just the program adding or subtracting to a .Xvalue or .Yvalue.

My MenuFunction works, but I am having trouble with the MoveFunction. Basically, I want the MoveFunction to ask the user which robot they want to use, go through the RobotArray, and print the Robot's name once found.

Again, this is just a test case so I can better understand the coding. Right now, I am getting two errors:

main.cpp:42:33: error: expected primary-expression before ‘]’ token
   42 |  string MoveFunction(RobotArray[], robotName, NumberOfRobots);

main.cpp:62:16: error: no match for call to ‘(std::string {aka std::__cxx11::basic_string}) ()’
   62 |   MoveFunction();

I don't know what to do for the first error, but I think the latter is due to my not having any objects in the function call, but I wouldn't know what to put in there anyway.

My complete code is below:

#include <iostream>
using namespace std;

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

void MenuFunction()
{
    cout << "Welcome to MultiRobo Guider." << endl;
    cout << "Please select:" << endl;
    cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}

int main()
{

    int NumberOfRobots;
    string robotName;
    cout << "Enter the number of robots" << endl;
    cin >> NumberOfRobots;
    cout << endl << "Enter their name(s)" << endl;
    userRobot RobotArray[NumberOfRobots];

    for (int i = 0; i < NumberOfRobots; i  )
    {
        cin >> robotName;
        RobotArray[i].name = robotName;
    }

    cout << endl;

    for (int j = 0; j < NumberOfRobots; j  )
    {
        cout << RobotArray[j].name << "'s position is ";
        cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
    }

    string MoveFunction(RobotArray[], robotName, NumberOfRobots);
    {
        cin >> robotName;
        for (int k = 0; k < NumberOfRobots; k  )
        {
            if (robotName == RobotArray[k].name)
            {
                cout << RobotArray[k].name;
            }
        }
    }

    MenuFunction();
    char input;
    cin >> input;

    if (input == 'm')
    {
        cout << "Which robot would you like to move?";
        MoveFunction();
    }
    else if (input == 'd')
    {
        cout << "distance";
    }
    else if (input == 'q')
    {
        cout << "quit";
    }
}

CodePudding user response:

First off, userRobot RobotArray[NumberOfRobots]; is a variable-length array, which is a non-standard extension supported by only a few compilers. To create an array whose size is not known until runtime, you should use new[] instead, or better std::vector.

That said, your main issue is that you are trying to define your MoveFunction() function inside of your main() function, which is not allowed. But, even if it were, you are not declaring it correctly. It has an erroneous ; on it. And RobotArray, robotName, and NumberOfRobots are not types, but variables. Like a variable, a function parameter always starts with a type. There are no untyped parameters/variables in C .

You need to either:

  • move MoveFunction() outside of main(), and fix its declaration to use proper types, eg:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

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

void MenuFunction()
{
    cout << "Welcome to MultiRobo Guider." << endl;
    cout << "Please select:" << endl;
    cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}

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

    string robotName;
    cin >> robotName;

    for (int k = 0; k < NumberOfRobots; k  )
    {
        if (robotName == RobotArray[k].name)
        {
            cout << RobotArray[k].name << endl;
            return;
        }
    }

    cout "Robot not found" << endl;
}

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

    int NumberOfRobots;
    cin >> NumberOfRobots;

    vector<userRobot> RobotArray(NumberOfRobots);

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

    for (int j = 0; j < NumberOfRobots; j  )
    {
        cout << RobotArray[j].name << "'s position is ";
        cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
    }

    MenuFunction();
    char input;
    cin >> input;

    if (input == 'm')
    {
        MoveFunction(RobotArray.data(), NumberOfRobots);
    }
    else if (input == 'd')
    {
        cout << "distance";
    }
    else if (input == 'q')
    {
        cout << "quit";
    }
}
  • or change MoveFunction() into a lambda, eg:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

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

void MenuFunction()
{
    cout << "Welcome to MultiRobo Guider." << endl;
    cout << "Please select:" << endl;
    cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}

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

    int NumberOfRobots;
    cin >> NumberOfRobots;

    vector<userRobot> RobotArray(NumberOfRobots);

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

    for (int j = 0; j < NumberOfRobots; j  )
    {
        cout << RobotArray[j].name << "'s position is ";
        cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
    }

    auto MoveFunction = [&]{
        cout << "Which robot would you like to move?";

        string robotName;
        cin >> robotName;

        for (int k = 0; k < NumberOfRobots; k  )
        {
            if (robotName == RobotArray[k].name)
            {
                cout << RobotArray[k].name << endl;
                return;
            }
        }

        cout "Robot not found" << endl;
    };

    MenuFunction();
    char input;
    cin >> input;

    if (input == 'm')
    {
        MoveFunction();
    }
    else if (input == 'd')
    {
        cout << "distance";
    }
    else if (input == 'q')
    {
        cout << "quit";
    }
}

CodePudding user response:

You have a semi-colon at the end of MoveFunction() definition, In C when you define a function the name of the function does not end with a semi-colon.

  • Related