试图通过结构类型的向量,但没有控制台输出
Trying to pass a vector of a struct type but no console output
我正在读取文件并将数据存储在结构类型的向量中。我有3种不同的功能:
- readfile(
insert arg here
)//读取文本文件,并在整个星期中获取名称和工作时间。 - bubblesort("更多arg")//自我解释
- 输出(" 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()
相关文章:
- C++:将控制台输出存储在宏中更好吗
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 如何修复此iFile以将.txt输出到控制台
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 控制台输出在 Qt5 中未正确显示
- 有人可以向我解释为什么控制台输出 0 吗?
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 为什么即使使用 for 循环遍历我的向量,它也没有输出到控制台?(C++)
- 使用 std::string_view 的子字符串控制台输出
- 从文件读取并通过矢量输出到控制台
- 在这种情况下,如何防止C++输出/控制台窗体关闭
- 如何在C++中居中输出控制台窗口
- boost::log 输出到 Visual Studio 输出控制台 - 为格式添加额外的 LF/CR
- C++:我如何保持我的程序(输出控制台)的活力
- 如何在Visual Studio中设置输出控制台宽度
- 如何在Qt GUI应用程序中嵌入输出控制台窗口
- 在Visual Studio中使用GLUT库,没有错误,但输出控制台不起作用
- 只在调试模式下输出控制台
- 简单的输入/输出控制台应用程序,不按给定的顺序(结构)打印问题
- Visual Studio 2012 - 嵌入式输出控制台,而不是cmd