Trying to make a sort of username and password program.
It appears to work fine, i printed almost everything that gets stored so as to prove that my functions work.
In the login function, it shows that pTemp->user.password = "ken". but the second time i print it, inside the while loop, in the first if, pTemp->user.password = a garbage value.
I'm stumped as to where it changes.
May anyone tell me where?
Please go over my program for any other corrections and/or logic corrections
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct dataTag
{
char username[10];
char password[10];
};
struct nodeTag
{
struct dataTag user;
struct nodeTag *pNext;
};
typedef struct dataTag dataType;
typedef struct nodeTag nodeType;
nodeType *newNode(dataType Src, nodeType *pNext)
{
nodeType *pTemp;
pTemp = malloc(sizeof(nodeType));
pTemp->user = Src;
pTemp->pNext = pNext;
return pTemp;
}
void login(nodeType *pFirst, char user[], char pass[])
{
nodeType *pTemp;
pTemp = pFirst;
int error = 1;
printf("pFirst->user.password = %s, pTemp->user.password = %s\n", pFirst->user.password, pTemp->user.password);
getch();
while((pTemp->pNext != NULL) && error==1)
{
printf("Now comparing: %s with %s\n", user, pTemp->user.username);
getch();
if(strcmp(user, pTemp->user.username) == 0)
{
printf("user check successeful!\nNow comparing: %s with %s\n", pass, pTemp->user.password);
getch();
if(strcmp(pass, pTemp->user.password) == 0)
{
printf("password check successful! loop should exit!\n");
error = 0;
printf("error value successfully changed! %d\n", error);
getch();
}
}
pTemp = pTemp->pNext;
}
if(error != 0)
printf("FALSE");
else
printf("TRUE");
}
main()
{
dataType nFirst;
nodeType *pTemp, *pFirst, *pSecond, *pThird, *pFourth;
strcpy(nFirst.username, "pau");
strcpy(nFirst.password, "ken");
pFirst = newNode(nFirst, pSecond);
strcpy(nFirst.username, "admin");
strcpy(nFirst.password, "admin");
pSecond = newNode(nFirst, pThird);
strcpy(nFirst.username, "student");
strcpy(nFirst.password, "dlsu1234");
pThird = newNode(nFirst, pFourth);
strcpy(nFirst.username, "commat2");
strcpy(nFirst.password, "math");
pFourth = newNode(nFirst, pTemp);
printf("%s\n%s\n\n%s\n%s\n\n%s\n%s\n\n%s\n%s\n\n", pFirst->user.username, pFirst->user.password, pSecond->user.username, pSecond->user.password, pThird->user.username, pThird->user.password, pFourth->user.username, pFourth->user.password);
/*pFirst->pNext = pSecond;
pSecond->pNext = pThird;
pThird->pNext = pFourth;
pTemp=pFirst;*/
char username[10], password[10];
printf("Username: ");
gets(username);
printf("Password: ");
int i;
for(i=0;i<10;i++)
{
password[i]=getch();
switch(password[i])
{
case 13: i=10;
printf("\n");
break;
case 8:
i-=2;
printf("\b \b");
break;
default: printf("*");
}
}
if(strlen(username) == 0)
{
if(password[0] == 13)
printf("Complete the form.");
else
printf("Username missing.");
}
else
{
if(password[0] == 13)
printf("Password missing.");
else
login(pFirst, username, password);
}
getch();
}