1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct Student{ //Struct to hold all the data
string name;
int id;
string status;
double gpa;
Student* nextStud;
};
Student* createstudent(); //Prototype for createstudent, which reads info from a file and puts the values into declared variables
void addstudent(Student *&FirstStud, Student *NewStud); //Prototype for add student, which adds a new student to the front of the list
void removestudent(Student *FirstStud); //Prototype for removestudent, which removes a target student from the list
void PrintList(Student *FirstStud); //Prototype for PrintList, which displays a list of all students
void ClassList(Student *FirstStud); //Prototype for ClassList, which displays a list of students in a certain class depending on what number between 1 and 4 was entered
void DeansList(Student *FirstStud); //Prototype for DeansList, which displays a list of students with a gpa of 3.5 or higher
void clearlist(Student* FirstStud); //Prototype for clearlist, which removes all students from the list
Student* FirstStud = NULL; //Initialzes and declares the pointer FirstStud to NULL (0)
Student* NewStud = NULL; //Initialzes and declares the pointer NewStud to NULL (0)
Student* NextStud=NULL; //Initialzes and Declares the pointer NextStud to NULL (0)
ifstream infile; //Declares the variable infile as an ifstream data type
int main(){
infile.open("prog3.txt"); //Opens the file "prog3.txt"
int choice; // Declares the variable choice, which will be used to decide which function is called
infile >> choice; //Gets the value of choice from the previously opened file
while (choice >0 && choice <=5){ //While loop that takes the value from the file and assigns it to its correct call, also checks to make sure the choice is between 0-4
if (choice == 1){ // Checks if the value is 1, which calls createstudent and addstudent if chosen
NewStud = createstudent(); // Call for createstudent and initialzes the result to NewStud
addstudent(FirstStud, NewStud); // Call for addstudent
}
else if (choice == 2){ // Checks if the value is 2, which calls removestudent if chosen
removestudent(FirstStud); //Call for removestudent
}
else if (choice == 3){ // Checks if the value is 3, which calls PrintList if chosen
PrintList(FirstStud); //Call for PrintList
}
else if (choice == 4){ // Checks if the value is 4, which calls ClassList if chosen
ClassList(FirstStud); //Call for ClassList
}
else if (choice == 5){ // Checks if the value is 5, which calls DeansList if chosen
DeansList(FirstStud); //Call for removestudent
}
infile >> choice;
}
clearlist(FirstStud); //Call for clearlist
infile.close(); //Closes the file
system("pause");
return 0;
}
Student* createstudent(){ //Function for createstudent
Student *NewStud = NewStud=NULL; //Declares the pointer NewStud
getline(infile, NewStud->name); // Reads in name from the text file
infile >> NewStud->id; // Reads in id from the text file
infile.ignore();
getline(infile, NewStud->status); // Reads in status from the text file
infile >> NewStud->gpa; // Reads in gpa from the text file
infile.ignore();
return NewStud; //Returns the pointer NewStud, and thus all the info stored in its struct
}
void addstudent(Student *&FirstStud, Student *NewStud){ //Function for addstudent
NewStud->nextStud = FirstStud;
FirstStud=NewStud; // Sets the NewStud to the front of the list
cout << FirstStud->name << "was added to the list"; //Reads the name of the student added and displays it
}
void removestudent(Student *FirstStud) //Function for removestudent
{
int targetid; //declares targetid, which is the id of the student the program looks for to delete
Student *NodePtr; //declares the pointer NodePtr
Student *PrevNode; //declares the pointer PrevNode
if (!FirstStud) //Makes sure the Node to be removed isnt the first node, deleting the first node would cause memory leak
{
cout << "The list is empty" << endl; //Message that displays the list is empty therefore nothing can be removed
return;
infile >> targetid; //Reads an int from the file into targetid
if (FirstStud->id == targetid) //Checks to see if the value that was read in is equal to the First stud
{
NodePtr = FirstStud;
FirstStud = FirstStud->nextStud;
delete NodePtr;
cout << "Room: " << targetid << " has been removed." << endl;
}
else
{
NodePtr = FirstStud;
while (NodePtr != NULL && NodePtr->id != targetid)
{
PrevNode = NodePtr;
NodePtr = NodePtr->nextStud;
}
if (NodePtr)
{
PrevNode->nextStud = NodePtr->nextStud;
delete NodePtr;
cout << "Student: " << targetid << " has been removed." << endl;
}
else
cout << "Your value was not on the list" << endl;
}
}
}
//
void PrintList(Student *FirstStud){
Student* tptr = FirstStud;
if (!tptr)
cout << left << setw(15) << "Name" << setw(15) << "id" << setw(15) << "status"
<< setw(15) << "gpa" << setw(15) << endl;
cout << setw(15) << "------" << setw(15) << "----" << setw(15) << "-----"
<< setw(15) << "-------" << setw(15) << "---------" << endl;
while (tptr)
cout << tptr->name << setw(15) << tptr->id << setw(15) << tptr->status << setw(15) << tptr->gpa << endl;
}
void ClassList(Student *FirstStud)
{
Student *tptr = FirstStud;
string ClassName;
int ClassNumber;
infile >> ClassNumber;
if (ClassNumber == 1){
string ClassName = "Fresh";
cout << "The " << ClassName << " are:" << endl;
}
else if (ClassNumber == 2){
string ClassName = "Soph";
cout << "The " << ClassName << " are:" << endl;
}
else if (ClassNumber == 3){
string ClassName = "Junr";
cout << "The " << ClassName << " are:" << endl;
}
else if (ClassNumber == 4)
{
string ClassName = "Senr";
cout << "The " << ClassName << " are:" << endl;
}
while (tptr){
if (tptr->status == ClassName)
cout << tptr->name << tptr->id << tptr->gpa << endl;
}
}
void DeansList(Student *FirstStud){
if (FirstStud->gpa >= 3.5){
cout << left << setw(15) << "Name" << setw(15) << "id" << setw(15) << "status"
<< setw(15) << "gpa" << setw(15) << endl;
cout << setw(15) << "------" << setw(15) << "----" << setw(15) << "-----"
<< setw(15) << "-------" << setw(15) << "---------" << endl;
cout << FirstStud->name << setw(15) << FirstStud->id << setw(15) << FirstStud->status << setw(15) << FirstStud->gpa << endl;
}
}
void clearlist(Student* FirstStud){
Student *NodePtr;
Student *NextStud;
NodePtr = FirstStud;
while (NodePtr){
NextStud = NodePtr->nextStud;
cout << NodePtr->name << "Has been removed from the list" << endl;
delete NodePtr;
}
FirstStud = NULL;
cout << "The list is now empty" << endl;
}
|