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
|
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
void getInput(int&);
/* If you do not know about classes yet, I heavily suggest learning about
* them. They are extremely important and are used to organize data. You can
* a few more other things with them, but as a beginner, you just need to know
* that you can organize data with it. If you can't get around classes,
* I would try to learn about structs. They are similar to class, except
* a programmer can access everything inside the struct, which defeats the
* purpose of something called inforamtion-hiding. */
class student {
public:
/* This is the "public" area of the class. This means any object that is
* instantiated can access these variables or functions directly */
student(); // A constructor. It's the same name as the class.
bool isStudentID_inDatabase();
void printStatusOfStudent();
void queryStudent(const int& idToLookFor);
private:
/* This is the "private" area of the class. Instantiated objects are not
* able to access these variables or functions directly, and therefore
* have to rely on public functions to do so. There is also something
* called a "friend" function if you want to research it. */
std::map<int, std::string> student_id;
};
int main() {
/* Below, we instantiated an object called "student_object". This object
is of the the student class, which is defined above. */
student student_object;
// Boolean value search to create a looping menu, as seen below.
bool search = true;
// id_to_SearchFor should be pretty obvious
int id_to_SearchFor;
while(search) {
getInput(id_to_SearchFor); // Get the input for ID
if(id_to_SearchFor == -1) {
/* You can define a specific value to exit the loop. For this program,
I let -1 denote the exit code. */
search = false;
}
else {
/* If the exit code was not entered, that means the user of the program
* wants to find student based on a student ID. The function
* queryStudent is defined below. */
student_object.queryStudent(id_to_SearchFor);
}
}
return 0;
}
student::student() {
/* This is the constructor of the class. When you instantiate an object
* that belongs to a specific class, the constructor of that class
* is invoked and and whatever statements is inside that constructor
* is executed. In this constructor, the students are set up with unique
* student ID numbers and names.
*
* std::map allows us to set unique key fields in the square brackets.
* This makes sense because students may have the same name, but surely
* they are unable to have the same ID number. As jonnin was saying, it
* is probably better to have these in a file where the program can read
* from. */
student_id[4401053] = "Student A";
student_id[4401054] = "Student B";
student_id[4401055] = "Student C";
student_id[4401056] = "Student D";
student_id[4401057] = "Student E";
student_id[4401058] = "Student F";
student_id[4401059] = "Student G";
student_id[4401060] = "Student H";
student_id[4401061] = "Student I";
student_id[4401062] = "Student J";
}
void student::queryStudent(const int& x) {
/* This finds the student in the std::map container */
if(!student_id.count(x)) {
/* If the student is not found... */
std::cout << "The student with an id: " << x << " was not found.\n";
}
else {
/* If the student is found... */
std::cout << "Student ID: " << x << std::endl;
std::cout << "Student name: " << student_id[x] << " is here\n";
}
}
void getInput(int& x) {
/* A function for getting input. It just looks pretty. That's all. */
std::cout << " >>> ";
std::cin >> x;
}
| |