how would i approach this problem?

This is a discussion on how would i approach this problem? within the C Programming forums, part of the General Programming Boards category; how would i approach a problem like this? PROBLEM: to create an array-based application used to register, view, modify, delete ...

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

    how would i approach this problem?

    how would i approach a problem like this?

    PROBLEM:

    to create an array-based application used to register, view, modify, delete student accounts... the information for each student account must also be stored in a file, unless they are deleted...


    QUESTION:

    must i store first the information on an array of structs then copy all information to the file upon closing [if program reopened, the file is read and infos are again stored on the array of structs for another manipulation]

    OR

    are there other approaches in handling this problem?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    For a small (such as a less than a thousand students in a "school project application") it works fine to simply read in the file at startup, and write the new version out at the end of the application (you may want to rename the original file before writing out the new content, just in case something goes wrong there). The benefit here is that it's easy to do.

    The other obvious alternative involve storing the file as a binary file and read/write to the file as needed. But it's most likely both slower and harder to implement. Note that if you don't store it in binary, there is no (easy) way to read record n of the file, as text records are different sizes depending on content [usually at least].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    can i ask a BIG favor please?

    can i ask you sir to write a program, as simple as possible[but atleast also an array of struct]
    that covers up the principles above? huhu

    cuz i can barely understand yet, i can barely picture out what to do... and i have to pass this the day after tomorrow... T.T

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, but that's not how it works here (and it's not how it SHOULD work on any other forum). The way it works is that you POST YOUR code, then we comment/help you. Not "You post the assignment, we solve it".

    Edit: If I wasn't so kind, I would have posted something that solves the problem, but is of absolutely no help to you (because it would be obvious that it's written by someone who knows more C than your teacher, most likely)!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    ahh i see sir... XD
    because it would be obvious that it's written by someone who knows more C than your teacher, most likely
    lol....

    but my code is not something like [read, manipulate, overwrite...] T.T thats why im doomed... i can even barely make the add_student work T.T
    btw, here's my noobish code
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct student_info{
    	long long int stud_num;
    	char name[50];
    	char bday[30];
    	char course[50];
    		} student;
    
    void login(char username[], char password[]);
    char admin(void);
    	void add_student(student accounts[20], FILE *file);
    
    //*******************************************************************
    
    int main(void)
    {
    
    	char option;
    	char temp[100];
    	char username[30];
    	char password[30];
    	student accounts[20];
    	FILE *file = fopen("Data/Accounts.txt", "a+");
    
    	clrscr();
    
    	login(username, password);
    
    	if(strcmp(username, "ADMIN") == 0 && strcmp(password, "admin") == 0)
    	{
    		option = admin();
    
    		switch(option)
    		{
    
    			case '1' : {
    							clrscr();
    							printf("\n\nThis is the [Add a Student Account] Function.\n\n");
    							printf("--------------------------------------------------------");
    							add_student(accounts, file);
    							break;
    					}
    
    			default : printf("\n\nInvalid option.\n\n");
    		}
    	}
    
    	else
    	{
    		gotoxy(19, 24); printf("Invalid username or password.");
    	}
    
    	fclose(file);
    
    
    	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,45); printf("Student Record Management System");
    	gotoxy(19,18); printf("Enter username:");
    	gotoxy(35,18); gets(username);
    	gotoxy(19,20); printf("Enter password:");
    	gotoxy(35,20); gets(password);
    }
    
    //*******************************************************************
    
    char admin(void)
    {
    	char option;
    
    	clrscr();
    	printf("\n\n-----Welcome Administrator!!!-----\n");
    	printf("\n[1] --- Add a Student");
    	printf("\n[2] --- Modify a Student Account.");
    	printf("\n[3] --- Delete a Student Account.");
    	printf("\n[4] --- Display all Student Acount Entries.");
    	printf("\n[5] --- Logout");
    	printf("\n[6] --- Terminate Program.\n");
    
    	printf("\n\nPlease choose the task you want to perform.\n\n");
    	scanf("%c", &option);
    
    	return option;
    }
    
    //*******************************************************************
    
    void add_student(student accounts[20], FILE *file)
    {
    	int i;
    	int free;
    
    	for(i = 0; i < 20; i++)
    	{
    		if(accounts[i].stud_num == NULL)
    			free = i;
    	}
    
    	printf("\n\nEnter student information:\n\n");
    
    	printf("\n\nStudent Number [which will be the account's username]:\n\n\n\t\t");
    	scanf("%lld", &accounts[free].stud_num);
    	fprintf(file, "%lld", accounts[free].stud_num);
    	fputs("\t\t", file);
    
    	printf("\n\nName:\t\t");
    	gets(accounts[free].name);		// i dont know  why 2 gets(); work while 1 gets(); doesnt...
    	gets(accounts[free].name);
    	fprintf(file, "%s", accounts[free].name);
    	fputs("\t\t", file);
    
    	printf("\nBirthdate:\t");
    	gets(accounts[free].bday);
    	fprintf(file, "%s", accounts[free].bday);
    	fputs("\t\t", file);
    
    	printf("\nCourse:\t\t");
    	gets(accounts[free].course);
    	fprintf(file, "%s", accounts[free].course);
    	fputs("\n", file);
    
    	printf("\n\nAccount creation successful.\n");
    }
    
    //*******************************************************************

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so it wouldn't have helped you a whole lot if I showed you how to do ADD a student, since you have (nearly at least) written that.

    By the way, this is the sort of reply you would get if I did reply with a program to solve the problem directly:
    parallelogram for statement issues

    I suggest you write the function to list all students now, so that you can see if it's working or not.

    Code:
    	gets(accounts[free].name);		// i dont know  why 2 gets(); work while 1 gets(); doesnt...
    	gets(accounts[free].name);
    That's because you are mixing scanf() and gets().

    You should NEVER use gets - fgets() is a much better option, since it's much safer [assuming you don't lie to it].


    --
    Mats
    Last edited by matsp; 03-30-2009 at 05:18 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    btw, im abandoning this code...
    i want to code another one but i wanna try first coding one not involving FILES...
    maybe just add, delete, display students... to see whether my array of structs works...
    after that, ill revise including FILES...

    cuz im still new to structs, much more an array of it and the FILES... xD haha srry

    one question...
    how can i see if a struct[in an array] is empty or not?
    this is for finding free slots in the array...

    Code:
    for(i = 0; i < 20; i++)
    	{
    		if(accounts[i].stud_num == NULL)
    			free = i;
    	}
    will this work? considering that the struct is still the same?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, removing files from the project for now is not a bad idea. You should be able to add that as a simple add-on later on, just a "writetofile" and "readfromfile" pair that saves and loads the data from a file.

    Your "find a free slot" is OK. I wouldn't use NULL, since NULL is specifically for pointers, not to be used as an integer (although it may be equivalent to 0(ZERO), it is given a different name for a reason). You also have two things to consider:
    1. Once you have found a free slot in your array, you don't have to search any more, so you can break the loop. Your current code will only ever insert at the LAST free slot.
    2. What happens if there isn't any free slot?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 08:02 PM
  4. Replies: 8
    Last Post: 04-19-2006, 09:07 AM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

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