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 210 211 212 213 214 215 216 217 218 219 220 221 222
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
//this security only allows 3 attempts before terminating the program
string accountName;
accountName = "admin";
string accountPass;
accountPass = "1234";
string loginName;
string loginPassword;
int loginAttempt = 0;
while (loginAttempt < 3)
{
cout << "Enter login name: ";
cin >> loginName;
cout << "Enter password: ";
cin >> loginPassword;
if (loginName == accountName && loginPassword == accountPass)
{
cout << "Hello " << loginName << ", Welcome to the ... " << endl;
break;
}
else
{
cout << "Invalid login attempt. Please try again.\n" << '\n';
loginAttempt++;
}
}
if (loginAttempt == 3)
{
cout << "Too many login attempts! The program will now terminate.";
return 0;
}
// function protypes
void addStudent(
vector<string> & name,
vector<string> & course,
vector<string> & section);
void deleteStudent(
vector<string> & name,
vector<string> & course,
vector<string> & section);
void modifyStudent(
vector<string> & name,
vector<string> & course,
vector<string> & section);
void listStudent(
vector<string> & name,
vector<string> & course,
vector<string> & section);
char getAction(string prompt);
const char exitProgram = '5'; // 5 to exit program
cout << "\n\t= * = STUDENT DATABASE MANAGEMENT SYSTEM = * =" << endl;
const string prompt = "\n \t\t\t 1. Add Records"
"\n \t\t\t 2. List Records"
"\n \t\t\t 3. Modify Records"
"\n \t\t\t 4. Delete Records"
"\n \t\t\t 5. Exit Program"
"\n\n"
"\t\t\t Select Your Choice: ";
vector<string> name(0);
vector<string> course(0);
vector<string> section(0);
char userAction = '?';
// Loop until the user enters the exit code.
userAction = getAction(prompt);
while (userAction != exitProgram) {
switch (userAction) {
case '1':
cout << "\nADD STUDENT:\n------------\n";
addStudent(name, course, section);
break;
case '2':
cout << "\nLIST ALL STUDENTS:\n------------------\n";
listStudent(name, course, section);
break;
case '3': // come back to this
cout << "\nMODIFY STUDENT:\n---------------\n";
modifyStudent(name, course, section);
break;
case '4':
cout << "\nDELETE STUDENT:\n---------------\n";
deleteStudent(name, course, section);
break;
default:
cout << "\nInvalid selection!\n";
break;
} // end switch
userAction = getAction(prompt);
} // end while
return 0;
}
// Adds student
void addStudent(
vector<string> &name,
vector<string> &course,
vector<string> §ion) {
string theName{""};
string theCourse{""};
string theSection{""};
cout << endl << "Enter the student name to add: ";
getline(cin, theName);
cout << "\nEnter " << theName << "'s course: ";
getline(cin, theCourse);
cout << "\nEnter " << theName << "'s section: ";
getline(cin, theSection);
name.push_back(theName);
course.push_back(theCourse);
section.push_back(theSection);
cout << theName << "'s information has been added." << endl;
return;
}
void listStudent(
vector<string> &name,
vector<string> &course,
vector<string> §ion) {
int nElements = 0;
nElements = name.size();
if (nElements > 0) {
cout << endl;
for (int i = 0; i < nElements; ++i) {
cout << i << ") Name: " << name.at(i)
<< ",\tCourse: " << course.at(i)
<< ",\tSection: " << section.at(i) << endl;
}
} else {
cout << endl << "there are no students to list." << endl;
}
return;
} // end listStudents
// function modifies student record
// function modifies student record
void modifyStudent(
vector<string> &name,
vector<string> &course,
vector<string> §ion) {
string theName = "";
string theCourse = "";
string theSection = "";
cout << endl << "Enter the student name to modify: " << endl;
getline(cin, theName);
cout << "Enter " << theName << "'s course: " << endl;
getline(cin, theCourse);
cout << "Enter " << theName << "'s section: " << endl;
getline(cin, theSection);
} // end modify student records
// function deletes student records
void deleteStudent(
vector<string> &name,
vector<string> &course,
vector<string> §ion) {
string theName = "";
string theCourse = "";
string theSection = "";
cout << endl << "Enter the student name to delete: " << endl;
getline(cin, theName);
cout << "Enter " << theName << "'s course: " << endl;
getline(cin, theCourse);
cout << "Enter " << theName << "'s section: " << endl;
getline(cin, theSection);
int i = name.size() - 1;
int any = 0;
for (; i >= 0; --i) {
if (name[i] == theName && course[i] == theCourse &&
section[i] == theSection) {
name.erase(name.begin() + i);
course.erase(course.begin() + i);
section.erase(section.begin() + i);
any++;
}
}
if (any > 0) {
cout << theName << "'s information has been deleted." << endl;
} else {
cout << theName << "'s info has not been found." << endl;
}
return;
}
// Get the action the user wants to perform
char getAction(string prompt) {
string answer = ""; // empty string
char firstChar = '?';
cout << endl << prompt << endl;
getline(cin, answer);
while (answer.length() == 0) {
getline(cin, answer);
}
firstChar = toupper(answer[0]); // change first char to uppercase
return firstChar;
}
|