C++ Project

May 26, 2009 at 4:28pm
Hi there this is my first time on a forum so I will try my best to make sense.
I am in the middle of a project and have hit a majour problem. I have a Cpp file that can add, delete, search and save data the problem I have is that I wish to add a password function to it but I have no idea how to go about doing that can anyone help me? please.


int option;
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;
struct employeerec{
string employeeID ; // array of employee ID numbers
string employee ; // array of employee names
string title ; // array of employee titles
} records [20]; // array of employees
int c;
int n;
int total=0;

void loademployeedata() // calls load data
{
ifstream myfile ("Employeefile.txt"); // opens file for input

if (myfile.is_open())
{
while (!myfile.eof()) // while not at end of file marker
{
getline (myfile, records[total].employeeID);
getline (myfile, records[total].employee);
getline (myfile, records[total].title);
total++;
}

myfile.close(); //close file

}
else cout << "Can't find file"; // if file not found

}

void Addemployee() // calls add employee
{
int c(-1);
bool found(false);
string reqemployeeID="";
do{
cout<< "Enter employee ID";
cin>> reqemployeeID;
if (reqemployeeID.length()!=14) cout<< "employee ID has invalid length. Please re-enter\n" << endl;
}while (reqemployeeID.length()!=14);

while ((c<total) && (!found))

{ c++;
if (reqemployeeID==records[c].employeeID) found=true;

}


if(found)

cout<< "employeeID already exists. Please re-enter\n";

else
{cin.ignore();

records[total].employeeID=reqemployeeID;
cout<<"Enter employee name \n" << endl;
getline(cin,records[total].employee);
cout<<"Enter employee title\n" << endl;
getline(cin,records[total].title);
total++;}

}
void Displayallemployees() //calls display all employees
{
for (n=0 ; n<total ; n++)
{
cout<< left << setw(15) << records[n].employeeID <<setw(15) << records[n].employee << records[n].title << endl;

}


}


void deleteemployee(int current) // calls delete employee
{ int c;
for (c=current;c<total-1;c++)
{records[c].employeeID =records[c+1].employeeID;
records[c].employee =records[c+1].employee;
records[c].title =records[c+1].title;
}
total--;
}
void Searchinemployee() // calls individual employee search
{
char answer;
int c(-1);
bool found(false);
string reqemployeeID="";
do{
cout<< "Enter employeeID";
cin>> reqemployeeID;
if (reqemployeeID.length()!=14) cout<< "employeeID has invalid length. Please re-enter\n" << endl;

} while (reqemployeeID.length()!=14);

while ((c<total) && (!found))

{ c++;
if (reqemployeeID==records[c].employeeID) found=true;

}
if(found)
{
cout << left << setw(15) << records[c].employeeID << setw(15) << records[c].employee << records[c].title << endl;
cout << "delete employee?\n";
cin >> answer;
if (toupper(answer)=='Y') deleteemployee(c) ;}
else

cout<< "employeeID does not exist. Please try again\n" << endl;

}

void Searchontitle() // calls search on title
{
int c(0);
string reqtitle;
cin.ignore();
cout << "enter employee title" << endl;
getline (cin, reqtitle);

for (c=0 ; c<total ; c++)
{

if (reqtitle == records[c].title)
{
cout << left << setw(15) << records[c].employeeID << setw(15) << records[c].employee << records[c].title << endl;
}

}
}
void SaveData() // calls save data
{
ofstream myfile ("employeeback.txt");

if (myfile.is_open())

for (n=0 ; n<total ; n++)

myfile << records[n].employeeID << endl;
myfile << records[n].employee << endl;
myfile << records[n].title << endl;

myfile.close();
}
void Quit() {cout<<"Quit \n";} // calls quit program

void actonOption() // calls act on option
{
switch(option)
{
case 1:loademployeedata();break;
case 2:Addemployee();break;
case 3:Displayallemployees();break;
case 4:Searchinemployee();break;
case 5:Searchontitle();break;
case 6:SaveData();break;
case 9:Quit();break;
}
}
void displayMenu() // displays menu
{

cout << "1 Load Data \n";
cout << "2 Add employee \n";
cout << "3 Display all employees \n";
cout << "4 Search individual employee \n";
cout << "5 Search on Title \n";
cout << "6 Save Data \n";
cout << "9 Quit \n";

cin >> option;

while((option<1)||(option>9)) // loop to catch errors
{
if((option<1)||(option>9))
cout <<"Menu selection must be in range 1 to 9"<<endl;
cin >> option;
}
}

int main() // main body of program
{
while (option != 9){
displayMenu();
actonOption();
}
return 0;

}
May 26, 2009 at 4:53pm
no need to post this big code if you want help on something else.. :)

the first thing.. if you are adding your records in plain text then whats the need of password?? anyone can open the file and read it. first thing you should do is add all the data encrypted.. you can use so many algos for that like blowfish, rsa etc etc.. there are many.. this way one can see the data from your program.

if you are not interested in this idea than that's fine because second part is also related to encryption.. ;)
you can take the user input as password and can save it to some file, or if you are on windows then in windows registry after encrypting it. one thing which needs to be taken care of is if someone deletes the password file!!!

does this make sense?
May 26, 2009 at 7:55pm
It makes some sense thank you but what I am aiming for is a log in function for when the program is ran you have to log in with a password and ID then the main menu is ran.
Topic archived. No new replies allowed.