'temp'未声明,请先使用此函数

'temp' is undeclared, first use this function

本文关键字:函数 temp 未声明      更新时间:2023-10-16
#include<iostream>
#include<conio.h>
#include<string>
#include<cstring>
#include<stdlib.h> 
using namespace std;
bool rgstr_stdnt(struct student *stud,struct list *ls);
double calculate_aggregate(struct student *);
void srch_student(struct student * next);
void addToList(struct student *stud, struct list *l);
void display(struct student *stud, struct list *l);
struct student
{
char name[20];
int matric_marks, inter_marks, aptitude_marks;
int temp;
student *next;
};
struct list
{
char name[20];
double aggr;
list *next;
};

这就是问题出现的地方,它说临时未声明,首先使用此功能,我无法纠正它

void srch_stdnt(struct student *stud)
{
char name[60];
cout << "Enter Student to search :";
cin>>name;
cout << name;
//down here, the error comes and whatever i knew, i have tried to solve it but could not
while (temp!=NULL){
if(strcmp(temp->name, name)==0){
}
temp = temp->next;
}
cout << "No match found";
}

int main()
{   
student * temp=new student();
student *s;  
s = (struct student *) malloc(sizeof(struct student));
struct list *ls;
ls = (struct list *) malloc(sizeof(struct list));
strcpy(ls->name,"");
ls->aggr = 0;
ls->next= NULL;
do
{
cout<<"                 STUDENT ENROLLMENT AND RECORDS MANAGEMENT"<<endl;

cout<<"1- To Enroll A New Sudent."<<endl;
cout<<"2- To View The Enrolled Students."<<endl;
cout<<"3- To Search Through The Already Enrolled Students."<<endl;
cout<<"4- Exit."<<endl;
int input;
cin>>input;
if (input == 1)
{
rgstr_stdnt(s, ls);
}
else if (input == 2)
{
display(s, ls);
}
else if(input == 3)
{
void srch_student(struct student * stud);
}
else if (input == 4)
exit(0);
cout<<endl;
} while(1);
getch();
}
bool rgstr_stdnt(struct student *stud,struct list *ls)
{   
student *s = stud; 
cout<<"Enter Name Of The Student: "<<endl;
cin>>s->name;
cout<<"Enter Percentage in 10th Grade: "<<endl;
cin>>s->matric_marks;
cout<<"Enter Intermediate Percentage:"<<endl;
cin>>s->inter_marks;
cout<<"Enter Percentage In Aptitude Test: "<<endl;
cin>>s->aptitude_marks;
double aggregate;
aggregate = calculate_aggregate(s);  
cout<<"Aggregate Percentage Is: "<< aggregate<<"%"<<endl;
if (aggregate >= 70)
{
cout<<"-> Student Enrolled In BSCS. <-"<<endl;
addToList(s,ls);
return true;
}
else if (aggregate >= 60)
{
cout<<"-> Student Enrolled In BE. <-"<<endl;
addToList(s,ls);
return true;
}
else if (aggregate >=50)
{
cout<<"-> Student Enrolled In MS. <-"<<endl;
addToList(s,ls);
return true;
}
else
{
cout<<"Sorry, Low Percentage. Student Can't Be Enrolled!"<<endl;
return false;
}
}

double calculate_aggregate(struct student *stud)
{   
student *s = stud;
double aggr;
aggr = s->matric_marks * 10/100  + s->inter_marks * 50/100 + 
s->aptitude_marks * 40/100;
return aggr;
}
void addToList(struct student *stud, struct list *l)
{   
list *pointer = l;
while (pointer->next != NULL)
{
pointer = pointer->next;
} 
pointer->next = (struct list *) malloc(sizeof(struct list));
pointer = pointer->next;
strcpy(pointer->name , stud->name);
pointer->aggr = calculate_aggregate(stud);
pointer->next = NULL;
}
void display(struct student *stud, struct list *l)
{
list *pointer = l;
if (pointer->next == NULL)
cout<<"No Students Enrolled Yet."<<endl;
else
{
cout<<" !- - - - - - - - -.  STUDENTS RECORDS  .- - - - - - - - - -! " 
<<endl;
while (pointer->next != NULL)
{
pointer = pointer->next;
cout<<"Name Of Student: "<<pointer->name<<endl;
cout<<"Aggregate Is: "<<pointer->aggr<<endl;
if (pointer->aggr >= 70)
cout<<"-> Student Enrolled In BSCS. <-"<<endl;
else if(pointer->aggr >=60)
cout<<"-> Student Enrolled In BE. <-"<<endl;
else
cout<<"-> Student Enrolled In MS. <-"<<endl;
cout<<endl;
}
}
}

任何能帮助我解决这个问题的人,我真的很感激。

如错误消息所述,您正在使用尚未声明的名为temp的变量。 您需要声明它并为其提供一个初始值:

struct student *temp = stud;
while (temp!=NULL){
...
//down here, the error comes and whatever i knew, i have tried to solve it but could not
while (temp!=NULL){

您尚未在函数中声明temp。这解释了编译器错误。

也许你打算使用:

struct student* temp = stud;
while ( temp != NULL ) 

由于您在C++土地上,请丢弃struct并使用:

student* temp = stud;
while ( temp != NULL ) 

也可以在代码的其余部分进行更改更改。