-
Pointers
I'm building a simple student database with pointers and basically a queue. I'm having trouble putting it together as I'd like to keep track of their name and ID..Here's my code thus far..
Code:
class studentList{
public:
int id;
string name;
studentList *next;
};
int main(){
studentList *temp = NULL;
studentList *front = NULL;
studentList *back = NULL;
int id, option;
string name;
do{
cout<<"1. Add New Student"<<endl;
cout<<"2. Delete First Student"<<endl;
cout<<"3. List All Students"<<endl;
cout<<"4. Exit"<<endl;
cout<<"Option: ";
cin>>option;
if(option == 1){
temp = new studentList;
int _id;
string _name;
cout<<"Please Enter Student's Name"<<endl;;
cin>>temp->name;
cout<<"Please Enter Student's ID"<<endl;;
cin>>temp->id;
}
else if(option == 2 && front != NULL){
temp = front;
front = front->next;
delete temp;
}
else if(option == 3){
temp = front;
while(temp){
cout<<temp->name<<endl;
temp = temp->next;
}
}
else if(option == 4){
cout<<"Thanks For Using This Program!!"<<endl;
}
else{
cout<<"Invalid Option"<<endl;
}
}while(option != 4);
return 0;
}
-
Define "having trouble". What does that mean ? We can't just guess what you don't understand.
Edit: But you may want to use a switch() instead of multiple if() / else() / else if().
-
Well with my enqueue and dequeue aka add and delete students I can't figure out how you can keep track of the student and the id with the same queue then show it.
-
So I figured out how to make my queue hold the name and show it.. but how can I get it to keep the ID with the name in the queue?
Code:
if(option == 1){
cout<<"Please Enter Student's Name"<<endl;;
cin>>name;
// cout<<"Please Enter Student's ID"<<endl;;
//cin>>id;
temp = new studentList;
temp->name = name;
if(front == NULL){
front = temp;
back = front;
}
else{
back->next = temp;
back = temp;
}
}
-
Actually I just figured it out! ( : ..amazing how you can really have a brain meltdown and forget everything!!!!