I am new c-programming student in TARC...I'm having troubles in my C code in my assignment..hopefully someone can help me out...
My assignment is about write the main program to allow users access into the main menu after they key in their user ID and correct password. After three unsuccessful attempts(i.e wrong password) the program exits.
the password and userID are stored inside a Password.txt...
the password must be encrypted. Example of encryption method :
a becomes z
b becomes a
c becomes b
.
.
z becomes y
e.g. the password "secret" is stored in the file as "rdbqds"
the real password is obtain by encrypting.
Now my problem is i dont know how to write a function to allow user to login and a function that check the correct userID with password from the Password.txt file....can someone help me??
Thanks a lot....
Code:
#include <stdio.h>
#include <stdlib.h>
#define N 10
#define FLUSH while(getchar()!='\n')
FILE *fpRead;
char userId[N],userPassword[N];
void menu(int *choice);
void getNewUser(char []);
void getPassword(char []);
void encrypt(char ptrEncrypt);
main()
{
int cont,choice;
if((fpRead=fopen("Password.txt","a+"))==NULL)
{
printf("\n Error - Cannot open the designated file \n");
}else
{
do{
system("cls");
menu(&choice);
switch(choice)
{
case 1:
{
getNewUser(userId);
break;
}
}
printf("Do you want to continue?(Y/N)>");
cont=getchar();
fflush(stdin);
}while(cont=='y' || cont=='Y');
}
return 0;
}
void menu(int *choice)
{
printf("Menu\n");
printf("----\n");
printf("1. Register new id\n");
printf("2. Login existing user id\n");
printf("9. Exit this program\n");
printf("Please enter your choice>");
scanf("%d",&*choice);
}
void getNewUser(char ptrUser[])
{
int check;
int count,i;
char temp;
do
{
printf("Please key in your user id>");
scanf("%Ns",ptrUser);
FLUSH;
for(count=0;*(ptrUser+count)!='\0';)
count++;
if(count>6)
printf("Our user id is limited in 1 to 6 character only.\n");
else
{
for(check=0,i=0;*(ptrUser+i)!='\0';i++)
{
temp=*(ptrUser+i);
if(temp>96 && temp<123)
check++;
else
{
printf("Please key in user id in lower case character type.\n");
break;
}
}
if(check==count)
{
for(i=0;i<6;i++)
fprintf(fpRead,"%c",ptrUser[i]);
fprintf(fpRead,"%\n");
}
}
}while(check!=count);
if(count<=6 && count==check)
getPassword(userPassword);
}
void getPassword(char ptrPassword[])
{
int count,i;
int check;
char temp;
char ptrEncrypt;
do
{
printf("Please key in your password>");
scanf("%Ns",ptrPassword);
FLUSH;
for(count=0;*(ptrPassword+count)!='\0';)
count++;
if(count>6)
printf("Our user password is limited in 1 to 6 character only.\n");
else
{
for(check=0,i=0;*(ptrPassword+i)!='\0';i++)
{
temp=*(ptrPassword+i);
if(temp>96 && temp<123)
check++;
else
{
printf("Please key in user password in lower case character type.\n");
break;
}
}
if(check==count)
{
for(i=0;*(ptrPassword+i)!='\0';i++)
{
ptrEncrypt=*(ptrPassword+i);
encrypt(ptrEncrypt);
}
fprintf(fpRead,"%\n");
}
}
}while(check!=count);
}
void encrypt(char ptrEncrypt)
{
if(ptrEncrypt==97)
{
ptrEncrypt+=25;
fprintf(fpRead,"%c",ptrEncrypt);
}
else
{
ptrEncrypt-=1;
fprintf(fpRead,"%c",ptrEncrypt);
}
}
Code tags added by Hammer