24. Mini Database
24 Sep 2019 | C++
Mini Database Exercise
1. conio.h
- console input output header
- 즉 콘솔에서의 입력과 출력에 관한 헤더이다.
- 함수들
- kbhit: 키보드가 눌렸는지 확인
- getch: 콘솔에서 buffer나 echo(타이핑한 키가 화면에 표시) 없이 한 개의 키 값을 입력 받음
- getche: 콘솔에서 키보드 입력으로 한 개의 키 입력을 받음 에코 됨
- ungetch: 키보드 버퍼로 문자 1개를 넣음
- gscanf: 콘솔 입력으로 부터 포매팅된 입력을 받음
- cputs: 콘솔에 직접 문자열을 출력
- cprintf: 포맷을 사용해 콘솔에 문자열을 출력
- clrscr: 화면을 지움
2.cstdlib.h
- rand(),srand, time들이 여기 헤더파일에 존재한다.
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <fstream>
using namespace std;
struct Person
{
string name;
string surname;
short age;
string telephone;
};
short peopleInDataBase;
Person people[20]; // it can be making 20 of them person.
void requireEnter();
void addPerson();
void showPeople();
void savePeopleToFile();
void loadPeopleFromFile();
void searchDatabase();
void removePersonFromDatabase();
int main()
{
char test;
loadPeopleFromFile(); // load it when the programming start
do
{
cout << "Number of People in Database: " << peopleInDataBase << endl;
cout << "MENU: " << endl;
cout << "1. Add Person" << endl;
cout << "2. Show All People" << endl;
cout << "3. Save People to File" << endl;
cout << "4. Load People from File" << endl;
cout << "5. Search for a Person" << endl;
cout << "6. Remove Person from DataBase" << endl;
cout << endl;
test = getch();
switch(test)
{
case '1':
addPerson();
break;
case '2':
showPeople();
break;
case '3':
savePeopleToFile();
break;
case '4':
loadPeopleFromFile();
break;
case '5':
searchDatabase();
break;
case '6':
removePersonFromDatabase();
break;
}
requireEnter();
system("cls");
}while(test != 27); // 27 is the keyboard asci code, so if we put "space bar" it doens work
return 0;
}
void requireEnter()
{
cout << "Click Enter to continue... " << endl;
while(getch() != 13); // this function never stop the function.
}
void addPerson()
{
cout << "Type name: ";
cin >> people[peopleInDataBase].name;
cout << "Type surname: ";
cin >> people[peopleInDataBase].surname;
cout << "Type age: ";
cin >> people[peopleInDataBase].age;
cout << "Type telephone: ";
cin >> people[peopleInDataBase].telephone;
peopleInDataBase++;
}
void showPeople()
{
if(peopleInDataBase > 0)
{
for (int i = 0; i < peopleInDataBase; i++)
{
cout << "Person index: " << (i+1) << endl;
cout << "Name: " << people[i].name << endl;
cout << "Surname: " << people[i].surname << endl;
cout << "Age: " << people[i].age << endl;
cout << "Telephone: " << people[i].telephone << endl << endl;
}
}
else
cout << "There is nobody in database." << endl;
}
void savePeopleToFile()
{
ofstream file("database.txt"); // only putting some data inside
if (file.is_open())
{
file << peopleInDataBase << endl;
for (int i = 0; i < peopleInDataBase; i++)
{
file << people[i].name << endl;
file << people[i].surname << endl;
file << people[i].age << endl;
file << people[i].telephone << endl;
}
file.close();
}
else
cout << "I couldnt save to database" << endl;
}
void loadPeopleFromFile()
{
ifstream file("database.txt"); //ifstream is only taking to read
if (file.is_open())
{
file >> peopleInDataBase;
if (peopleInDataBase > 0)
{
/*for (int i = 0; i < peopleInDataBase; i++)
{
file >> people[i].name;
file >> people[i].surname;
file >> people[i].age;
file >> people[i].telephone;
}*/
int i = 0;
do
{
file >> people[i].name; // where we want to put things from
file >> people[i].surname; // loading >> this way
file >> people[i].age;
file >> people[i].telephone;
i++;
}while(!file.eof());
cout << "People has been loaded properly. " << endl;
}
else
cout << "Databse is Empty" << endl;
}
else
cout << "The file database.txt doesnt exist" << endl;
}
void searchDatabase()
{
if (peopleInDataBase > 0)
{
string name;
cout << "Type a name of person you want to look for: ";
cin >> name;
for (int i = 0; i < peopleInDataBase; i++)
{
if (name == people[i].name)
{
cout << "Person index: " << (i+1) << endl;
cout << "Name: " << people[i].name << endl;
cout << "Surname: " << people[i].surname << endl;
cout << "Age: " << people[i].age << endl;
cout << "Telephone: " << people[i].telephone << endl << endl;
}
}
}
else
cout << "There is nobody in database what do you want to look for??!" << endl;
}
void removePersonFromDatabase()
{
if (peopleInDataBase > 0)
{
short index;
cout << "Who do you want to remove? Type index: " << endl;
cin >> index;
if (peopleInDataBase >= index)
{
for (short k = index; k < peopleInDataBase; k++) // because we want to remove from some point
{
people[k-1].name = people[k].name; // because it started from 0 matrix
people[k-1].surname = people[k].surname; // so when index reserve 1 in the txt file it means 0 person
people[k-1].age = people[k].age;
people[k-1].telephone = people[k].telephone;
}
peopleInDataBase--;
savePeopleToFile();
}
else
cout << "There is nobody like that" << endl;
}
else
cout << "There is nothing to remove" << endl;
}
return
Mini Database Exercise
1. conio.h
- console input output header
- 즉 콘솔에서의 입력과 출력에 관한 헤더이다.
- 함수들
- kbhit: 키보드가 눌렸는지 확인
- getch: 콘솔에서 buffer나 echo(타이핑한 키가 화면에 표시) 없이 한 개의 키 값을 입력 받음
- getche: 콘솔에서 키보드 입력으로 한 개의 키 입력을 받음 에코 됨
- ungetch: 키보드 버퍼로 문자 1개를 넣음
- gscanf: 콘솔 입력으로 부터 포매팅된 입력을 받음
- cputs: 콘솔에 직접 문자열을 출력
- cprintf: 포맷을 사용해 콘솔에 문자열을 출력
- clrscr: 화면을 지움
2.cstdlib.h
- rand(),srand, time들이 여기 헤더파일에 존재한다.
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <fstream>
using namespace std;
struct Person
{
string name;
string surname;
short age;
string telephone;
};
short peopleInDataBase;
Person people[20]; // it can be making 20 of them person.
void requireEnter();
void addPerson();
void showPeople();
void savePeopleToFile();
void loadPeopleFromFile();
void searchDatabase();
void removePersonFromDatabase();
int main()
{
char test;
loadPeopleFromFile(); // load it when the programming start
do
{
cout << "Number of People in Database: " << peopleInDataBase << endl;
cout << "MENU: " << endl;
cout << "1. Add Person" << endl;
cout << "2. Show All People" << endl;
cout << "3. Save People to File" << endl;
cout << "4. Load People from File" << endl;
cout << "5. Search for a Person" << endl;
cout << "6. Remove Person from DataBase" << endl;
cout << endl;
test = getch();
switch(test)
{
case '1':
addPerson();
break;
case '2':
showPeople();
break;
case '3':
savePeopleToFile();
break;
case '4':
loadPeopleFromFile();
break;
case '5':
searchDatabase();
break;
case '6':
removePersonFromDatabase();
break;
}
requireEnter();
system("cls");
}while(test != 27); // 27 is the keyboard asci code, so if we put "space bar" it doens work
return 0;
}
void requireEnter()
{
cout << "Click Enter to continue... " << endl;
while(getch() != 13); // this function never stop the function.
}
void addPerson()
{
cout << "Type name: ";
cin >> people[peopleInDataBase].name;
cout << "Type surname: ";
cin >> people[peopleInDataBase].surname;
cout << "Type age: ";
cin >> people[peopleInDataBase].age;
cout << "Type telephone: ";
cin >> people[peopleInDataBase].telephone;
peopleInDataBase++;
}
void showPeople()
{
if(peopleInDataBase > 0)
{
for (int i = 0; i < peopleInDataBase; i++)
{
cout << "Person index: " << (i+1) << endl;
cout << "Name: " << people[i].name << endl;
cout << "Surname: " << people[i].surname << endl;
cout << "Age: " << people[i].age << endl;
cout << "Telephone: " << people[i].telephone << endl << endl;
}
}
else
cout << "There is nobody in database." << endl;
}
void savePeopleToFile()
{
ofstream file("database.txt"); // only putting some data inside
if (file.is_open())
{
file << peopleInDataBase << endl;
for (int i = 0; i < peopleInDataBase; i++)
{
file << people[i].name << endl;
file << people[i].surname << endl;
file << people[i].age << endl;
file << people[i].telephone << endl;
}
file.close();
}
else
cout << "I couldnt save to database" << endl;
}
void loadPeopleFromFile()
{
ifstream file("database.txt"); //ifstream is only taking to read
if (file.is_open())
{
file >> peopleInDataBase;
if (peopleInDataBase > 0)
{
/*for (int i = 0; i < peopleInDataBase; i++)
{
file >> people[i].name;
file >> people[i].surname;
file >> people[i].age;
file >> people[i].telephone;
}*/
int i = 0;
do
{
file >> people[i].name; // where we want to put things from
file >> people[i].surname; // loading >> this way
file >> people[i].age;
file >> people[i].telephone;
i++;
}while(!file.eof());
cout << "People has been loaded properly. " << endl;
}
else
cout << "Databse is Empty" << endl;
}
else
cout << "The file database.txt doesnt exist" << endl;
}
void searchDatabase()
{
if (peopleInDataBase > 0)
{
string name;
cout << "Type a name of person you want to look for: ";
cin >> name;
for (int i = 0; i < peopleInDataBase; i++)
{
if (name == people[i].name)
{
cout << "Person index: " << (i+1) << endl;
cout << "Name: " << people[i].name << endl;
cout << "Surname: " << people[i].surname << endl;
cout << "Age: " << people[i].age << endl;
cout << "Telephone: " << people[i].telephone << endl << endl;
}
}
}
else
cout << "There is nobody in database what do you want to look for??!" << endl;
}
void removePersonFromDatabase()
{
if (peopleInDataBase > 0)
{
short index;
cout << "Who do you want to remove? Type index: " << endl;
cin >> index;
if (peopleInDataBase >= index)
{
for (short k = index; k < peopleInDataBase; k++) // because we want to remove from some point
{
people[k-1].name = people[k].name; // because it started from 0 matrix
people[k-1].surname = people[k].surname; // so when index reserve 1 in the txt file it means 0 person
people[k-1].age = people[k].age;
people[k-1].telephone = people[k].telephone;
}
peopleInDataBase--;
savePeopleToFile();
}
else
cout << "There is nobody like that" << endl;
}
else
cout << "There is nothing to remove" << endl;
}
return
Comments