我试图做一个简单的程序来了解更多关于参考段落、指针以及如何在 C++ 中的结构上使用这 2 的知识,但我遇到了一些问题。
我在下面的代码中遇到错误“错误:'totalStudent' 未在此范围内声明”,我的问题是,我应该如何声明“totalStudent”。
#include <iostream>
using namespace std;
struct test{
char name[30];
int age;
};
void addStudent(struct test *ptrTest,int *totalStudent){
for(int i=0;i<2;i++){
cout<<"\nInsert the name: ";
cin.sync();
cin.getline(ptrTest->name,sizeof(ptrTest->name));
cout<<"\nInsert the age: ";
cin.sync();
cin>>ptrTest->age;
*totalStudent+=1;
}
}
void showStudent(struct test *ptrTest,int totalStudent){
for(int i=0;i<totalStudent;i++){
cout<<"\nName: "<<ptrTest->name;
cout<<"\nAge: "<<ptrTest->age;
}
};
int main()
{
struct test t;
addStudent(&t,&totalStudent);
showStudent(&t,totalStudent);
return 0;
}
我不能很好地使用带有结构的指针和参考段落。我只能在不使用结构时使用它们。
您忘记在以下范围内声明此变量main
:
int main()
{
struct test t;
// LIKE THIS
int totalStudent;
addStudent(&t,&totalStudent);
showStudent(&t,totalStudent);
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句