Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* fstream Edit File/Database By Moses Limbo
- */
- #include <conio.h>
- #include <stdio.h>
- #include <iostream.h>
- #include <string.h>
- #include <stdlib.h>
- #include <fstream.h>
- struct{
- char user[20],pass[20],name[50],address[50],course[20],section[20];
- }mehh[50];
- char input[50];
- int ctr=0,opt=0,len=0,flag=0,eopt=0;
- void load(),save(),space(),unspace(),login(),reg(),view(),edit(),loginmenu(),modify();
- void main(){
- clrscr();
- load();
- cout<<"[1] Login\n[2] Register\n[3] Exit\n";
- cin>>opt;
- switch (opt){
- case 1: login(); break;
- case 2: reg(); break;
- default: exit(0);
- }
- }
- void login(){
- char luser[20],lpass[20];
- clrscr();
- cout<<"Username: ";
- cin>>luser;
- cout<<"Password: ";
- cin>>lpass;
- for (int m=0;m<ctr;m++){
- if(!strcmp(luser,mehh[m].user)&&!strcmp(lpass,mehh[m].pass)){
- loginmenu();
- flag=1;
- }
- }
- if(flag==0){
- cout<<"Incorrect Username or Password...";
- getch();
- main();
- }
- }
- void reg(){
- clrscr();
- cout<<"Username: ";
- cin>>mehh[ctr].user;
- cout<<"Password: ";
- cin>>mehh[ctr].pass;
- cout<<"Name: ";
- unspace();
- strcpy(mehh[ctr].name,input);
- cout<<"Address: ";
- unspace();
- strcpy(mehh[ctr].address,input);
- cout<<"Course: ";
- cin>>mehh[ctr].course;
- cout<<"Section: ";
- cin>>mehh[ctr].section;
- cout<<"Login Successful";
- getch();
- ctr++;
- save();
- main();
- }
- void loginmenu(){
- clrscr();
- cout<<"[1] View\n[2] Edit\n[3] Logout";
- cin>>opt;
- switch(opt){
- case 1: view(); break;
- case 2: edit(); break;
- case 3: main(); break;
- default: main();
- }
- }
- void view(){
- clrscr();
- for(int n=0; n<ctr; n++){
- cout<<"Id: "<<n+1<<endl<<endl;
- cout<<"Username: "<<mehh[n].user<<endl;
- cout<<"Password: "<<mehh[n].pass<<endl;
- cout<<"Name: ";
- strcpy(input,mehh[n].name);
- space();
- cout<<"Address: ";
- strcpy(input,mehh[n].address);
- space();
- cout<<"Course: "<<mehh[n].course<<endl;
- cout<<"Section: "<<mehh[n].section<<endl;
- cout<<endl;
- }
- getch();
- loginmenu();
- }
- void edit(){
- clrscr();
- for(int i=0;i<ctr;i++){
- cout<<i+1<<"\t"<<mehh[i].user<<endl;
- }
- cout<<"Enter ID: ";
- cin>>eopt;
- eopt-=1;
- clrscr();
- cout<<"Edit: ";
- cout<<"1. Username\n2. Password\n3. Name\n4. Address\n5. Course\n6. Section\n";
- cin>>opt;
- switch(opt){
- case 1: cout<<"Username: ";
- gets(mehh[eopt].user);
- cout<<"Edit successful...";
- getch();
- modify(); //modifies the file
- main(); break;
- case 2: cout<<"Password: ";
- gets(mehh[eopt].pass);
- cout<<"Edit successful...";
- getch();
- modify(); //modifies the file
- main(); break;
- case 3: cout<<"Name: "; unspace();
- strcpy(mehh[eopt].name,input);
- cout<<"Edit successful...";
- getch();
- modify(); //modifies the file
- main(); break;
- case 4: cout<<"Address: "; unspace();
- strcpy(mehh[eopt].address,input);
- cout<<"Edit successful...";
- getch();
- modify(); //modifies the file
- main(); break;
- case 5: cout<<"course: ";
- gets(mehh[eopt].course);
- cout<<"Edit successful...";
- getch();
- modify(); //modifies the file
- main(); break;
- case 6: cout<<"section: ";
- gets(mehh[eopt].section);
- cout<<"Edit successful...";
- getch();
- modify(); //modifies the file
- main(); break;
- default: main();
- }
- }
- void load(){
- ifstream read;
- read.open("mehh.txt");
- read>>ctr;
- for (int i=0;i<ctr;i++){
- read>>mehh[i].user>>mehh[i].pass>>mehh[i].name>>mehh[i].address>>mehh[i].course>>mehh[i].section;
- }
- read.close();
- }
- void save(){
- ofstream write;
- write.open("mehh.txt");
- write<<ctr<<endl;
- for (int j=0;j<ctr;j++){
- write<<mehh[j].user<<" "<<mehh[j].pass<<" "<<mehh[j].name<<" "<<mehh[j].address<<" "<<mehh[j].course<<" "<<mehh[j].section<<endl;
- }
- write.close();
- }
- void modify(){ //function to edit the file
- ofstream write;
- write.open("mehh.txt");
- write<<ctr<<endl;
- for (int j=0;j<ctr;j++){
- if(j==eopt){ //to match what id to replace and overwrite the certain line...
- write<<mehh[eopt].user<<" "<<mehh[eopt].pass<<" "<<mehh[eopt].name<<" "<<mehh[eopt].address<<" "<<mehh[eopt].course<<" "<<mehh[eopt].section<<endl;
- }
- else{ //if it doesn't match
- write<<mehh[j].user<<" "<<mehh[j].pass<<" "<<mehh[j].name<<" "<<mehh[j].address<<" "<<mehh[j].course<<" "<<mehh[j].section<<endl;
- }
- }
- write.close();
- }
- void unspace(){
- gets(input);
- len=strlen(input);
- for (int k=0;k<len;k++){
- if (input[k]==' '){
- input[k]='_';
- }
- }
- }
- void space(){
- len=strlen(input);
- for (int l=0;l<len;l++){
- if (input[l]=='_'){
- input[l]=' ';
- }
- }
- cout<<input<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement