PDA

View Full Version : Creating a login with C++




dq9
Mar 14, 2008, 01:36 AM
Ok, so I am kinda new to C++, kinda not.
I am trying to create a login for a command line utility C++ program. I followed the post here (http://forums.macrumors.com/showpost.php?p=1624842&postcount=2), and made a few modifications to the code.
Here is my code:
// test.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib> // for exit(1);

using namespace std;


void Login();

int main()
{
Login();
return 0;
}

void Login()
{
char login_un[50], login_pw[50], username[50], password[50];
int c;
ifstream uin("user.txt");
ifstream pin("pass.txt");

cout<<"Main\n\n"
<<"(1) Login\n"
<<"(2) Quit\n";
cin>> c;



if (c==1)
{
uin.getline(username, 50);
while (strcmp(login_un, username) !=0)
{
cout<<"Username: ";
cin.getline(login_un, 50);
if (strcmp(login_un, username) ==0) break;
else
cout<<"\nInvalid Username.\n";
}

pin.getline(password, 50);
while (strcmp(login_pw, password) !=0)
{
cout<<"\nPassword: ";
cin.getline(login_pw, 50);
if (strcmp(login_pw, password) ==0) break;
else
cout<<"\nInvalid Password\n";
}

}

else if (c==2)
{
cout<<"Quitting\n";
exit(1);
}
return;
}
My user.txt is as follows:

admin
dq9
guest
My pass.txt is as follows:

test
pie
-

When I type in admin and test for the user and password, everything works fine. But when I try dq9 or guest with their passwords or even the admin password, the program says "Invalid Username." What can I do to fix this so I can login with dq9 or guest?



trevis
Mar 14, 2008, 03:13 AM
This program is pretty weird in many respects, but to the answer the question,
it does not work for any other name then admin:test because the next lines from *.txt files are never read, only one line is read from each file.

zcarter
Mar 14, 2008, 03:22 AM
Okay, first off...does it give you any errors?

Second off..why don't you just use one file? Make the format like this..

USERNAME PASSWORD
USERNAME2 PASSWORD2
USERNAME3 PASSWORD3

then, do a loop to the end of the file
(remember fin is the name of my ifstream class..)
(be sure to include <string>)

string username[50], password[50]; //Assuming you wont have more than 50 usernames/passwords stored..

int i = 0;
while(!fin.eof())
{
fin >> username[i];
fin >> password[i];
i++;
}

Then you can do..the comparison...
int INDEX = //whatever the username/password combo you need to grab via the array
if(username[INDEX] == typed_username && password[INDEX] == typed_password)
{
cout << "Successful Login..";
}

else
{
cout << "Error...";
}


If you need more help, don't hesitate to ask..but it seems like your biggest problems are #1: The file organization and #2: What usernames/passwords in the .txt file to compare with the typed information.

- Zac

dq9
Mar 14, 2008, 11:26 PM
I don't understand the part where you added in INDEX. Can you show me an example code of how I would use that?

And thanks for the help so far.

zcarter
Mar 16, 2008, 09:45 PM
Okay, when I say Index..I mean like this:

In an array..you have for example this...

Array[0]
Array[1]
Array[2]

Right?

The part in between the [ ]'s is considered the "index"

So when I put index..its assuming you will have figured out what will be in between the index..

for example if I did a for loop like this


for(i = 1; i<5; i++)
{
cout << "array[" << i << "]" << endl;
}

You would get something like this:
array[1]
array[2]
array[3]
array[4]

in that case..the "i" was the index.

Does that make sense?

I'd be more than happy to help out more..just message me on aim..

World War Zac


- Zac

dq9
Mar 19, 2008, 10:55 PM
int INDEX = //whatever the username/password combo you need to grab via the array
if(username[INDEX] == typed_username && password[INDEX] == typed_password)

Ok I understand a little more now. I still don't understand what you mean by username/password combo.

dq9
Mar 23, 2008, 09:40 PM
sorry for doublepost

How do I change the INDEX with each different username so I'm not searching the same index every time?

ChrisA
Mar 23, 2008, 10:41 PM
Ok, so I am kinda new to C++, kinda not.
I am trying to create a login for a command line utility C++ program. I followed the post here (http://forums.macrumors.com/showpost.php?p=1624842&postcount=2), and made a few modifications to the code.

This is really not the best way to do this. First off it is trivally simple to crack this even after you get it to work. The passwords are inside you executable file for anyone to find. If this is a class asignment then OK but if this is a real world application you need a better scheeme.

As luck would have it this problem was solved 30 or years ago and you don't have to re-invent the wheel.

use the "getpass" funtion to read the password. It will turn off character echo.

Next use the function "crypt" on what the user typed. and zero out the cleartext passwd with "memset"

next make a call to "getpwent" to get the user's encrypted password. Compare with above.

There are man pages for all of the above functions and the above method is portable and works on all UNIX-like systems, Mac OS X, Solaris and so on. C++ is pretty easy to learn but what takes a longer time is the environment and all the libraries. I've been at this from back when UNIX was new and I don't know a tenth of it.

dq9
Mar 23, 2008, 11:30 PM
Do you know of any tutorials on how to use those functions?

I eventually want to port this over to Windows, that's why I'm coding things the way I am, will those functions work in Windows as well?

zcarter
Mar 23, 2008, 11:37 PM
He's right...it's not the best way to do things.

As far as the encryption..im sure its similar to the way I do it in PHP.

I THINK this is how it would work.

Lets say your password is: ABC and you want to encrypt it.

string password;

crypt(password);
//May look something crazy (im making this up) (*#*(*@&*E@DM*DUM@(*U@ = encrypted password

Then..to check

cin >> pass;


Notice how when I CHECK the password, I encrypt the password they type in. The STORED password that you are comparing the TYPED password to is already encrypted...so you just need to encrypt the password that user types in and compare that encrypted password with the already encrypted password that you have stored for comparison.


Does that make sense?

For example:
if(password == crypt(pass))
{
//valid password
}

- Zac

dq9
Mar 24, 2008, 12:06 AM
I probably should have added earlier, I'm a bit slow (drugs, long story, I'm clean now)

None of that made any sense at all!
The method we had going earlier made a bit of sense. Can some one expand on that?

zcarter
Mar 24, 2008, 01:32 AM
Okay,

Example: - This is the database you stored the usernames and passwords in
------------------------------------------------------------------------------------------------------------------------------
U-name:
Zac
John

Pass:
(@*R)(@*D()@UF)(*@(*@ (This is Zac's encrypted password. His real UNENCRYPTED pass is lets say... 'ZacsPass')
AJ*()*)(R*(U@OIJFIL@F*L (This is Encrypted word for 'Johny123' HIS real UNENCRYPTED pass is lets say ... 'Johny123')
------------------------------------------------------------------------------------------------------------------------------
I input the username and password from the .txt file

Example:

fin >> fromFile_Username
fin >> fromFile_Password

Lets say I'm Zac..and I want to login.

It asks for my username...I type in 'Zac'
It asks for my password...I type 'ZacsPass' (this would be cin>>password)

So what now? Well, fromFile_Password is the password stored in the .txt file..which is encrypted...it is as I wrote: (@*R)(@*D()@UF)(*@(*@

But the password 'Zac' typed into the C++ program was 'ZacsPass'...thats not encrypted.

All you need to do ..is encrypt 'ZacsPass' and compare it with the password stored in the .txt file:

Example being.. (remember...the variable 'password' below was set to be 'ZacsPass' because thats what he typed in)
if(fromFile_Password == crypt(password))
{
//return true
}
else
{
//Invalid
}


Does that help?

dq9
Mar 24, 2008, 03:25 PM
Thank you for all your help so far.

I just have one last question about the password in the .txt file. When I encrypt it, is there a process I must follow or can I just type in random characters?

Ok sorry for doublepost

Forget all this encryption stuff. It's way over my head.

Can you guys please just explain how to do this with text from a regular text file. Or write a sample source code for me to look at.

zcarter
Mar 24, 2008, 09:04 PM
When I have time, Ill make u a video C++ tutorial.

- Zac

dq9
Mar 24, 2008, 09:25 PM
You don't have to go to that much trouble, but if you do thank you very much.

jeznav
Mar 27, 2008, 10:24 PM
Lol...@ everyone trying to make it difficult

All he wanted was to compare two strings from a text file(s) if it matches or not.

Anyways, the part in your program what made it not work because you did not have any sort of indexing to indicate WHERE it should read next.

What you should do is:

1. When the user enters a the username, have a WHILE LOOP until EOL and check if the username entered MATCHES the line read in user.txt. Also, you must create a variable that will increment by 1 every time the loop passes. The variable acts as a counter to indicate WHERE the username in the file was read. You must place it inside the while loop at the very end.

Conditions: IF the the username entered does not match ANY of the strings in the user.txt then you can print some error message not having a valid username.

2. So after that loop has finished and that we found a valid username, (the important thing that we have is a variable which is the index for the pass.txt) you need to use that variable to move down the list in the pass.txt

Do this by creating a FOR loop with that variable and in that for loop you read line.

Example: Suppose I typed in guest as my username. The while loop iterated the 3rd time through user.txt and found it. It also gave a number value of 2 in my counter variable (remember that the variable started from 0, that means value of 0 = 1st line , 1 = 2nd line , 2 = 3rd line, etc,). As it goes to the password checker, the for loop has read a line in pass.txt 3 times which give you the string value of -

Now you can do string comparison on the password entered and what it has read from the file..

Conditions: IF no matching password where on the list then again...put any error message you like indicating wrong password.

Hope that helps!!!

-jez

lee1210
Mar 27, 2008, 11:35 PM
As I've been reading more of these forums lately, I figured I may as well brush up on some C++. Excuse any C-isms, I tried to keep it stream and string oriented, etc.


#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;

string readString(ifstream&);

int main(int argc, char *argv[]) {
char user[80];
char pass[80];
string line_in;
string::size_type pos;
ifstream fp_in;
int count = 0;
string user_pass[100][2];

fp_in.open("users.txt"); //Open user file
if(fp_in == NULL) {
cout << "Could not open user file, exiting." << endl;
return -1;
}
while(!fp_in.eof()) { //While there is something to read
line_in=readString(fp_in); //Read a line from the file
if(line_in.length() > 0) { //If the line is non-empty
pos = line_in.find("\t",0); //Find the tab deliminter
if( pos != string::npos) { //If a tab is found..
user_pass[count][0] = line_in.substr(0,pos); //Get username
user_pass[count][1] = line_in.substr(pos+1); //Get password
//cout << "User: " << user_pass[count][0] << " Pass: " << user_pass[count][1] << endl;
count++;
}
}
}
fp_in.close(); //Done with the user file
if(!count > 0) {
cout << "Userlist is empty, exiting" << endl;
return -2;
}
int logged_in=0;
while(!logged_in) { //Force user to login. Give a quit option if you'd like
cout << "Enter user name: ";
cin >> user;
cout << "Enter password: ";
cin >> pass;
int found = 0;
for(int x=0;x<count && !found;x++) { //For each user/password pair..
if(user_pass[x][0].compare(user) == 0) { //Matched the username
found = 1;
if(user_pass[x][1].compare(pass) == 0) { //Matched user and pass
found = 1;
logged_in = 1;
} else {
cout << "Invalid password!" << endl;
}
}
}
if(!found) {
cout << "Invalid username!" << endl;
}
}
//Once we're done with that loop, they logged in successfully.
cout << "Logged in successfully!" << endl;
return 0;
}

string readString(ifstream& reader) {
char buffer[256];
reader.getline(buffer,256);
string str1 = buffer;
return str1;
}



I think it's better to just buffer up the users and passwords rather than rewinding the file each time to check if what was entered is in the file.

Hope this helps. There's no encryption, etc. I thought i'd keep it simple. I probably didn't catch all of the edge cases, but it should be enough to give you some ideas, get you started, etc.

-Lee

dq9
Mar 28, 2008, 12:43 AM
Thank you both! I still have yet to fix my problems, damn college taking up all my time, but I will definitely take pointers (get it? Ok bad joke.) from both your posts. Thanks for the help and I'll let you guys know how I make out.

Elie-amag
Apr 3, 2008, 11:14 AM
Thanks Lee. I also was struggling with username/password codes until I found out your code. It's working perfectly if someone only wants to deal with predefined usernames and passwords.

Is there any possibility to allow the user to change his assigned username or password (I mean personalize it)? In this case the code should be able to save both new username and password entries to the file users.txt at the position where was located the previous pair.

Hope you can help out with this by some other code posted.

lee1210
Apr 3, 2008, 01:33 PM
I would handle the possible changing of passwords the same way.

Read up the whole file and store all of the usernames and passwords.

If a user changes their password, you change it in memory, then write a routine that re-writes the whole file. You could certainly trouble yourself with getting the "right" line, but that's a lot of work for not a lot of payoff. You can apply this same method to adding or removing users as well. Read up the current file into memory, if a user is deleted you just remove them from the list in memory and write back. If a user is added, you just append to the list and write it out. The write routine would be the same for all of them.

If there is any possibility that more than one user is accessing the "system" at a time, that changes things dramatically. You then need to start with file locking, etc.

I know *I* can write that code, but the reason you're here is so you can learn. Get started and if you need help, post the code and what problem you're having. I am hoping this is a learning exercise for you, and not something that will be used seriously. If so, then file locking, encryption, etc. all start getting involved, and that really complicates an otherwise straight-forward way to learn basic programming and I/O.

-Lee

Elie-amag
Apr 8, 2008, 09:20 AM
Thanks again Lee. Don't worry, this is a learning exercise. I'm busy writing routines for password/username changing and file rewriting. I'm having a problem with variable updating between two functions.

In fact, in order to use your code efficiently, I divided it into smaller routines: OpenandReadFile(), Authenticate(), then created new routines: ChangeUsername(), ChangePassword() and RewriteFile().

Before a user can change his username/password, he has to enter first the current username/password. So the Authenticate() routine has to be called before he can be granted the possibility to change anything.

I'm using the variable count in the OpenandReadFile() routine and I need its updated value in the Authenticate routine. So I declared it globally. But I'm not getting the updated value when Authenticate() is called so the condition for the for loop is jeopardized. On the other hand I can't combine the OpenandReadFile() and the Authenticate() routines because I only need the Authenticate() routine when changing username/password.

Do you understand something to my problem? If so, could you help? Sorry I'm sort of newbie to C/C++.

Elie

lee1210
Apr 8, 2008, 09:31 AM
In a case like this I do not see the need for a global variable. In C++ you can pass by reference, or use C-style pointers to pass things to be modified into a function. You should be able to declare count in main, then pass it by reference or its pointer to OpenAndReadFile, which would update this value, then you can pass it to Authenticate.

I don't know if you're taking a course, learning from a book, etc. so I don't know if pass by reference or pointer manipulation would come first in your studies. Chances are pass by reference, as it's considered easier, but the result is the same.

-Lee

P.S. You could avoid worrying about pass by reference or pointers by having OpenAndReadFile return an int which is the count. That way you could just assign the function's result to a variable in main, then pass that to Authenticate.

Elie-amag
Apr 11, 2008, 08:30 AM
I got it thanks to Lee. You gave me the opportunity to learn about passing by reference. I passed "count" by reference to my OpenAndReadFile() routine then passed it to Authenticate. It worked perfectly. I'm even using the updated value of "count" in my RewriteFile() routine.

Now I'm having a problem with the RewriteFile() routine. After the file has been rewritten, the authentication is no more successful. I'm getting an "Incorrect password" message for all users even the ones whose password hasn't been changed. But when I check the file "users.txt" it has been modified correctly and the new information (password) is at the right position. I don't know if there is a mess up with the tab delimiter or line ending in my code below.

Note: When I rewrite the file "users.txt" manually, using notepad, and modify password or username, the authentication succeeds. Anyone knows the mistake in my code? Here is the code:

void RewriteFile()
{

int row, col;

ofstream myfile;
myfile.open ("users.txt");
if (myfile.is_open())
{
for (row=0; row<count; row++)
{
for (col=0; col<2; col++)
myfile<<user_pass[row][col]<<"\t";
myfile<<endl;
}

myfile.close();
}
else cout << "Unable to open file\n";

}


P.S. The RewriteFile() routine is called only after the change has been made in the user_pass[][] array.

Elie-amag
Apr 14, 2008, 05:45 AM
Just forget it. I saw my mistake in the code. I solved it with an if statement. I'm cleared now. In fact I was printing two tab delimiters per line, one after the username and another after the password. So the second tab was creating a mismatch in the password.

This code is now working:

void RewriteFile()
{

int row, col;

ofstream myfile;
myfile.open ("users.txt");
if (myfile.is_open())
{
for (row=0; row<count; row++)
{
for (col=0; col<2; col++)
{
myfile<<user_pass[row][col];
if(col==0) myfile<<"\t";
}
myfile<<endl;
}

myfile.close();
}
else cout << "Unable to open file\n";

}

aimmazleigh
Jan 22, 2011, 11:53 PM
Hi Lee,

I tried your code to learn but I received this debugging error :

undefined reference to `line_in(std::basic_ifstream<char, std::char_traits<char> >&)'

pointing to this statement : line_in=readString(fp_in);

my data file looks like this:

username password
username password
username password

May I know what is wrong with this?

thank you.

lee1210
Jan 23, 2011, 05:38 AM
Being that this is 3 years old I really don't remember it too well. I'm confused by how many uses of char[] there are, etc. Anyway, line_in is declared in the code I posted, so you missed something in a copy/paste or changed it afterwards. It should be type string and the declaration should be at the top of the function in question.

-Lee

jakelime
Sep 30, 2011, 01:08 PM
hi guys im new in this forum and im searching for a code to learn about using "ifstream and ofstream" thats when i came upon this forum..

im a newbie when it comes to coding so thats why i need some help..

we are to create a login program in c++ using those above and i dont know where to start..

this is what our prof output:
.................................................................................................... ....................
Username:
Password:

1.choose a different username:
2.choose a different password:
3.Exit
.................................................................................................... ....................
might be simple to others..

the thing is he said that if the username or password is incorrect, meaning it woud not login.. and should show that the username or password is wrong..

he uses "if, while, else"

he also used "switch"
i think he opened a different source file for each in "username, password and switch"

i dont know how did it.. and the username and password shows up in 2 different notepad and automatically updates itself when changing username and password..

if someone can help me with this it will be deeply appreciated.. tnx guys..

subsonix
Sep 30, 2011, 01:31 PM
Start over, use nothing but C++ strings and you get overloaded == signs and so on to compare the strings. No size, terminating zeros and so on to keep in mind. I assume this is not going to be used for a real login anyway right?


Edit: I'll add a bit to this. Have you done anything at all yet? Do you know how to print a string or get a string from stdin to a variable? If not, spend some time reading in your course book before attempting to solve it. It's not much to the exercise except, io to the terminal, and comparison of variables.

BALAKAJENTHIRAN
May 16, 2012, 08:09 AM
#include<iostream>
#include<stdlib.h>
#include<ios>
#include<fstream>
#include<iomanip>
#include<string>
#include<conio.h>
#include<windows.h>
using namespace std;

int add_record(int i);
int display_record(int i);
int edit_record(int i);
int detete_record(int i);
int search_record(int i);


struct
{


int id;
char sName[50];
char sParentName[50];
char sAddress[50];
char sIC[50];
char sPhoneNo[50];
char sMobileNo[50];
char sSchool[50];
char sQualification[50];
char sMajor[50];



}mv[100];
int main()
{
int iname=0;
fstream f;
int i=0;
ifstream ifile;
ifile.open("Save Data.txt");
while(ifile.peek()!=EOF)
{
ifile>>mv[i].id;
ifile>>mv[i].sName;
ifile>>mv[i].sParentName;
ifile>>mv[i].sAddress;
ifile>>mv[i].sIC;
ifile>>mv[i].sPhoneNo;
ifile>>mv[i].sMobileNo;
ifile>>mv[i].sSchool;
ifile>>mv[i].sQualification;
ifile>>mv[i].sMajor;
i++;
}
fflush(stdin);
ifile.close();

while(true)
{
system("cls");
int ch;
cout<<endl;
cout<<"\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"\t\t|---- Student Registration System ----|"<<endl;
cout<<"\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"\t\t"<<endl;
cout<<"\t\t"<<endl;
cout<<"\t\t|There Is "<<i<<" Entry(ies) In The Database|"<<endl;
cout<<"\t\t---------------------------------------"<<endl;
cout<<endl;
cout<<"\t\t .:::::::::::::::::::::::::::::::::.\n";
cout<<"\t\t .::| 1.Add Record(s) |::."<<endl;
cout<<"\t\t .::| |::."<<endl;
cout<<"\t\t .::| 2.Show All The Record(s) |::."<<endl;
cout<<"\t\t .::| |::."<<endl;
cout<<"\t\t .::| 3.Find Record(s) |::."<<endl;
cout<<"\t\t .::| |::."<<endl;
cout<<"\t\t .::| 4.Modified Record(s) |::."<<endl;
cout<<"\t\t .::| |::."<<endl;
cout<<"\t\t .::| 5.Remove Record(s) |::."<<endl;
cout<<"\t\t .::| |::."<<endl;
cout<<"\t\t .::| 6.Quit |::."<<endl;
cout<<"\t\t .:::::::::::::::::::::::::::::::::.\n";
cout<<endl;
cout<<"\tPlease Enter Your Choice by Choosing a Number {1-6} -> ";

cin>>ch;
switch(ch)
{

case 1:
i=add_record(i);
system("cls");

break;
case 2:
display_record(i);
system("cls");
break;
case 3:
search_record(i);
system("cls");
break;
case 4:
edit_record(i);
system("cls");
break;
case 5:
i=detete_record(i);
system("pause");
system("cls");
break;
case 6:
cout<<"\n\nThank You For Using This Program...."<<endl;
exit(i);
break;
default:
cout<<"\n\nInvalid Choice!!!"<<endl;
system("pause");
system("cls");
break;
}
}
return 0;
}
int add_record (int i)
{
int iname=0;
char reply;
int x;
ofstream ofile;
while(1)
{
system("cls");
mv[i].id=i+1;
cout<<"\t.::|You Are Entering the Details for Student ID = [ "<<i+1<<" ]|::."<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<"Enter Student Name : ";
cin>>mv[i].sName;
cout<<""<<endl;
cout<<"Enter Student Parent Name : ";
cin>>mv[i].sParentName;
cout<<""<<endl;
cout<<"Enter Student Address : ";
cin>>mv[i].sAddress;
cout<<""<<endl;
cout<<"Enter Student IC : ";
cin>>mv[i].sIC;
cout<<""<<endl;
cout<<"Enter Student Phone No. : ";
cin>>mv[i].sPhoneNo;
cout<<""<<endl;
cout<<"Enter Student Mobile No. : ";
cin>>mv[i].sMobileNo;
cout<<""<<endl;
cout<<"Enter Student School : ";
cin>>mv[i].sSchool;
cout<<""<<endl;
cout<<"Enter Student Qualification : ";
cin>>mv[i].sQualification;
cout<<""<<endl;
cout<<"Enter Student Major : ";
cin>>mv[i].sMajor;
cout<<""<<endl;
i++;
cout<<""<<endl;
cout<<"DATA ADDED SUCCESSFULLY!\n";
cout<<endl;


ofile.open("SaveData.txt",ios::trunc);
for(x=0;x<i;x++)
{
ofile<<mv[x].id<<endl;
ofile<<mv[x].sName<<endl;
ofile<<mv[x].sAddress<<endl;
ofile<<mv[x].sIC<<endl;
ofile<<mv[x].sPhoneNo<<endl;
ofile<<mv[x].sMobileNo<<endl;
ofile<<mv[x].sSchool<<endl;
ofile<<mv[x].sQualification<<endl;
ofile<<mv[x].sMajor<<endl;
}
fflush(stdin);
ofile.close();
cout<<"\nDo you want to Continue ?"<<endl;
cout<<"Y = Yes : "<<endl;
cout<<"N = No "<<endl;
cout<<"Please Enter your choice here -> ";
cin>>reply;
if(reply=='n')
{
return i;
break;

}
}
return i;
}
int display_record(int i)
{
int iname=0;
int y;
system ("cls");
cout<<"\t\t------Display Report------\n\n"<<endl;
for(y=0;y<i;y++)
{
cout<<"\t\t[Student's ID ]\t\t ="<<mv[y].id<<endl;
cout<<"\t\t[Student's Name ]\t\t ="<<mv[y].sName<<endl;
cout<<"\t\t[Student's Parent Name ]\t\t ="<<mv[y].sParentName<<endl;
cout<<"\t\t[Student's Address ]\t\t ="<<mv[y].sAddress<<endl;
cout<<"\t\t[Student's IC Number ]\t\t ="<<mv[y].sIC<<endl;
cout<<"\t\t[Student's Phone Number ]\t\t ="<<mv[y].sPhoneNo<<endl;
cout<<"\t\t[Student's Mobile Number]\t\t ="<<mv[y].sMobileNo<<endl;
cout<<"\t\t[Student's School ]\t\t ="<<mv[y].sSchool<<endl;
cout<<"\t\t[Student's Qualification]\t\t ="<<mv[y].sQualification<<endl;
cout<<"\t\t[Student's Major ]\t\t ="<<mv[y].sMajor<<endl;
cout<<endl;
}
cout<<"\t\t------End Of Report------"<<endl;
system("pause");
return 0;
}
int edit_record(int i)
{
int x;
int iname=0;
ofstream ofile;
if (i<0)
{
cout<<"\nNothing To Edit!\n";
system("pause");
return i;
}
int id,z;
system("cls");
cout<<"\nEnter The ID Number You Want To Edit:";
cin>>id;
if(id>i||id<0)
{
cout<<"\nNot Valid ID!\n";
system("pause");
return i;
}
for (z=0;z<=i;z++)
{
if(id==mv[z].id)
{
cout<<"\nEditing Student Details For ID : "<<mv[z].id<<endl<<endl;
cout<<"OLD Name :"<<mv[z].sName;
cout<<"-->NEW Name: ";
cin>>mv[z].sName;
cout<<"OLD Parent Name : "<<mv[z].sParentName;
cout<<"-->NEW PARENT NAME: ";
cin>>mv[z].sParentName;
cout<<"OLD Address : "<<mv[z].sAddress;
cout<<"-->NEW ADDRESS : ";
cin>>mv[z].sAddress;
cout<<"OLD IC No : "<<mv[z].sIC;
cout<<"-->NEW IC NO : ";
cin>>mv[z].sIC;
cout<<"OLD Phone No : "<<mv[z].sPhoneNo;
cout<<"-->NEW PHONE NO : ";
cin>>mv[z].sPhoneNo;
cout<<"OLD Mobile No : "<<mv[z].sMobileNo;
cout<<"-->NEW MOBILE NO : ";
cin>>mv[z].sMobileNo;
cout<<"OLD School : "<<mv[z].sSchool;
cout<<"-->NEW SCHOOL : ";
cin>>mv[z].sSchool;
cout<<"OLD Qualification : "<<mv[z].sQualification;
cout<<"-->NEW QUALIFICATION : ";
cin>>mv[z].sQualification;
cout<<"OLD Major : "<<mv[z].sMajor;
cout<<"-->NEW MAJOR : ";
cin>>mv[z].sMajor;
cout<<endl;

cout<<"DATA EDITED SUCCESSFULLY!!";
system("pause");
}
}
ofile.open("SaveData.txt",ios::trunc);
if(ofile.fail())
{
cout<<"error writting to Directory\n\n";
}
for(x=0;x<i;x++)
{
ofile<<mv[x].id<<endl;
ofile<<mv[x].sName<<endl;
ofile<<mv[x].sAddress<<endl;
ofile<<mv[x].sIC<<endl;
ofile<<mv[x].sPhoneNo<<endl;
ofile<<mv[x].sMobileNo<<endl;
ofile<<mv[x].sSchool<<endl;
ofile<<mv[x].sQualification<<endl;
ofile<<mv[x].sMajor<<endl;
}
fflush(stdin);
ofile.close();
return 0;
}
int search_record(int i)
{
int iname=0;
system("cls");
if(i<0)
{
cout<<"\nNothing To Search!\n";
system("pause");
return i;
}
int id,z;
cout<<"Enter The ID Number You Want To Search : ";
cin>>id;
if(id>i||id<0)
{
cout<<"\nNot Valid ID!\n";
system("pause");
return i;
}
for (z=0;z<=i;z++)
{
if (id==mv[z].id)
{
cout<<"\t\t[Student ID ]\t\t="<<mv[z].id<<endl;
cout<<"\t\t[Student Name ]\t\t="<<mv[z].sName<<endl;
cout<<"\t\t[Student Parent Name ]\t\t="<<mv[z].sParentName<<endl;
cout<<"\t\t[Stduent Address ]\t\t="<<mv[z].sAddress<<endl;
cout<<"\t\t[Student IC No ]\t\t="<<mv[z].sIC<<endl;
cout<<"\t\t[Student Phone No ]\t\t="<<mv[z].sPhoneNo<<endl;
cout<<"\t\t[Student Mobile No ]\t\t="<<mv[z].sMobileNo<<endl;
cout<<"\t\t[Student School ]\t\t="<<mv[z].sSchool<<endl;
cout<<"\t\t[Student Qualification]\t\t="<<mv[z].sQualification<<endl;
cout<<"\t\t[Student Major ]\t\t="<<mv[z].sMajor<<endl;
cout<<endl<<endl;
cout<<"DATA SEARCH SUCCESSFULLY!";
system("pause");
}
}
return 0;
}
int detete_record(int i)
{
int iname=0;
ofstream ofile;
int id,z,a=0;
system("cls");
if(i<=0)
{
cout<<"\nNothing To Delete!\n";
system("pause");
return 1;
}
cout<<"Enter The ID Number You Want To Delete : ";
cin>>id;
if (id>i||id<0)
{
cout<<"\nNot Valid ID!\n";
system("pause");
return i;
}
for(z=0;z<=i;z++)
{
if (id==mv[z].id)
{
cout<<"\t\t[Student ID ]\t\t="<<mv[z].id<<endl;
cout<<"\t\t[Student Name ]\t\t="<<mv[z].sName<<endl;
cout<<"\t\t[Student Parent Name ]\t\t="<<mv[z].sParentName<<endl;
cout<<"\t\t[Stduent Address ]\t\t="<<mv[z].sAddress<<endl;
cout<<"\t\t[Student IC No ]\t\t="<<mv[z].sIC<<endl;
cout<<"\t\t[Student Phone No ]\t\t="<<mv[z].sPhoneNo<<endl;
cout<<"\t\t[Student Mobile No ]\t\t="<<mv[z].sMobileNo<<endl;
cout<<"\t\t[Student School ]\t\t="<<mv[z].sSchool<<endl;
cout<<"\t\t[Student Qualification]\t\t="<<mv[z].sQualification<<endl;
cout<<"\t\t[Student Major ]\t\t="<<mv[z].sMajor<<endl;
cout<<endl<<endl;


strcpy(mv[z].sName,mv[z+1].sName);
strcpy(mv[z].sParentName,mv[z+1].sParentName);
strcpy(mv[z].sAddress,mv[z+1].sAddress);
strcpy(mv[z].sIC,mv[z+1].sIC);
strcpy(mv[z].sPhoneNo,mv[z+1].sPhoneNo);
strcpy(mv[z].sMobileNo,mv[z+1].sMobileNo);
strcpy(mv[z].sSchool,mv[z+1].sSchool);
strcpy(mv[z].sQualification,mv[z+1].sQualification);
strcpy(mv[z].sMajor,mv[z+1].sMajor);

}
if (mv[z].id>id)
{
strcpy(mv[z].sName,mv[z+1].sName);
strcpy(mv[z].sParentName,mv[z+1].sParentName);
strcpy(mv[z].sAddress,mv[z+1].sAddress);
strcpy(mv[z].sIC,mv[z+1].sIC);
strcpy(mv[z].sPhoneNo,mv[z+1].sPhoneNo);
strcpy(mv[z].sMobileNo,mv[z+1].sMobileNo);
strcpy(mv[z].sSchool,mv[z+1].sSchool);
strcpy(mv[z].sQualification,mv[z+1].sQualification);
strcpy(mv[z].sMajor,mv[z+1].sMajor);
cout<<"DATA DELETE SUCCESSFULLY!";

}
}
ofile.open("SaveData.txt",ios::trunc);
if(ofile.fail())
{
{
cout<<"error writting to directory\n\n";
}
}
for (z=0;z<i;z++)
{

ofile<<mv[z].id<<endl;
ofile<<mv[z].sName<<endl;
ofile<<mv[z].sAddress<<endl;
ofile<<mv[z].sIC<<endl;
ofile<<mv[z].sPhoneNo<<endl;
ofile<<mv[z].sMobileNo<<endl;
ofile<<mv[z].sSchool<<endl;
ofile<<mv[z].sQualification<<endl;
ofile<<mv[z].sMajor<<endl;
}
fflush(stdin);
ofile.close();
return--i;
}