#include <stdio.h>
#include <conio.h> //
#include <string.h>
typedef struct U_Information
{ //Create structure
char Name [10];
char RRN [15];
char ID [16];
char Password [16];
char Phone [16];
char Address [40];
char Email [20];
} } UI;
character file_name[256]="User Data1.txt"; //Set file name
#define MAX_USER_DATA 100 //
UI g_user_data[MAX_USER_DATA]; //
int g_user_count; //
voidReadUserData() //Read saved user information
{
int index = 0;
FILE *p_file = fopen(gfile_name, "r");
if(p_file != NULL){
fread(&g_user_count, sizeof(int), 1, p_file); //
for(index = 0; index < g_user_count; index++){
fread(g_user_data + index, sizeof(UI), 1, p_file); //
}
fclose(p_file);
}
}
save voidSaveUserData() // save user information to file
{
int index = 0;
FILE *p_file = fopen(gfile_name, "w");
if(p_file != NULL){
fwrite(&g_user_count, sizeof(int), 1, p_file); //
for(index = 0; index < g_user_count; index++){
fwrite(g_user_data + index, sizeof(UI), 1, p_file); //
}
fclose(p_file);} }
void GetPassword (char farm_password[]) // Make it look like * when entering a password
{
int input = 0, index = 0;
while(input != 13){
input = getch(); //
if(input == 13) parm_password[index] = 0; //
else {
printf("*");
parm_password[index++] = input; //
}
}
printf("\n");
}
Create a member ID by entering voidUserRegister() //user information
{
printf ("To register as a member, enter the following information:\n\n");
printf("1. Input Name: ");
gets(g_user_data[g_user_count].Name);
printf("2. Input RRN: ");
gets(g_user_data[g_user_count].RRN);
printf("3. Input ID: ");
gets(g_user_data[g_user_count].ID);
printf("4. Input Password: ");
gets(g_user_data[g_user_count].Password);
printf("5. Input Phone Number: ");
gets(g_user_data[g_user_count].Phone);
printf("6. Input Address: ");
gets(g_user_data[g_user_count].Address);
printf("7. Input Email: ");
gets(g_user_data[g_user_count].Email);
g_user_count++; //
printf ("You are a member". Please log in again. \n\n");
}
int main () // Basic mail (enter member ID, password)
//->Success message on success, re-enter password if incorrect
//-> If the ID is incorrect, ask if you want to register as a member if there is no OR - Enter member information at YES Enter again at NO
{
int index = 0, input = 0;
char id[16], password[16], success_login = 0;
ReadUserData(); //
while(!success_login){
printf("Input ID: ");
gets(id);
printf("Input Password: ");
GetPassword(password);
for(index = 0; index < g_user_count; index++){ //
if(!strcmp(g_user_data[index].ID, id)){ //
if(!strcmp(g_user_data[index].Password, password)){ //
success_login = 1; //
printf ("You have successfully logged in". \n\n");
} else printf ("Make sure your password is entered correctly." \n\n");
break;
}
}
if(index == g_user_count){ //
printf("ID not registered". Would you like to register? ( y / n ): ");
input = getche();
printf("\n\n");
if(input == 'y' || input == 'Y') UserRegister(); //
else printf ("Please re-enter".\n\n");
}
}
SaveUserData(); //
}
This is a copy from the Internet. The source is http://www.tipssoft.com/bulletin/board.php?bo_table=old_bbs&wr_id=119 from here and modified a little bit. There are so many commands that I don't understand. I'm trying to understand. // Please give me an explanation Please make it simple.
c++ struct
#include <stdio.h>
#include <conio.h> //
#include <string.h>
First, conio.h is the header file. Search for it yourself.
#define MAX_USER_DATA 100 //
#define
is used to specify a macro. Here you specify MAX_USER_DATA
which specifies a value of 100 for MAX_USER_DATA
. This value cannot be changed to a constant.
#define
is used in programming competitions, but it is mainly used as follows.
#define REP(n) for(int i=0; i<n; i++)
Then, when using a repeat sentence, you can reduce the typo and write as follows:
int n;
cin>>n;
REP(n)
cout<<i; // output: 0123
UI g_user_data[MAX_USER_DATA]; //
You might think that the UI suddenly popped out.
typedef struct U_Information
{ //Create structure
char Name [10];
char RRN [15];
char ID [16];
char Password [16];
char Phone [16];
char Address [40];
char Email [20];
} } UI;
The facts are defined above. typedef
is an 'alias' of a data type. In other words, the alias of U_Information
is UI
. These two can be used together and they play the same role.
typedef
is also used in programming competitions.
typedef vector<int> vi;
typedef pair<int,int> pi;
You can reduce typos by writing:
int g_user_count; //
This may be difficult to name, but it is actually an integer variable declaration. In c/c++, the variable name can contain letters (a–z, A–Z), underscores (_), and numbers (0–9). However, the number cannot be the first variable name.
int abc;
int ab12;
int a_b_1;
int _ab;
int __ab;
int 1a; //compilation error!!!
fread(&g_user_count, sizeof(int), 1, p_file); //
This function is defined in stdio.h
as follows. A function used to enter a file.
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
ptr
is the pointer to the variable you want to store the value, size
is the number of bytes to store (it will be 4 bytes because it is an integer variable), and count
is the number of values you want to read in the document. The last stream
is a file stream. This was defined above.
FILE *p_file = fopen(gfile_name, "r");
fread(g_user_data + index, sizeof(UI), 1, p_file); //
fwrite(&g_user_count, sizeof(int), 1, p_file); //
fwrite(g_user_data + index, sizeof(UI), 1, p_file); //
Think of these functions as similar to those above. fwrite
is a file output function.
input = getch(); //
The getch()
function is defined in conio.h
. This function specifically returns the pressed key value when input is received, but the pressed value is not written to the console window.
if(input == 13) parm_password[index] = 0; //
The getch
function above enters the input
. A value of 13 means that you execute the following statement.In c/c++
, char
and int
can be freely converted. The value of 13 is the ASCII code value and the return value. By the way, the value of Enter is 10, but I'm not sure here.
parm_password[index++] = input; //
parm_password
is an array defined earlier. index
is also a variable defined earlier. Here, the unary operator ++ is used, which increases the value of the variable by 1. But there are two uses. It's whether it's used before or after a variable. Both increase the value of the variable by 1; if written before, it is a line increment and then a line increment after. You can easily understand it by looking at the following.
int a=0;
cout<<a++<<"; // output result: 0, value of a: 1
a=0;
cout<<++a<<"; // output result: 1, value of a: 1
g_user_count++; //
And that's also what I've described above. However, g_user_count
is defined as a global variable, so initialization to zero is automatic. So you don't have to initialize it, you can use your increase or decrease.
ReadUserData(); //
This is to call a function defined above.
void ReadUserData() //Read saved user information
{
int index = 0;
FILE *p_file = fopen(gfile_name, "r");
if(p_file != NULL){
fread(&g_user_count, sizeof(int), 1, p_file); //
for(index = 0; index < g_user_count; index++){
fread(g_user_data + index, sizeof(UI), 1, p_file); //
}
fclose(p_file);
}
}
for(index = 0; index < g_user_count; index++){ //
The top is called the for
statement. The for
statement is repeated and consists of the following:
for (//definition; // conditional statement; //increment)
{
//Code
}
The definition runs only the first time. Conditional statements are checked for each iteration, repeat if true, and exit if false. An increase or decrease is a phrase that runs after one iteration. I usually write my feelings and feelings there.
if(!strcmp(g_user_data[index].ID, id)){ //
if(!strcmp(g_user_data[index].Password, password)){ //
strcmp
is short for string compare
and is literally a function that compares two strings. That is, when g_user_data[index.ID
and id
are different, the code within the if
statement is executed.
success_login = 1; //
You put 1 in the variable defined above.
if(index == g_user_count){ //
If index
and g_user_count
are equal, the if
statement is executed.
if(input == 'y' || input == 'Y') UserRegister(); //
If input
is y
or Y
, the UserRegister
function is invoked.
SaveUserData(); //
The SaveUserData
function is invoked.
© 2024 OneMinuteCode. All rights reserved.