Thread: Suggestions for this code? and 1 question....

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    39

    Suggestions for this code? and 1 question....

    consultation time! =) please?
    can u comment or suggest regarding this code?
    i left the "delete_student" as comment because im unsure how to do it...
    how would make it that when i delete an account, all other accounts on its right immediately moves 1 element to the left? to avoid empty slots between accounts....
    like this...

    NOTE: when i log in then log out, then type "quit" in both User and Pass [as required to terminate program]... it exits normally, yet when i rerun it, it doesnt work, i have to reopen the IDE to make it work again....

    thanks!

    studnum1 name1 bday1 course1
    studnum2 name2 bday2 course2
    studnum3 name3 bday3 course3
    that when i delete profile2
    it would be something like this....

    studnum1 name1 bday1 course1
    studnum(from 3 to 2) name(from 3 to 2) bday(from 3 to 2) course(from 3 to 2)

    instead of

    studnum1 name1 bday1 course1
    _______ ______ ____ _____
    studnum3 name3 bday3 course3
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct student_profile{
    	long long int stud_num;
    	char name[50];
    	char bday[50];
    	char course[50];
    	} STUDENT;
    
    void login(char username[], char password[]);
    char admin(void);
    	void add_student(STUDENT array[20]);
    	void display_students(STUDENT array[20]);
    	void modify_student(STUDENT array[20]);
    	//void delete_student(STUDENT array[20]);
    	//void delete_all(STUDENT array[20]);
    
    //*******************************************************************
    
    int main(void)
    {
    	STUDENT array[20];
    	char username[50];
    	char password[50];
    	char option;
    	int i;
    
    	for(i = 0; i < 20; i++)
    	{
    		array[i].stud_num = 0;
    	}
    
    	clrscr();
    
    	while(strcmp(username,"quit") != 0 || strcmp(password,"quit") != 0)
    	{
    
    	login(username, password);
    
    
    	if(strcmp(username, "ADMIN") == 0 && strcmp(password, "admin") == 0)
    	{
    		option = '0';
    
    		while(option != '7')
    		{
    			option = admin();
    			switch(option)
    			{
    				case '1' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Add Student] Function --->...");
    					getche();
    					add_student(array);
    					break;
    				}				//end of case '1'
    
    				case '2' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Modify Student Account] Function --->...");
    					getche();
    					modify_student(array);
    					break;
    				}				//end of case '2'
    
    				case '4' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Display all Accounts] Function --->...");
    					getche();
    					display_students(array);
    					break;
    				}				//end of case '4'
    
    				case '6' :
    				{
    					gotoxy(14,50); printf("Logging out...");
    					getche();
    					option = '7';
    					break;
    				}				//end of case '5'
    
    				case '7' :
    				{	gotoxy(14,48); printf("Terminating Program...");
    					getche();
    					return 0;
    				}				//end of case '6'
    
    				default :
    				{	printf("\n\nInvalid option");
    				}  				//end of default
    			}					//end of switch
    		}						//end of while(option != '6')
    	}							//end of while(strcmp...)
    
    	/*else if(ang account kay student)
    	{
    	}*/
    
    	else
    	{
    		if(strcmp(username,"quit") != 0 && strcmp(password,"quit") != 0)
    		{
    			gotoxy(19,30); printf("Invalid username or password.");
    		}
    
    	}
    	}							//end of super big loop! XD haha
    
    	gotoxy(19,31); printf("Terminating Program...");
    	getche();
    	return 0;
    }
    
    //*******************************************************************
    
    void login(char username[], char password[])
    {
    	clrscr();
    
    	gotoxy(19,5);  printf("********* ********    ***     *** *********");
    	gotoxy(19,6);  printf("**    ***  **    ***   ***   ***  **    ***");
    	gotoxy(19,7);  printf("**     **  **     ***  ***   ***  **     **");
    	gotoxy(19,8);  printf("***        **    ***   **** ****  ***      ");
    	gotoxy(19,9);  printf("********   *******     ** *** **  ******** ");
    	gotoxy(19,10); printf(" ********  **    ***   ** *** **   ********");
    	gotoxy(19,11); printf("      ***  **     **   **  *  **        ***");
    	gotoxy(19,12); printf("**     **  **     **   **     **  **     **");
    	gotoxy(19,13); printf("***    **  **     **   **     **  ***    **");
    	gotoxy(19,14); printf("********* ***     *** ****   **** *********");
    	gotoxy(45,40); printf("Just type 'quit' as username");
    	gotoxy(45,41); printf("and 'quit' also as password");
    	gotoxy(45,42); printf("to terminate program.");
    	gotoxy(45,45); printf("Student Record Management System");
    	gotoxy(19,22); printf("Enter username:");
    	gotoxy(35,22); gets(username);
    	gotoxy(19,24); printf("Enter password:");
    	gotoxy(35,24); gets(password);
    }
    
    //*******************************************************************
    
    char admin(void)
    {
    	char option;
    
    	clrscr();
    
    	gotoxy(14,2); printf("===================================================");
    	gotoxy(14,4); printf("               Administrator's Menu");
    	gotoxy(14,6); printf("===================================================");
    
    	gotoxy(14,10); printf("[1]     ---     Add a Student");
    	gotoxy(14,12); printf("[2]     ---     Modify a Student Account.");
    	gotoxy(14,14); printf("[3]     ---     Delete a Student Account.");
    	gotoxy(14,16); printf("[4]     ---     Display all Student Account Entries.");
    	gotoxy(14,18); printf("[5]     ---     Delete all Student Account Enrries.");
    	gotoxy(14,20); printf("[6]     ---     Logout");
    	gotoxy(14,22); printf("[7]     ---     Terminate Program.");
    
    	gotoxy(14,26); printf("===================================================");
    
    	gotoxy(14,30); printf("Please choose the task you want to perform.");
    	gotoxy(14,32); scanf("%c", &option);
    
    	return option;
    }
    
    //*******************************************************************
    
    void add_student(STUDENT array[20])
    {
    	int i = 0;
    	int empty;
    	char opt = 'y';
    
    	while(opt == 'y' && i < 19){
    	for(i = 0; i < 20; i++)
    	{
    		if(array[i].stud_num == 0)
    		{
    			empty = i;
    			break;
    		}
    	}
    
    	clrscr();
    
    	if(i >= 19)
    	{
    		gotoxy(14,2);  printf("===================================================");
    		gotoxy(14,4);  printf("            Enter account information");
    		gotoxy(14,6);  printf("===================================================");
    		gotoxy(14,22); printf("===================================================");
    		gotoxy(14,32); printf("There are currently no free slots.");
    		gotoxy(14,26); printf("===================================================");
    
    	}
    
    	else
    	{
    		gotoxy(14,2);  printf("===================================================");
    		gotoxy(14,4);  printf("            Enter account information");
    		gotoxy(14,6);  printf("===================================================");
    		gotoxy(14,22); printf("===================================================");
    		gotoxy(14,26); printf("===================================================");
    
    		gotoxy(16,9);  printf("Student Number  [Username]:");
    		gotoxy(16,13); printf("Name:");
    		gotoxy(16,15); printf("Birthdate:");
    		gotoxy(16,17); printf("Course:");
    
    		gotoxy(32,11); scanf("%lld", &array[empty].stud_num);
    		gotoxy(32,13); gets(array[empty].name);					//try unya ang fgets(); if sakto na...
    		gotoxy(32,13); gets(array[empty].name);
    		gotoxy(32,15); gets(array[empty].bday);
    		gotoxy(32,17); gets(array[empty].course);
    
    		gotoxy(14,24); printf("       --- Account successfully created ---");
    
    		if(i >= 19)
    		{
    			clrscr();
    			gotoxy(14,2);  printf("===================================================");
    			gotoxy(14,4);  printf("            Enter account information");
    			gotoxy(14,6);  printf("===================================================");
    			gotoxy(14,22); printf("===================================================");
    			gotoxy(14,24); printf("There are currently no free slots.");
    			gotoxy(14,26); printf("===================================================");
    
    		}
    
    		else
    		{
    			gotoxy(14,32); printf("Do you want to enter another account?");
    			gotoxy(14,33); printf("[y for yes, else no]");
    			gotoxy(14,35); opt = getche();
    		}
    	}
    	} // end of while loop
    
    	gotoxy(14,48); printf("<--- Redirecting to the Administrator's page...");
    	getche();
    }
    
    //*******************************************************************
    
    void display_students(STUDENT array[20])
    {
    	int i, j;
    	long long int temp;
    
    	clrscr();
    
    	gotoxy(6,2);  printf("=====================================================================");
    	gotoxy(6,4);  printf("STUDENT NUMBER");
    	gotoxy(25,4); printf("NAME");
    	gotoxy(45,4); printf("BIRTHDATE");
    	gotoxy(65,4); printf("COURSE");
    	gotoxy(6,6);  printf("=====================================================================");
    
    	//for(i = 1; i <= 20 - 1; i++)										/*passes*/
    	//{
    	//    for(j = 0; j <=  20-2; j++)  									/*one pass*/
    	 //   {
    	//        if(array[j].stud_num > array[j + 1].stud_num)    			/*one comparison*/
    	//        {
    	//			temp = array[j].stud_num;        						/*one swap*/
    	//            array[j].stud_num = array[j + 1].stud_num;
    	//          	array[j + 1].stud_num = temp;
            //    }
    	//	}
    	//}
    
    	for(j = 10, i = 0; array[i].stud_num != 0; j++, i++)
    	{
    		if(array[i].stud_num > 0)
    		{
    			gotoxy(6,j);  printf("%lld", array[i].stud_num);
    			gotoxy(25,j); puts(array[i].name);
    			gotoxy(45,j); puts(array[i].bday);
    			gotoxy(65,j); puts(array[i].course);
    		}
    	}
    
    	gotoxy(6,j+2); printf("\=====================================================================");
    	gotoxy(6,48);  printf("<----- Redirecting to the Administrator's Page...");
    	getche();
    }
    
    //*******************************************************************
    
    void modify_student(STUDENT array[20])
    {
    	int i;
    	long long int stud_num;
    
    	clrscr();
    	printf("\n\nPlease enter the student number of the account to be modified:\n\n");
    	scanf("%lld", &stud_num);
    	printf("\n\nSearching Student Account with Student Number %lld...", stud_num);
    
    	for(i = 0; i < 20; i++)
    	{
    		if(stud_num == array[i].stud_num)
    			break;
    	}
    
    	if(i >= 19)
    	{
    		printf("\n\nThe given student number did not match any of the existing accounts.\n");
    		printf("\n\nRedirecting to Administrator's Page...");
    		getche();
    	}
    
    	else
    	{
    		gotoxy(1,10);  printf("=====================================================");
    		gotoxy(1,12);  printf("Modifying the account of %s", array[i].name);
    		gotoxy(1,14);  printf("=====================================================");
    		gotoxy(1,16);  printf("Current Student Number: %lld", array[i].stud_num);
    		gotoxy(40,16); printf("New Student Number:");
    		scanf("%lld", &array[i].stud_num);
    		gotoxy(1,18);  printf("Current Name: %s", array[i].name);
    		gotoxy(40,18); printf("New Name: ");
    		gets(array[i].name);
    		gets(array[i].name);
    		gotoxy(1,20);  printf("Current Birthdate: %s", array[i].bday);
    		gotoxy(40,20); printf("New birthdate: ");
    		gets(array[i].bday);
    		gotoxy(1,22);  printf("Current Course: %s", array[i].course);
    		gotoxy(40,22); printf("New Course: ");
    		gets(array[i].course);
    
    		printf("\n\n=====================================================");
    		printf("\n\nStudent account modified...");
    		printf("\n\nRedirecting to Administrator's Page...");
    		printf("\n\n=====================================================");
    		getche();
    	}
    }
    
    //*******************************************************************
    
    /*void delete_student(STUDENT array[20])
    {
    	printf("\n\nEnter the ");
    
    }*/
    
    //********************************************************************
    
    /*void delete_all(STUDENT array[20])
    {
    
    }*/
    
    //*******************************************************************

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should use fgets instead of gets. That'll cause you all sorts of trouble with your input not being what you expect. Unless you eat the newline somehow between calls to gets, they'll skip your second attempt at input.

    You'll need to roll your own if you want the array to move stuff over when you "delete" from it. Just a simple for loop should be fine. You will also want to keep track of some way to know a record is empty (like setting the use ID to zero or something). But that's really a display issue more than anything else.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    actually in initialized every stud num to 0... to know if the account is empty...

    Code:
    for(i = 0; i < 20; i++)
    {
    	array[i].stud_num = 0;
    }
    You should use fgets instead of gets
    how? i thought fgets only works for files? sorry im new to this... xD
    thanks anyway dude!

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Huskar View Post
    how? i thought fgets only works for files? sorry im new to this... xD
    thanks anyway dude!
    use stdin as a file pointer

    FAQ has examples of the code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are basically 3 ways to organize the student records:

    1) Keep everything tightly *packed*, leaving no empty spaces. Everything stays sorted just as you want them to be, all the time.

    This is great, but very time consuming if you have a lot of additions and deletions. It causes a lot of data transferring, which adds to the risk of bad data being introduced into your database, sooner or later.

    2) You mark up to 10% or so of your deletions as "empty" perhaps with a zero student number, but keep that "slot". Then when an addition is needed, you only need to move students records over until you find an empty slot, and stop.

    Less time consuming. Displays and printouts will need the logic added so they don't print out the empty student records with zero student numbers. Lots of data movement still, but much less than in #1. If you have more than 15% of your entries empty, then it's time to re-pack the database and knock that number down to 5% to 10% or so of empty slots.

    3) You use an index to refer to the students. Data is never moved, except to re-pack the database if the number of empty records is too great. When they're deleted, their index may be set to 0 or the student number may be used for this. Newer students are tacked always onto the end of the physical data file, but the index may sort them in the way you want, with the key you want to sort them with.

    Except for re-packing the database, there is no movement of the records, once they're added to the file.

    I suggest you put in several print statements into the command line version, and see just how far into the program you're going when it "won't run" that second time. I have no experience with that kind of error. The only two idea's that come to mind are:

    1) A file was left open, and now your CL version want to re-open it, and can't.


    2) Memory was mangled somehow, and you can't run your program until you get a "fresh" block of memory (which you would get from the IDE).

    Since the error centers on the CL, print statements are your best way of troubleshooting the problem, I believe.

  6. #6
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    this is cool
    Code:
            gotoxy(19,5);  printf("********* ********    ***     *** *********");
    	gotoxy(19,6);  printf("**    ***  **    ***   ***   ***  **    ***");
    	gotoxy(19,7);  printf("**     **  **     ***  ***   ***  **     **");
    	gotoxy(19,8);  printf("***        **    ***   **** ****  ***      ");
    	gotoxy(19,9);  printf("********   *******     ** *** **  ******** ");
    	gotoxy(19,10); printf(" ********  **    ***   ** *** **   ********");
    	gotoxy(19,11); printf("      ***  **     **   **  *  **        ***");
    	gotoxy(19,12); printf("**     **  **     **   **     **  **     **");
    	gotoxy(19,13); printf("***    **  **     **   **     **  ***    **");
    	gotoxy(19,14); printf("********* ***     *** ****   **** *********");

    what IDE do you use?

    turboC++ 3?
    Last edited by creeping death; 03-31-2009 at 12:25 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    thanks vart and Adak...

    @creeping death, i use Turbo C30
    i was actually finding a generator for designs like that [i.e. you enter characters and then converts it to big "word arts" using fellow characters,] yet i dont know that to write on search engines... xD haha so i made one myself thanks anyway

Popular pages Recent additions subscribe to a feed