What's wrong with my search program?

This is a discussion on What's wrong with my search program? within the C Programming forums, part of the General Programming Boards category; hi all experts... this is my assignment question, im required to create a search engine on a student database.I've created ...

  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

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    31
    ehmm...is anyone free to help me?I only have 2 days to my assignment due date...

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Arrrgh! Too... much... code...... go on boys... I'll stay here...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You are using fgets() to input all your strings, but I can not see any code that removes the '\n' from that strings. Might be the reason why strcmp() fails.
    Kurt

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > struct A course_taken[6];
    One file has this

    > struct A course_taken[3];
    The other file has this.

    It's all going to be the wrong shape after the first record!

    Tip:
    Put all your structs in say
    database.h

    and have
    #include "database.h"
    in both your source files, then at least they will retain some consistency.

    > printf("Major:Electronics\n");
    > .....
    Notice how the code repeats itself over and over with very minor changes from one block to the next.
    This is just begging to be turned into a function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    31
    Oppps, that's a silly mistake i made...Thanks alot guys and especially Salem!

    Actually there are more stuff to search for in this program which i haven included.The rest will be likely to return more than one result,unlike what i did before.May i know how can i do the coding in both my main and seqSearch functions to display more than one result?
    thank you

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, 02: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, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21