In my short experience it is highly unusual that a reasonably framed question, such as I believe yours to be, does not elicit a single response on this board over 2 days. (Unreasonable questions often return greater number of responses but that's another matter). So in the spirit of fools rushing in where angels fear &c. here's my 2 cents:
the LinkedList class requires an additional method (let's call it Check_ID) that'd display the message you want. The method would look like:
Code:
bool LinkedList::Match_ID(int& ID)const{
Node* sNode;
while(sNode != NULL){
if (sNode -> GetStudentID() == ID){
cout<<"Already in the list"<<endl;
return true;
}
sNode = sNode->GetNext();
}
return false;
}
Note that your class' data members are horribly exposed, being declared public, and I had to offer them some modesty by declaring them private and hence the getter methods GetNext(), GetStudentID(). So now, upon cin>>ID, ID is passed through Match_ID() until it returns false and then you'd proceed as before.
However, if you are not constrained to eschew STL and C++11, may I also propose an alternative, fuller (incl. your menu items 2 - 6) suggestion based on them, as below?
Code:
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main(){
map<int, string> StudentList;
int choice, id;
while(1){
cout<<"1. Enter student information 2. Search student by ID 3. Search student by Name \n";
cout<<"4. Delete student information 5. Update student information 6. Print all students 7. Quit \n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1:{
do{cout<<"Enter student id: "<<endl;
cin>>id;
} while(StudentList.count(id));/*no matches, returns 0, breaks loop; map, by definition, does not accept duplicate keys but this check ensures that any value corresponding to a duplicate key is not lost */
cout<<"Enter student name: "<<endl;
string name;
cin>>name;
StudentList.insert(pair<int,string>(id,name));
}
break;
case 2:{
cout<<"Enter student id to search: "<<endl;
int search_id;
cin>>search_id;
auto search = StudentList.find(search_id);
if(search != StudentList.end()){
cout<<"ID "<<search->first<<" belongs to student: "<<search->second<<endl;
}else{
cout<<"ID not found"<<endl;
}
}
break;
case 3:{
cout<<"Enter student name to search: "<<endl;
string search_name;
cin>>search_name;
bool name_found = false;
map<int, string>::iterator itr;
for(itr = StudentList.begin(); itr != StudentList.end(); ++itr){
if(itr->second == search_name){
cout<<"Student: "<<itr->second<< " has ID: "<<itr->first<<endl;
name_found = true;
}
}
if(itr ==StudentList.end() && name_found == false){//how to use range loops and print no name found only once????
cout<<"No student with that name"<<endl;
}
}
break;
case 4:{cout<<"Enter student id to delete: "<<endl;
int delete_id;
cin>>delete_id;
auto search = StudentList.find(delete_id);
if(search != StudentList.end()){
StudentList.erase(search);
cout<<"Records of student with ID: "<<delete_id<<" deleted"<<endl;
}else{
cout<<"ID not found"<<endl;
}
}
break;
case 5:{cout<<"Enter student id to update: "<<endl;
int update_id;
cin>>update_id;
cout<<"Enter student name to update: "<<endl;
string update_name;
cin>>update_name;
map<int,string>::iterator search = StudentList.find(update_id);
if(search != StudentList.end()){//first check key exists, else new element inserted into map;
StudentList[update_id] = update_name;
cout<<"Records of student with ID: "<<update_id<<" updated"<<endl;
}else{
cout<<"ID not found"<<endl;
}
}
break;
case 6:{
for(map<int,string>::iterator itr = StudentList.begin(); itr != StudentList.end(); ++itr){
cout<<"StudentID: "<<itr->first<<", StudentName: "<<itr->second<<endl;
}
}
break;
case 7: exit(1);
default:
cout<<"Error! Invalid Key... \n";
cout<<"Enter any key to get back menu. \n";
cin.get();
system("CLS");
}
}
}