A recent course designed to require a user to log in to a program, usually software pages such as user login registration is related to the database. However, such small programs like curriculum design require less security and can be implemented in C + +.
First, we need to know the process of user login. We should not be unfamiliar with this.
First of all, the registration process .
1. Require user name (if the user name is already registered, re-enter)
2. Require the password to be entered.
3, Require re-enter the password (if two times the same password entered the successful registration, or re-registration). 】
followed by the landing process
1. Enter the user name (re-enter if the user name does not exist)
2, enter the password (if correct, enter the program, or re-login)
Then the following code is used to implement:
There are two ways to do this, one, using the FStream file read and write stream. Second, the use of structural chain list (this method is supplemented later)
I. read and write streams using fstream files
1 voidregist ()2{//Register3 Char*password =New Char[ -];//allocate 20 bytes, and then return the first address to password, which means that password can be seen as a 20-byte character array, the string4 stringname;5 /*struct User6 {7 string password;8 string name;9 }; Defined user structure body*/Ten user C; OneIfstream fin;//declaring Ifstream objects AFin.open ("USER.TXR"Ios::inch);//Associating Files -cout <<"Please enter user name:"; -Cin>>name; the while(Fin >> c.name >> c.password)//read successful return 1 - { - if(c.name==name) -{cout <<"the user name has been registered"<<endl;regist ();} + } - CharTest;intI=0; +cout <<"Enter Password:"; A while((Test=getch ())! ='\ r') at { - if(test==8)//ASC2 Code 8 for BACKSPACE - { - if(i>0) - { -cout << Test <<" "<<test; inpassword[i--]=' /'; - } to } + Else - { the if(i< -) * { $cout <<"*" ;Panax Notoginsengpassword[i]=test; -i++; the } + } A } thePassword[i] =' /'; +cout <<"Enter the password again" ; - Char*password2 =New Char[ -]; $I=0; $ while((Test=getch ())! ='\ r') - { - if(test==8) the { - if(i>0)Wuyi { thecout << Test <<" "<<test; -password2[i--]=' /'; Wu } - } About Else $ { - if(i< -) - { -cout <<"*"; Apassword2[i]=test; +i++; the } - } $password2[i]=' /'; the } the if(strcmp (PASSWORD,PASSWORD2)! =0) the { the -cout <<"password two times incorrect input"; in regist (); the } the Else About { thecout <<"Registration Successful"; the } the ofstream Fout; +Fout.open ("USER.TXR", Ios_base:: out|Ios_base::app); -Fout << name <<" "<< Password <<Endl; theFout.close ();//closes the file connection but does not delete the streamBayicout <<"Return to login screen in three seconds"; theNterface ();//Interface the}
1 voidlogo ()2{//Login3 Char*password =New Char[ -];4 stringname;5cout <<"User name:"<<Endl;6Cin>>name;7cout <<"Password:"<<Endl;8 CharTestintI=0;9 while((Test=getch ())! ='\ r')Ten { One if(test==8)//ASC2 Code 8 for BACKSPACE A { - if(i>0) - { thecout << Test <<" "<<test; -password[i--]=' /'; - } - } + Else - { + if(i< -) Acout <<"*"; atpassword[i]=test; -i++; - } - } -password[i]=' /'; - ifstream fin; inFin.open ("USER.TXR", Ios_base::inch); - if(Fin.fail ()) to { +cout <<"File open failed"; - } theUser C;intF1; * while(fin>>c.name>>C.password) $ {Panax Notoginseng if(c.name==name&&c.password==password) - { thecout <<"Landing Success"; +f1=1;//Login Success Flag A nterface (); the } + } - if(f1==0)//f1==0 Logon Failure $ { $cout <<"incorrect user name or password"; - logo (); - } the}
Design of the course designed by C + + for user Login registration