试图通过结构类型的向量,但没有控制台输出

Trying to pass a vector of a struct type but no console output

本文关键字:输出 控制台 向量 结构 类型      更新时间:2023-10-16

我正在读取文件并将数据存储在结构类型的向量中。我有3种不同的功能:

  1. readfile( insert arg here)//读取文本文件,并在整个星期中获取名称和工作时间。
  2. bubblesort("更多arg")//自我解释
  3. 输出(" arg")//上述向量的内容

功能原型:

void readFile(vector<Employee *> workers, int numOfEmployees);
void bubbleSort(vector<Employee *> workers, int numOfEmployees);
void output(vector<Employee *> workers, int numOfEmployees);

结构:

struct Employee
{
    string name;
    vector<int> hours;
    int totalHours;
}

主:

vector<Employee *> workers;
int numOfEmployees = 0;
readFile(workers, numOfEmployees);
bubbleSort(workers, numOfEmployees);
output(workers, numOfEmployees);
cout << endl;
system("pause");
return 0;

readfile:

ifstream fin;
fin.open("empdata4.txt");
if (fin.fail())
{
    cout << "File failed to open.  Program will now exit.n";
    exit(1);
}
fin >> numOfEmployees;
workers.resize(numOfEmployees);
for (int row = 0; row < numOfEmployees; row++)
{
    workers[row] = new Employee;
    workers[row]->hours.resize(7);
    fin >> workers[row]->name;
    for (int i = 0; i < 7; i++)
    {
        fin >> workers[row]->hours[i];
    }
}

//出于明显的原因排除泡泡排序

输出:

 for (int i = 0; i < numOfEmployees; i++)
 {
     cout << workers[i]->name << " ";
     for (int x = 0; x < 7; x++)
     {
         cout << workers[i]->hours[x] << " ";
     }
     cout << endl;
 }

控制台输出为空白,减去MAIM中的cout << endl;,而system("pause");我认为我大部分时间都正确设置了所有内容,但我仍然不知道。感谢您的帮助!

编辑:添加功能原型和struct

将功能标头更改为

void readFile(vector<Employee *>& workers, int& numOfEmployees);
void bubbleSort(vector<Employee *>& workers, int& numOfEmployees);
void output(vector<Employee *>& workers, int& numOfEmployees);

没有参考,您是按价值传递的,因此,无论您对向量进行的修改和int函数内部的int不会影响您的向量和int在您的主机中,因此Main中的向量始终是空的。p>更好的是,甚至不需要numofemployes。

void readFile(vector<Employee *>& workers);
void bubbleSort(vector<Employee *>& workers);
void output(vector<Employee *>& workers);

如果您需要员工人数,只需致电workers.size()