Thread: What's wrong with my search program?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    31

    What's wrong with my search program?

    hi all experts...

    this is my assignment question, im required to create a search engine on a student database.I've created this but the program cant read the struct Record stud info .It's working well if u want to search for the 1st student but it returns 'wrong username/no record found'' for 2nd student onwards.
    Is it because of my search program or my files?I've posted both anyway...Hope somebody can help me here...thank you so much...

    This is my program to write my files:
    Code:
    #include <stdio.h>
    
    struct Login{
    char name[15];
    char password[15];
    };
    
    struct Course{
    	char name[50];
    	char id[10];
    	char year[10];
    };
    
    struct A{
    	char name[50];
    	char id[10];
    	char grade[3];
    };
    
    struct Record{
    	struct Login log;
    	char name[30];
    	long int id;
    	char year[10];
    	char major[20];
    	int credit_hours;
    	float gpa;
    	char deanlist[5];
    	struct A course_taken[6];
    };
    
    void clear_buffer(void);
    
    int main()
    {
    struct Login lecturer[3];
    
    struct Course electronics[2];
    struct Course tele[2];
    struct Course computer[2];
    struct Course multimedia[2];
    struct Course microwave[2];
    struct Course optical[2];
    struct Course nanotech[2];
    struct Course electrical[2];
    
    struct Record stud[3];
    
    FILE *lptr, *cptr, *rptr;
    int i,j;
    int ch,yesno;
    
    /* Writing Login usernames and passwords for students and lecturers into login.txt */
    
    if((lptr=fopen("login.txt","w"))==NULL){
    	printf("Error!File could not be opened");
    	exit(1);
    }
    else{
    	
    	for(i=0 ; i<3 ; i++){
    		printf("Please key in lecturer's username and password\n");
    		fgets(lecturer[i].name,15,stdin);
    		fgets(lecturer[i].password,15,stdin);
    		fwrite(&lecturer[i], sizeof(struct Login), 1, lptr);
    	}
    
    fclose(lptr);
    
    }
    
    /* Writing Courses name, id and year for each Major into course.txt */
    
    if((cptr=fopen("course.txt","w"))==NULL){
    	printf("Error!File could not be opened");
    	exit(1);
    }
    else{
    	printf("Major:Electronics\n");
    	for(i=0 ; i<2 ; i++){
    		printf("Please enter course name, id and year\n");
    		fgets(electronics[i].name,50,stdin);
    		fgets(electronics[i].id,10,stdin);
    		fgets(electronics[i].year,10,stdin);
    
    		fwrite(&electronics, sizeof(struct Course), 1, cptr);
    	}
    
    	printf("Major:Telecommunications\n");
    	for(i=0 ; i<2 ; i++){
    		printf("Please enter course name, id and year\n");
    		fgets(tele[i].name,50,stdin);
    		fgets(tele[i].id,10,stdin);
    		fgets(tele[i].year,10,stdin);
    
    		fwrite(&tele[i], sizeof(struct Course), 1, cptr);
    	}
    
    	printf("Major:Computer\n");
    	for(i=0 ; i<2 ; i++){
    		printf("Please enter course name, id and year\n");
    		fgets(computer[i].name,50,stdin);
    		fgets(computer[i].id,10,stdin);
    		fgets(computer[i].year,10,stdin);
    
    		fwrite(&computer[i], sizeof(struct Course), 1, cptr);
    	}
    
    	printf("Major:Multimedia\n");
    	for(i=0 ; i<2 ; i++){
    		printf("Please enter course name, id and year\n");
    		fgets(multimedia[i].name,50,stdin);
    		fgets(multimedia[i].id,10,stdin);
    		fgets(multimedia[i].year,10,stdin);
    
    		fwrite(&multimedia[i], sizeof(struct Course), 1, cptr);
    	}
    
    	printf("Major:Microwave\n");
    	for(i=0 ; i<2 ; i++){
    		printf("Please enter course name, id and year\n");
    		fgets(microwave[i].name,50,stdin);
    		fgets(microwave[i].id,10,stdin);
    		fgets(microwave[i].year,10,stdin);
    
    		fwrite(&microwave[i], sizeof(struct Course), 1, cptr);
    	}
    
    	printf("Major:Optical\n");
    	for(i=0 ; i<2 ; i++){
    		printf("Please enter course name, id and year\n");
    		fgets(optical[i].name,50,stdin);
    		fgets(optical[i].id,10,stdin);
    		fgets(optical[i].year,10,stdin);
    
    		fwrite(&optical[i], sizeof(struct Course), 1, cptr);
    	}
    
    	printf("Major:Nanotechnology\n");
    	for(i=0 ; i<2 ; i++){
    		printf("Please enter course name, id and year\n");
    		fgets(nanotech[i].name,50,stdin);
    		fgets(nanotech[i].id,10,stdin);
    		fgets(nanotech[i].year,10,stdin);
    
    		fwrite(&nanotech[i], sizeof(struct Course), 1, cptr);
    	}
    
    	printf("Major:Electrical\n");
    	for(i=0 ; i<2 ; i++){
    		printf("Please enter course name, id and year\n");
    		fgets(electrical[i].name,50,stdin);
    		fgets(electrical[i].id,10,stdin);
    		fgets(electrical[i].year,10,stdin);
    
    		fwrite(&electrical[i], sizeof(struct Course), 1, cptr);
    	}
    
    fclose(cptr);
    }
    
    /* Writing Records of each student into student.txt */
    
    if((rptr=fopen("student.txt","w"))==NULL){
    	printf("Error!File could not be opened");
    	exit(1);
    }
    else{
    	
    	for(i=0 ; i<3 ; i++){
    		printf("Enter student's Login username and password?([1]Yes/[0]No>>>>>");
    		scanf("%d",&yesno);
    		if(yesno==1){
    			printf("Please key in student's Login username and password\n");
    			clear_buffer();
    			fgets(stud[i].log.name,15,stdin);
    			fgets(stud[i].log.password,15,stdin);
    			
    			
    		}
    		else{
    			break;
    		}
    
    		printf("Please enter student's name, id, year, major, credit hours, gpa and dean's list[yes/no]\n");
    		fgets(stud[i].name,30,stdin);
    		scanf("%ld %s %s %d %f %s",&stud[i].id
    					  ,stud[i].year
    					  ,stud[i].major
    					  ,&stud[i].credit_hours
    					  ,&stud[i].gpa
    					  ,stud[i].deanlist);
    		
    		printf("Enter next course?([1]Yes/[0]No)>>>>>");
    		scanf("%d",&ch);
    		for(j=0 ; j<6 ; j++){
    			if(ch==1){
    				printf("Please enter student's course taken name, id and grade.\n");
    				clear_buffer();
    				fgets(stud[i].course_taken[j].name,50,stdin);	
    				fgets(stud[i].course_taken[j].id,10,stdin);
    				fgets(stud[i].course_taken[j].grade,3,stdin);
    		
    				printf("Enter next course?([1]Yes/[0]No)>>>>>");
    			
    				scanf("%d",&ch);
    				
                            }
    			else{
    				break;
    			}
    			
    		}
    	fwrite(&stud[i], sizeof(struct Record), 1, rptr);
    	}
    
    fclose(rptr);
    }
    
    return 0;
    
    }
    
    
    void clear_buffer(void)
    {
         int a;
         while((a=getchar())!='\n' && a!=EOF);
    }

    This is my search program:
    Code:
    #include<stdio.h>
    
    struct Login{
    char name[15];
    char password[15];
    };
    
    struct A{
    	char name[50];
    	char id[10];
    	char grade[3];
    };
    
    struct Record{
    	struct Login log;
    	char name[30];
    	long int id;
    	char year[10];
    	char major[20];
    	int credit_hours;
    	float gpa;
    	char deanlist[5];
    	struct A course_taken[3];
    };
    
    void clear_buffer(void);
    int login(int type,char *keyname,char *keypass);
    int seqSearch(int input,struct Record key,struct Record dir[]);
    
    
    int main()
    {
    
    struct Login lecturer[3];
    struct Record stud[3];
    struct Record query;
    
    int user,search,result;
    char username[15],userpass[15];
    
    
    printf("Login as...\n");
    printf("[1]Student [2]Lecturer [3]Guest\n");
    
    scanf("%d",&user);
    clear_buffer();
    
    if(user==3){
    	printf("No login required for guests\n\n");
    }
    else{
    
    	printf("Username:");
    	fgets(username,15,stdin);
    	printf("Password:");
    	fgets(userpass,15,stdin);
    
    	login(user,username,userpass);
    }
    
    
    
    printf("Search by...\n");
    printf("[1]Student Name\n[2]Student ID\n\n");
    printf("Selection >>> ");
    
    scanf("%d",&search);
    clear_buffer();
    
    switch(search){
    
    case 1:
    printf("Student Name:");
    
    fgets(query.name,30,stdin);
    
    result=seqSearch(1,query,stud);
    
    if(result==-1){
    	printf("No record found\n");
    	exit(1);
    }
    else{
    	printf("\n%s \t%ld \t%s \t%s \t%d \t%.2f \t%s\n",
    		stud[result].name, stud[result].id, stud[result].year, stud[result].major, 
    		stud[result].credit_hours,stud[result].gpa,stud[result].deanlist);
    }
    
     break;
    
    case 2:
    printf("Student ID:");
    
    scanf("%ld",&query.id);
    clear_buffer();
    
    result=seqSearch(2,query,stud);
    
    if(result==-1){
    	printf("No record found\n");
    	exit(1);
    }
    else{
    	printf("\n%s \t%ld \t%s \t%s \t%d \t%.2f \t%s\n",
    		stud[result].name, stud[result].id, stud[result].year, stud[result].major, 
    		stud[result].credit_hours,stud[result].gpa,stud[result].deanlist);
    }
    
     break;
    
    default:
    
    printf("invalid input\n");
    exit(1);
    
    break;
    
    }
    
    
    return 0;
    
    
    }
    
    
    
    
    
    void clear_buffer(void)
    {
         int ch;
         while((ch=getchar())!='\n' && ch!=EOF);
    }
    
    
    
    
    
    
    int login(int type,char *keyname,char *keypass){
    	int j;
    	FILE *rptr, *lptr;
    	struct Record stud[3];
    	struct Login lecturer[3];
    
    switch(type){
    
    case 1:
    if((rptr=fopen("student.txt","r"))==NULL){
    	printf("Error!File could not be opened\n");
    	exit(1);
    }
    else{
    	
    rewind(rptr);
    
    for(j=0 ; j<3 ; j++){
    	fread(&stud[j], sizeof(struct Record), 1, rptr);
    	if(((strcmp(keyname,stud[j].log.name))==0) && (strcmp(keypass,stud[j].log.password))==0){
    		printf("Login Successful\n\n");
    		break;
    	}
    	else{
    		if(j==2){
    			printf("Wrong username or password\n");
    			exit(1);
    		}
    	}
    }
    }
    break;
    
    case 2:
    if((lptr=fopen("login.txt","r"))==NULL){
    	printf("Error!File could not be opened\n");
    	exit(1);
    }
    else{
    	
    rewind(lptr);
    
    for(j=0 ; j<3 ; j++){
    	fread(&lecturer[j], sizeof(struct Login), 1, lptr);
    	if(((strcmp(keyname,lecturer[j].name))==0) && (strcmp(keypass,lecturer[j].password))==0){
    		printf("Login Successful\n\n");
    	break;
    	}
    	else{
    		if(j==2){
    			printf("Wrong username or password\n");
    			exit(1);
    		}
    	}
    }
    }
    break;
    
    
    default:
    printf("Invalid input\n");
    exit(1);
    
    break;
    }
    }
    
    
    
    
    
    
    
    
    
    
    
    int seqSearch(int input,struct Record key,struct Record dir[]){
    	int i;
    	FILE *rptr;
    
    	if((rptr=fopen("student.txt","r"))==NULL){
    	printf("Error!File could not be opened");
    	exit(1);
    }
    else{
    
    rewind(rptr);
    
    switch(input){
    
    case 1:
    
    for(i=0 ; i<3 ; i++){
    	fread(&dir[i], sizeof(struct Record), 1, rptr);
    	if((strcmp(key.name,dir[i].name))==0){
    		return(i);
    		break;
    	}
    	else{
    		if(i==2){
    			return(-1);
    		}
    	}
    }
    break;
    
    case 2:
    
    for(i=0 ; i<3 ; i++){
    	fread(&dir[i], sizeof(struct Record), 1, rptr);
    	if(key.id==dir[i].id){
    		return(i);
    		break;
    	}
    	else{
    		if(i==2){
    			return(-1);
    		}
    	}
    }
    break;
    
    default:
    
    printf("invalid input\n");
    exit(1);
    
    break;
    
    }
    }
    fclose(rptr);
    
    }
    Last edited by Salem; 04-28-2006 at 07:11 AM. Reason: Folding long code lines

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Bible program - search functionality
    By ChadJohnson in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 04-15-2005, 11:33 PM
  4. search array program
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 11-15-2002, 07:33 AM
  5. command line program, something wrong in the argv
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-26-2001, 09:36 AM