jeudi 25 février 2016

Trouble using the new operator please

I was looking at error C2131: expression did not evaluate to a constant and saw that the new operator was needed so that I can create a dynamic array in MSVB. my code worked fine in Dev-C++, but I want it to work in MSVB.

int lines_in_file()
{
    int number_of_lines = 0;
    std::string line;

    std::ifstream menu_file;
    menu_file.open("menu.dat");

    while (getline(menu_file, line))
        ++number_of_lines;

    menu_file.close();

    return number_of_lines;
}

void employee_menu()
{
    int lines = lines_in_file();
    int choice;
    std::string names[lines];

    std::ifstream menu_file;
    menu_file.open("menu.dat");

    for (int i = 0; i <= lines - 1; i++)
    {
        getline(menu_file, names[i]);
    }

    for (int i = 0; i <= lines - 1; i++)
    {
        std::cout << "Name " << i + 1 << ": " << names[i] << "\n";
    }
}

I searched on how to use the new operator, but I'm not sure why the examples keep using * or **.

As you may be able to see, I am trying to get a menu to show a list of employees in a file. I'll then use that menu to choose an employee and do further tasks.

Aucun commentaire:

Enregistrer un commentaire