类中的重载函数(Students())是输出输入的信息,但它不起作用
The overloaded function (Students()) within the class is to output the information inputted, But it does not function
我在一个名为 Students 的类中重载了一个函数。输入信息的那个有效,但输出信息的那个不起作用。我确实相信问题主要出在某个地方。代码更糟,我已经解决了很多问题并来到了这个。如果我尝试输出学生的姓名,它只会结束程序。
这些是成员函数和变量:
class ShowStudents
{
public:
//Declaring a struct
struct Employee
{
string name_public = "";
int age_public = 0;
double ID_No_publice = 0;
};
//Array declaration of type Employee
Employee employees[arraysize];
void Students(int size_public)
{
cout << "Input the number of information to be entered: ";
cin >> size_public;
// Reseting the screen
system("cls");
for (int i = 0; i < size_public; i++)
{
cout << "Enter a name: ";
cin >> employees[i].name_public;
cout << endl;
cout << "Enter age: ";
cin >> employees[i].age_public;
cout << endl;
cout << "Enter the salary: ";
cin >> employees[i].salary_public;
cout << endl;
system("cls");
}
}
void Students(Employee employees[arraysize], int& size_public)
{
for (int i = 0; i < size_public; i++)
{
if (i == 0)
{
cout << setw(4) << "Namet" << "Aget" << "Wage" << endl << endl;
}
cout << setw(4) << employees[i].name_public << 't'
<< employees[i].age_public << 't'
<< employees[i].salary_public << endl;
}
}
};
> This is my main function:
int main()
{
//Declared the variables
student.Students(size);
student.Students(student.employees, size);<------ does not output
system("cls");
}
我已经删除了主要部分的一些不那么重要的部分。我想提供一个最小的可重现代码。到目前为止,我已经使用堆栈溢出一个月了,所以如果我的问题不好或不理解,你可以告诉我。我也有一个月零一周的编程时间,所以我可能会问很多可能很愚蠢的问题。请不要生气。
我注意到的第一件事如下(根据评论还有其他一些错误):
在主要声明中,您声明:
int size = 0;
然后致电
student.Students(size);
然而在方法
void Students(int size_public)
按值及其修改传递size_public
cin >> size_public;
在外面没有效果。
然后你打电话:
student.Students(student.employees, size);<------ does not output
但如前所述size
值仍然是=0
的,因为它的值没有被Students(int size_public)
方法修改。
一种可能的解决方法是通过引用传递大小。修改原型:
void Students(int size_public)
自
void Students(int& size_public)
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 类中的重载函数(Students())是输出输入的信息,但它不起作用
- 运行时类型信息(为什么此代码不起作用)
- 设置控制台信息窗口大小调整不起作用 c++
- 为什么这段代码不起作用(请参阅详细信息)?