一个函数中的不同数组
different arrays in one function
我是 c++ 的初学者,我必须纠正一个函数,该函数要求用户输入五个不同数组的第一个元素的值。例如,学生姓名数组、学生 ID 数组等。这里的问题是这个函数的参数是什么。
这是我的尝试,我声明数组是全局的。我不知道错误在哪里。
const int SIZE=1000;
int studN[SIZE];
int id[SIZE];
string courseName[SIZE];
string courseSec[SIZE];
int regNom[SIZE];
void insertNew()
{
int index=0;
index++;
cout<<"Please enter the student name: ";
cin>>studN[index];
cout<<"Please enter the student ID: ";
cin>>id[index];
cout<<"Please enter the course name: ";
cin>>courseName[index];
cout<<"Please enter the course section: ";
cin>>courseSec[index];
cout<<"Please enter the registration number: ";
cin>>regNom[index];
cout<<" Information stored"<<endl;
}
如果我理解正确,您想编写一个函数,该函数将使用给定索引设置数组元素的值。该函数可以如下所示
bool Insert( int studN[], int id[], string courseName[], string courseSec[], int regNom, size_t size, size_t pos )
{
if ( size <= pos ) return false;
cout << "Please enter the student name: ";
cin >> studN[pos];
// other stuff
// ,,,
return true;
}
但无论如何,最好定义一个包含相应字段的结构或类数组。
由于已将数组声明为全局变量,因此无需为函数提供任何参数。
但是,索引应在函数外部初始化一次为 0。这是因为您不想在同一索引 = 1 处一次又一次地覆盖这些值。
此外,在每次int
读取操作后,都应键入cin.ignore()
,因为n
字符驻留在输入缓冲区中,这会干扰进一步的字符串读取。
首先,
你需要一个main()函数来启动程序。这可能就是你想要的cin/cout所在的地方。
如果你想隐藏变量,那么像你这样的变量并不是最佳的,所以把它们和你拥有的其余代码一起塞进主{}中
然后,您要将已添加到另一个函数的这些参数发送到另一个函数。喜欢
PrintStudents( int* studN, etc....)
{
}
请注意,您需要使用指针。
相关文章:
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 打印 2D 动态数组 c++ 函数
- C++数组函数
- 在数组函数指针中调用void
- 数组函数似乎没有找到最大值
- 使用数组函数和循环求解伪元编码|C
- 在头文件中的函数原型中获取指针数组:函数声明为 void
- 数组函数错误输出
- 读取文件,然后创建数组函数C++
- 指针数组函数参数-奇怪的行为
- 二维数组 - 函数
- "return"没有从数组函数返回正确的值
- 数组函数问题
- 数组函数的大小返回 int 的大小而不是数组
- 从数组函数中删除元素
- 如何从C++中最好地调用具有C99风格数组函数签名的函数
- 追加/打印/插入数组c++函数问题
- 如何将数组函数转换为指针函数
- 多维数组函数输出垃圾
- C++:数组函数的递归最大值