Thread: Help with creating a program to store student records using string arrays

  1. #16
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    I think you've shown enough effort for greater help. Your code refactoring should have come up with something like this.....
    Code:
    /* following define is to satisfy compiler so that it allows scanf/fscanf
    needed on the rather moany visual studio 2015 */
    #define _CRT_SECURE_NO_WARNINGS
    
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    
    
    typedef struct student
    {
        char fname[100], lname[100];
        double grades[100];
        int ID;
    }student;
    
    
    void AuthorInfo()
    {
        printf("Timofey Kononets\n2651763\n");
    }
    
    
    // result is 0 for a clean exit, nonzero for an exit forced by error
    void KillProgram(int result)
    {
        exit(result);
    }
    
    
    void DoLogin()
    {
        int a = 0; // terrible variable name, be descriptive so code reads similar to english
        char username[100];
        char password[100];
        char fusername[100];
        char fpassword[100];
        while (1)
        {
            printf("Press 0 to exit\nPress 1 for author info\nPress 2 to Log in\n");
            scanf("%d", &a);
            if (a == 0)
                KillProgram(0);
            if (a == 1)
                AuthorInfo();
            if (a == 2)
            {
                FILE *file = fopen("password1.txt", "r");
                if (file == NULL)
                {
                    printf("Can't open passwords file!!\nKilling program\n");
                    KillProgram(1);
                }
                printf("Enter a user name\n");
                scanf("%s", username);
                printf("Enter password\n");
                scanf("%s", password);
                while (fscanf(file, "%s%s", fusername, fpassword) != EOF)
                {
                    if (strcmp(username, fusername) == 0)
                    {
                        if (strcmp(password, fpassword) == 0)
                        {
                            printf("Login successful\n");
                        }
                        else
                        {
                            printf("Login unsuccessful\n");
                        }
                    }
                }
                fclose(file);
            }
    
    
        }
    }
    
    
    void DoSecondMenu( student records[] )
    {
        // work this out
    }
    
    
    int main()
    {
        student student_records[1000] = { 0 };
        DoLogin();
        DoSecondMenu(student_records);
        return 0;
    }

  2. #17
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I would have done something more like this.
    Code:
    int again = 1;
    while (again) {
        switch (main_menu()) {
        case 0:
            again = 0;
            break;
        case 1:
            show_info();
            break;
        case 2:
            if (logon())
                student_menu();
            break;
        }
    }
    This is just a quick sketch to show the idea and may not be quite right, and I haven't shown any function arguments.

    But the idea for the OP is that dividing your program up into useful functions can really simplify things.

  3. #18
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    I wanted mostly to show the propagation of the student_records array from main into DoSecondMenu() which should then be further propagated into SaveRecords(...) and LoadRecords(...) etc. DoSecondMenu should just show a menu and call more functions each with one simple purpose.
    I changed as little about the OP's code as possible. He used ifs rather than switch so I used his code, I just reorganised it into something more readable.
    Last edited by Hobbit; 12-05-2016 at 07:52 PM.

  4. #19
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    Thank you Hobbit. Ill look over and continue working on the code and if anything ill ask some more questions. It makes sense to put it into functions, i just didn't think of that. This is the first programming language that I'm learning and it makes sense more or less, but when I have to write it down to make a program, that's where I have a problem, but thanks again for the help and ill probably be back for some more questions later.

  5. #20
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    It's pretty much completely your own code, I just reorganised it. I've left a rather large TO DO bit for you to continue with. It only handles the first login menu so far, you have to work out how to write the DoSecondMenu bit and break down further code into functions.

    I also forgot a return statement in the DoLogin function to tell main that that function is finished with on success. Just add return; after you print login successful, or the infinite loop will just ask you to login again. One small caveat is you should fclose the file there just before the return. I'll leave you to add that. I'm tired though, got a real problem with noisy neighbours partying seven nights a week until 4.30-6am.

    You also may or may not want to #define the constant RECORD_SIZE as 1000 and change the array to student student_records[RECORD_SIZE]

    Here fixed it, was my mistake.
    Code:
    /* following define is to satisfy compiler so that it allows scanf/fscanf
    needed on the rather moany visual studio 2015 */
    #define _CRT_SECURE_NO_WARNINGS
    
    
    #define RECORDS_SIZE 1000
    
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    
    
    typedef struct student
    {
    	char fname[100], lname[100];
    	double grades[100];
    	int ID;
    }student;
    
    
    void AuthorInfo()
    {
    	printf("Timofey Kononets\n2651763\n");
    }
    
    
    // result is 0 for a clean exit, nonzero for an exit forced by error
    void KillProgram(int result)
    {
    	exit(result);
    }
    
    
    void DoLogin()
    {
    	int a = 0; // terrible variable name, be descriptive so code reads similar to english
    	char username[100];
    	char password[100];
    	char fusername[100];
    	char fpassword[100];
    	while (1)
    	{
    		printf("Press 0 to exit\nPress 1 for author info\nPress 2 to Log in\n");
    		scanf("%d", &a);
    		if (a == 0)
    			KillProgram(0);
    		if (a == 1)
    			AuthorInfo();
    		if (a == 2)
    		{
    			FILE *file = fopen("passwords1.txt", "r");
    			if (file == NULL)
    			{
    				printf("Can't open passwords file!!\nKilling program\n");
    				KillProgram(1);
    			}
    			printf("Enter a user name\n");
    			scanf("%s", username);
    			printf("Enter password\n");
    			scanf("%s", password);
    			while (fscanf(file, "%s%s", fusername, fpassword) != EOF)
    			{
    				if (strcmp(username, fusername) == 0)
    				{
    					if (strcmp(password, fpassword) == 0)
    					{
    						printf("Login successful\n");
    						fclose(file);
    						return; //end loop and go back to main
    					}
    					else
    					{
    						printf("Login unsuccessful\n");
    					}
    				}
    			}
    			fclose(file);
    		}
    
    
    	}
    }
    
    
    void DoSecondMenu( student records[] )
    {
    	// work this out
    }
    
    
    int main()
    {
    	student student_records[RECORDS_SIZE] = { 0 };
    	DoLogin();
    	DoSecondMenu(student_records);
    	return 0;
    }
    Last edited by Hobbit; 12-06-2016 at 12:17 AM.

  6. #21
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    I finally had a chance to work on the program since I'm having exams. I got some things done but im having difficulty with it printing the GPA and storing multiple grades in the index. When i ran it before i input the gpa in subchoice 4, it printed the name of the student i would input but didn't print the grade. And im not sure how it would be best to do the GPA because i know its the average of however many grades are inputted but im just not sure how to write it in code, as in whether it should be looped multiple times.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    
    
    
    
    #define RECORDS_SIZE 1000
    
    
    
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    
    
    
    
    
    
    typedef struct student
    {
    	char fname[100], lname[100];
    	double grades[100];
    	int ID;
    }student;
    
    
    int a = 0;
    int j = 0;
    int i = 0;
    int choice = 0;
    int subchoice = 0;
    char username[100];
    char password[100];
    char fusername[100], fpassword[100];
    void AuthorInfo()
    
    
    {
    	printf("Timofey Kononets\n2651763\n");
    }
    
    
    
    
    // result is 0 for a clean exit, nonzero for an exit forced by error
    void KillProgram(int result)
    {
    	exit(result);
    }
    
    
    
    
    void DoLogin()
    {
    
    
    	while (1)
    	{
    		printf("Press 0 to exit\nPress 1 for author info\nPress 2 to Log in\n");
    		scanf("%d", &choice);
    		if (choice == 0)
    			KillProgram(0);
    		if (choice == 1)
    			AuthorInfo();
    		if (choice == 2)
    		{
    			FILE *file = fopen("passwords1.txt", "r");
    			if (file == NULL)
    			{
    				printf("Can't open passwords file!!\nKilling program\n");
    				KillProgram(1);
    			}
    			printf("Enter a user name\n");
    			scanf("%s", username);
    			printf("Enter password\n");
    			scanf("%s", password);
    			while (fscanf(file, "%s%s", fusername, fpassword) != EOF)
    			{
    				if (strcmp(username, fusername) == 0)
    				{
    					if (strcmp(password, fpassword) == 0)
    					{
    						printf("Login successful\n");
    						fclose(file);
    						return; //end loop and go back to main
    					}
    					else
    					{
    						printf("Login unsuccessful\n");
    					}
    				}
    			}
    			fclose(file);
    		}
    	}
    }
    
    
    
    
    void DoSecondMenu(student records[])
    {
    	while (2)
    	{
    
    
    		printf("Press 1 for author info\nPress 2 to enter new student\nPress 3 to enter grade for existing student\nPress 4 to print student records\nPress 5 to save student records\nPress 6 to load student records\nPress 7 to logout\n");
    		scanf("%d", &subchoice);
    
    
    		if (subchoice == 1)
    		{
    			printf("Timofey Kononets\n2651763\n");
    		}
    		else if (subchoice == 2)
    		{
    			printf("Enter student first name\n");
    			scanf("%s", records[i].fname);
    			printf("Enter student last name\n");
    			scanf("%s", records[i].lname);
    			i++;
    		}
    		else if (subchoice == 3)
    		{
    			printf("Enter student ID\n");
    			scanf("%d", &j);
    			printf("Enter a numeric value for the gradepoint in this class\n");
    			scanf("%lf", records[j].grades[a]);
    			j++;
    			a++;
    
    
    		}
    		else if (subchoice == 4)
    		{
    			for (int k = 0; k < i; k++)
    			{
    				printf("First name: %s   Last Name: %s", records[k].fname, records[k].lname);
    				for (k = 0; k < j; k++)
    				{
    					printf("GPA: %lf", records[k].GPA / records[k].grades[a]);
    				}
    
    
    			}
    		}
    	}
    }
    
    
    
    
    int main()
    {
    	student student_records[RECORDS_SIZE] = { 0 };
    	DoLogin();
    	DoSecondMenu(student_records);
    	return 0;
    }

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The first step is to remove all those global variables, and declare them locally only within the functions which need them.

    The second step is to examine what that 'a' variable is all about.

    You see, you need another member of the structure which tells you how many grades each student has. Your single global variable 'a' is really just the total number of grades entered (across all students).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Student Records Using Binary Search Tree
    By ThR1LL in forum C Programming
    Replies: 2
    Last Post: 05-15-2014, 03:32 PM
  2. Student records program
    By mugiwara528 in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 02:53 AM
  3. Sorting student records by letter grade
    By holly14326 in forum C Programming
    Replies: 2
    Last Post: 07-27-2010, 07:49 PM
  4. C programming sort of student records
    By holly14326 in forum C Programming
    Replies: 1
    Last Post: 07-26-2010, 10:58 PM
  5. Student records
    By CoreLink in forum C Programming
    Replies: 27
    Last Post: 04-25-2010, 01:57 PM

Tags for this Thread