This is simple student information system project where you can do following things:
- Add Records
- List Records
- Modify Records
- Delete Records
To store data a .txt file is used.
I just want an honest critique of my code so I can improve.
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
void flowcontrol();//directs you based on what you are trying to do;
int entrynumb();//counts the number astericks and stores them as a base for how many students they are for student number assignment;
void addrecord();//adds new students to the database;
void seerecord(string n);//lets you see the data on a student number;
void modifyrecord(string n, char c);//deletes a desired record;
int number = 0;
string strnumber = to_string(number);
struct student
{
string firstname, lastname, birthday;
int age, grade;
int number;
void writeto(student a)//writes student values to a file for retrieval and modification;
{
ofstream datastore;
datastore.open ("datastore.txt", ios_base::app);
datastore << '\n' << "Number : " << a.number;
datastore << '\n' << "Name :" << a.firstname << ' ' << a.lastname;
datastore << '\n' << "DOB : " << a.birthday;
datastore << '\n' << "Age : " << a.age;
datastore << '\n' << "Grade : " << a.grade;
datastore << '\n' << "*";
datastore << '\n' << "----------------------------------------------";
datastore.close();
}
};
int main()
{
flowcontrol();
}
void spacer(int a)//adds a certain number of newline spaces;
{
cout << string(a, '\n');
}
void flowcontrol()
{
spacer(3);
cout << "Welcome to the student database version 1.101" << '\n';
cout << "<------------------------------------------->" << '\n';
cout << " What would you like to do? " << '\n';
cout << " ADD | MODIFY | DELETE | SEE " << '\n';
cout << " a | m | d | s " << '\n';
spacer(3);
char ch; cin >> ch;
spacer(22);
switch(ch)
{
case 'a':
addrecord();
break;
case 's':
{
spacer(2);
cout << "Student ID number : " << '\n';
string n; cin >> n;
spacer(5);
seerecord(n);
break;
}
case 'd':
{
spacer(2);
cout << "Student ID number : " << '\n';
string n; cin >> n;
spacer(5);
modifyrecord(n, 'd');
break;
}
case 'm':
{
spacer(2);
cout << "Student ID number : " << '\n';
string n; cin >> n;
spacer(5);
modifyrecord(n, 'm');
break;
}
}
}
int entrynumb()//student number generator;
{
string line;//stores a line of the datastore file here for reference;
vector<string> liner;//stores the amount of entries;
ifstream myfile ("datastore.txt");//opens the datastore file;
if (myfile.is_open())
{
while (getline(myfile,line))//grabs the lines in datastore and does something;
{
if(line == "*")//does this if the condition is met;
{
liner.push_back("*");//pushes the asterick to the liner vector;
}
}
myfile.close();
}
return liner.size();//returns the number of astericks stored wich is directley correlated with the number of students;
}
void addrecord()//new student generator;
{
student strnumber;//initiates new student entry as a student object;
strnumber.number = entrynumb();//grabs the proper student number;
string strcontainer;//to store string responses such as firstname/lastname;
int intcontainer;//to store int responses such as age, dob, etc;
cout << "First name? " << '\n';
cin >> strcontainer; strnumber.firstname = strcontainer;
spacer(1);
cout << "last name ? " << '\n';
cin >> strcontainer; strnumber.lastname = strcontainer;
spacer(1);
cout << "birthday ? " << '\n';
cin >> strcontainer; strnumber.birthday = strcontainer;
spacer(1);
cout << "age ? " << '\n';
cin >> intcontainer; strnumber.age = intcontainer;
spacer(1);
cout << "grade ? " << '\n';
cin >> intcontainer; strnumber.grade = intcontainer;
spacer(1);
strnumber.writeto(strnumber);//calls to write the students data to datastore file;
number++;//adds the number up by one per entry to keep track of number of students;
main();//restarts the process;
}
void seerecord(string n)//takes the number provided and shows the student information;
{
string line;
ifstream myfile ("datastore.txt");//opens the datastore file;
if (myfile.is_open())
{
while (getline(myfile,line))//grabs the lines in datastore and does something;
{
if(line == "Number : " + n)//loops through and prints every line related associated with the number;
{ cout << " Student entry " << '\n';
cout << "<------------------------------------------->" << '\n';
cout << '\n' << line << '\n';
for(int i = 0; i < 5; i++ )
{
getline(myfile, line);
cout << line << '\n';
}
cout << "<------------------------------------------->";
}
}
myfile.close();
}
flowcontrol();
}
void modifyrecord (string n, char c)//delete//modify a desired record;
{
vector<string> datahold;//holds all the entries kept unchanged;
ifstream myfile ("datastore.txt");//opens the file;
string line;//for reference to each line;
if (myfile.is_open())
{
while (getline(myfile, line))
{
if(line == "Number : " + n)//if the number that is to be deleted is found it's associated entries wont be stored;
{
switch(c)//checks to see if your deleting or modifying a student entry;
{
case 'd'://delete;
{
for(int i = 0; i < 6; i++)
{
getline(myfile, line);
cout << "DELETING: " << line << '\n';
}
flowcontrol();
break;
}
case 'm'://edit;
{
string entries[4] = {"Name : ", "DOB : ", "Age : ", "Grade : "};
datahold.push_back(line);
for(int j = 0; j < 4; j++)//loops through and edits each entry;
{
string strcontainer;
getline(myfile, line);
cout << "Currently: " << line << '\n' << "Edit ->"; cin >> strcontainer;
datahold.push_back(entries[j] + strcontainer);
cout << "---------------------------------------->" << '\n';
}
flowcontrol();
break;
}
}
}
else
{
datahold.push_back(line);//pushes entries that won't be edited or deleted;
}
}
myfile.close();
}
ofstream myfilenew;
myfilenew.open ("datastore.txt");
for(unsigned int i = 0; i < datahold.size(); i++)//iterates through all the kept entries and rewrites them to the file;
{
myfilenew << datahold[i] << '\n';
}
}