Hier ist mein Code:C - Hashtable funktioniert nicht richtig
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define m 9
int flag1=1;
//Creating memory for username and password
struct node // The list that is being used to store username and password
{
char username[30];
char password[30];
struct node *next;
};
int getkey(char[30]); //a method to get "randomly" a key
int hash(int); // a method to create the hashcode
void insert(struct node **,char[30],char[30]); //adding a node struct to a specific possition in hashtable
void initializeHashTable(struct node **);
void search(struct node **,char[30]);//check if password exists
void searchuser(struct node **,char[30]); //check if username exists
void print(struct node**);//printing the hashtable
void print(struct node **T)
{
struct node *tmp;
int i,flag=0;
printf("-------Data Base-------\n");
for(i=0;i<m;i++)
{
tmp=*(T+i);
while(tmp!=NULL)
{
printf("Username: %s Password: %s\n",tmp->username,tmp->password);
tmp=tmp->next;
}
}
}
void search(struct node **T,char password[30])
{
struct node *tmp;
int i,flag=0;
for(i=0; i<m; i++)
{
tmp=*(T+i);
while(tmp!=NULL)
{
if(strcmp(tmp->password,password)==0)
{
flag=1;
break;
}
tmp=tmp->next;
}
if(flag==1)
break;
}
if(flag==1)
printf("Authentication Successfull\n");
else
printf("Authentication Failed\n");
}
void searchuser(struct node **T,char user[30])
{
struct node *tmp;
int i,flag=0;
for(i=0; i<m; i++)
{
tmp=*(T+i);
while(tmp!=NULL)
{
if(strcmp(tmp->username,user)==0)
{
flag=1;
break;
}
tmp=tmp->next;
}
if(flag==1)
break;
}
if(flag==0)
{
printf("Username NOT Found\n");
flag1=0;
}
}
void initializeHashTable(struct node **T)
{
int i;
for (i=0; i<m; i++)
*(T+i)=NULL;
}
int getkey(char name[30])
{
int i=0;
int key=0;
for (i=0; i<15; i++)
{
key+=name[i];
}
return key;
}
int hash(int key)
{
return key%m;
}
void insert (struct node **T,char name[30],char password[30])
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
strcpy(newnode->username,name);
strcpy(newnode->password,password);
int key;
key=getkey(name);
int hashcode;
hashcode=hash(key);
//printf("Key:%d\n",key);
// printf("Hashcode:%d\n",hashcode);
if(*T+hashcode==NULL)
{
(*(T+hashcode))->next=newnode;
newnode->next=NULL;
}
else
{
newnode->next=(*(T+hashcode));
(*(T+hashcode))=newnode;
}
}
int main()
{//possible content of file: phill 1234
char choice;
struct node **T;
struct node **head;
head==NULL;
T=(struct node**)malloc(m*sizeof(struct node));//creating the hashmap
FILE *fp;
char name[30];
char pass[30];
fp=fopen("passwords.txt","r");
if (fp==NULL)
{
printf("File Not Found");
return 0;
}
else
{
initializeHashTable(&(*T));
while(!feof(fp)) // end of file
{
fscanf(fp,"%s %s",name,pass); //reads until first wild space,
//one each loop, you get 1 username and 1 password from the file
//adding them on hashmap
insert(&(*T),name,pass);
}
}
//user authentication
do{
printf("Enter Username:\n");
fflush(stdin);
scanf("%s",&name);
searchuser(&(*T),name);
if(flag1==1)
{
printf("Enter Password:\n");
scanf("%s",&pass);
search(&(*T),pass);
}
printf("Try Again? (y/n)\n");
fflush(stdin);
scanf("%d",&choice);
}while(choice!='y');
free(T);
free(fp);
free(head);
}
- Mein Code funktioniert auf der Schleife in Ordnung, es funktioniert die Authentifizierung erfolgreich, aber dann manchmal stürzt, oder i gibt den gleichen Details (Benutzername und Passwort) und irgendwie die Authentifizierung fehl
Teil des Codes, die ich seinen Buggy glauben:
//user authentication do{ printf("Enter Username:\n"); fflush(stdin); scanf("%s",&name); searchuser(&(*T),name); if(flag1==1) { printf("Enter Password:\n"); scanf("%s",&pass); search(&(*T),pass); } printf("Try Again? (y/n)\n"); fflush(stdin); scanf("%d",&choice); }while(choice!='y');
wenn ich
do..while(choice=='y')
setzen diewhile choice=='y'
die Schleife laufen Mittel stoppt esif choice=='y'
die
Ausgabe ein bisschen seltsam ist: Authenticator nach dem 1. nicht richtig funktioniert versuchen
Thanks A Los für deine Zeit!
Schlagen Sie vor, dass Sie einen Debugger verwenden, um die Ausführung Ihres Programms zu verfolgen und seinen Status zu überprüfen. Das ist der beste Weg, um solche Probleme zu debuggen (man kann sich nicht immer an SO wenden, um für Sie zu debuggen). – kaylum
Ich habe seit Wochen versucht, habe keine Lösung gefunden ... wie kann ich einen Debugger verwenden? – Phill
Versuchen Sie, fgets() anstelle von scanf() zu verwenden. Auf der letzten scanf() gibt es eine gute Möglichkeit, dass ein unerwünschter Zeilenumbruch-Char, der übertragen wurde. Fügen Sie diese Zeile 'fgets (stdin, marble, sizeof (marble))' nach dem letzten scanf() ein. und deklariere 'char Müll [50];'. –