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

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    18

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

    First I'll post the assignment, then explain my problem, and then post my code.

    Assignment:
    Instructions:
    You are to create a program that tracks student grades.
    When the program launches the user should be presented with three options;

    Press 0 to exit
    Press 1 for author info
    Press 2 to Log in

    Selecting 0 exits the program
    Selecting 1 prints your name and student id
    Selecting 3 prompts the user to enter a user name and password.
    You should then open the file "passwords" and see if the user is authorized to use the program*
    If the username + password was not valid print an error to the user and go back to the previous menu.

    *The passwords file will contain a list of user names and passwords, in the format of:
    user1 password1
    user2 password2
    user3 password3

    If the username + password was valid present the user with a new menu:
    Press 1 for author info
    Press 2 to Enter new student
    Press 3 to enter grade for existing student
    Press 4 to print student records
    Press 5 to save student records
    Press 6 to load student records
    Press 7 to logout

    Selecting 0 exits the program
    Selecting 1 prints your name and student id
    Selecting 2 Prompts the user for a first and last name. Store this information in the system as a student record.
    You should be able to handle 1000 students.
    Student records have
    First name,
    Last name,
    Student ID,
    A list of grades (up to 100 classes). Grades are entered in grade point format (4.0 for an A)

    Selecting 3 prompts the user for a student ID and a grade to be entered. Store this grade in that student's record.
    Selecting 4 prints the names, ids, GPA and a list of all earned grades for every student. See the sample file for formatting.
    Selecting 5 prompts the user for a file name and saves all stored student records to a file. (or in other words, save game)
    Selecting 6 prompts the user for a file name and loads student information from a file (or in other words, load game)
    Selecting 7 Log out the current user and return to the shorter menu from above.

    Your program should also keep a log file of all launch, log in, failed log in, log out, save, load, student creation, grade entry attempts, as well as application exit
    This file should be called students.log persist across multiple application starts.
    except for exit and start entries, the current logged in username should also be entered in the log.

    Note: When loading you can assume all previous student entries are being deleted, rather than detecting repeat students.


    Problem:
    I began writing the code and am having difficulty with it checking if the entered username and password are similar with the username and password in the file. note that I have a file that has usernames and passwords corresponding to those usernames listed in a format explained in the instructions. If someone can help me start this correctly and throughout this program as i encounter problems that would be awesome.

    Code:
    #include<stdio.h>
    #include <string.h>
    
    
    typedef struct student
    {
        char fname[100], lname[100], ID[100];
        double grades;
    }student;
    
    
    int main()
    {
        student arr[1000];
    
    
        int i = 0;
        int a = 0;
        int sim;
        char username[100];
        char password[100];
        char fusername[100], fpassword[100];
    
    
    
    
        while (1)
        {
    
    
            printf("Press 1 for author info\nPress 2 to Log in\n");
    
    
            scanf("%d", &a);
    
    
            if (a == 1)
            {
                printf("Timofey Kononets\n2651763\n");
            }
    
    
            if (a == 2)
            {
                FILE *file = fopen("passwords.txt", "r");
                printf("Enter a user name and password\n");
                scanf("%s%s", username, password);
            
    
    
                while (fscanf(file, "%s%s", fusername, fpassword) != EOF);
                {
                    if (strcmp(username, fusername) == 0);
                    {
                        if (strcmp(password, fpassword) == 0);
                        {
                            printf("Login successful\n");
                        }
                    }
                }
    
    
        
                }
            if (a == 0)
            {
                exit(0);
            }
    
    
        
        }
        system("pause");
        return (0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
                while (fscanf(file, "%s%s", fusername, fpassword) != EOF);
                {
                    if (strcmp(username, fusername) == 0);
                    {
                        if (strcmp(password, fpassword) == 0);
    Remove the ; at the end of each of these lines.

    You've written this
    Code:
                while (fscanf(file, "%s%s", fusername, fpassword) != EOF) {
                   // do nothing
                }
                {
                    if (strcmp(username, fusername) == 0) {
                            // do nothing
                    }
                    {
                        if (strcmp(password, fpassword) == 0) {
                            // do nothing
                        }
    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.

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    Thanks, but is the while loop given the correct statement because its supposed to scan the file and match the previously entered username with the username in the file and the entered password with the password in the file but when i try to run it, it breaks at the loop and i cant seem to figure out what the problem is

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Did you remove the semicolons as per Salem's advice?
    Post your current code.

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    Code:
    #include<stdio.h>
    #include <string.h>
    
    
    typedef struct student
    {
        char fname[100], lname[100], ID[100];
        double grades;
    }student;
    
    
    int main()
    {
        student arr[1000];
    
    
        int i = 0;
        int a = 0;
        int sim;
        char username[100];
        char password[100];
        char fusername[100], fpassword[100];
    
    
    
    
        while (1)
        {
    
    
            printf("Press 1 for author info\nPress 2 to Log in\n");
    
    
            scanf("%d", &a);
    
    
            if (a == 1)
            {
                printf("Timofey Kononets\n2651763\n");
            }
    
    
            if (a == 2)
            {
                FILE *file = fopen("passwords.txt", "r");
                printf("Enter a user name and password\n");
                scanf("%s%s", username, password);
            
    
    
                while (fscanf(file, "%s%s", fusername, fpassword) != EOF)
                {    
                    if (strcmp(username, fusername) == 0)
                    {
                        if (strcmp(password, fpassword) == 0)
                        {
                            printf("Login successful\n");
                        }
                    }
                }
            }
            if (a == 0)
            {
                exit(0);
            }
    
    
            
    
    
        }
        system("pause");
        return (0);
    }

  6. #6
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    And this is what i have in the passwords.txt file that i made. Its formatted where the first word is the username and second word is the password. Ex: Kenny is the username and Lofton is the password. Its supposed to scan this file and then match the entered data to see if it matches whats in the file.

    Passwords.txt:
    Kenny Lofton
    Otto Orf
    Einar Diaz
    Huong Pham
    Hien Pham
    Mahdi Mahmoud
    Oussama Stiti
    Omar Vizquel

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It seems to work for me.

    Press 1 for author info
    Press 2 to Log in
    2 <-- is it possible you're forgetting to do this???
    Enter a user name and password
    Omar Vizquel
    Login successful <-- SUCCESS!
    Press 1 for author info
    Press 2 to Log in

    Usually we would have a separate Username and Password prompt.
    You should check that the password file opened properly.
    You should close the password file.
    You should use a if/else if structure or a switch statement:
    Code:
    if (a == 0) {
        ...
    }
    else if (a == 1) {
        ...
    }
    else if (a == 2) {
        ...
    }
    else {
        ...
    }
    
    // or use a switch
    
    switch (a) {
    case 0:
        ...
        break;   // don't forget the break statements!
    case 1:
        ...
        break;
    case 2:
        ...
        break;
    default:
        ...
        break;   // doesn't really need a break since it's the last one
    }

  8. #8
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    This is what i get when i enter the username and password

    Help with creating a program to store student records using string arrays-capture-png

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It looks like your passwords file is not opening properly, hence my advice to check it:
    Code:
    FILE *file = fopen("passwords.txt", "r");
    if (file == NULL) {
        perror("passwords.txt");
        exit(EXIT_FAILURE);     // remember to include <stdlib.h>
    }
    ...
    // and remember to
    fclose(file);
    The password file is probably in the wrong directory. I don't know where it should go, but you could try it in different places in the project directory.

  10. #10
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Are you on windows? Hiding file extensions? Have you accidentally called your file passwords.txt.txt in which case the fopen will fail which is signified by fopen returning a NULL pointer ?

  11. #11
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    Yes I'm on windows. That could be it because after i input the if statement that algorithm gave me, it doesn't even pull up the program, every time i try to run it on visual studio it doesn't even pull up the command prompt. So should i make another file?

    Also, I'm wondering how should i code if someone enters an invalid username or password for it to say login unsuccessful

  12. #12
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    the passwords file is a txt document so isn't that why the .txt extension is put so that it reads from the txt file?

  13. #13
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    call up file explorer, check the view menu and make sure the box show file extensions is ticked. Then check your passwords file is 1) where it is supposed to be ( same directory as the built executable) and 2) that it is actually called passwords.txt and not miscalled passwords.txt.txt

    Your last code posted works, you have a problem with a misnamed file, a misplaced file, or file permissions.

  14. #14
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    I'm having trouble inputting the student ID. The way its supposed to work is that the ID is supposed to correlate with each index, meaning if each index is a student, then the ID is equivalent to the order in which each student is inputted. So index 0 is ID 0 and student index 85 is ID 85. I'm also wondering how would it be correct to print the info according to choice 4.

    Here is a sample of how things are supposed to work as given by my teacher:
    Press 1 for author info
    Press 2 to login
    2
    Enter user name
    Kenny
    Enter password
    Lofton
    Login Successful
    Press 0 to exit
    Press 1 for author info
    Press 2 to Enter new student
    Press 3 to enter grade for existing student
    Press 4 to print student records
    Press 5 to save student records
    Press 6 to load student records
    Press 7 to logout
    2
    Enter student first name
    tim
    Enter student last name
    kon
    Press 0 to exit
    Press 1 for author info
    Press 2 to Enter new student
    Press 3 to enter grade for existing student
    Press 4 to print student records
    Press 5 to save student records
    Press 6 to load student records
    Press 7 to logout
    3
    Enter student ID
    0
    Enter the numeric value of the grade point for this class
    t
    5.4
    Press 0 to exit
    Press 1 for author info
    Press 2 to Enter new student
    Press 3 to enter grade for existing student
    Press 4 to print student records
    Press 5 to save student records
    Press 6 to load student records
    Press 7 to logout
    3
    Enter student ID
    0
    Enter the numeric value of the grade point for this class
    t
    4.2
    Press 0 to exit
    Press 1 for author info
    Press 2 to Enter new student
    Press 3 to enter grade for existing student
    Press 4 to print student records
    Press 5 to save student records
    Press 6 to load student records
    Press 7 to logout
    4
    First Name: tim Last Name: kon: GPA: 4.800000
    5.400000
    4.200000
    Press 0 to exit
    Press 1 for author info
    Press 2 to Enter new student
    Press 3 to enter grade for existing student
    Press 4 to print student records
    Press 5 to save student records
    Press 6 to load student records
    Press 7 to logout

    Here is my code so far:



    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    typedef struct student
    {
    	char fname[100], lname[100];
    	double grades[100];
    	int ID;
    }student;
    
    
    int main()
    {
    	student arr[1000];
    
    
    	int j = 0;
    	int i = 0;
    	int a = 0;
    	char username[100];
    	char password[100];
    	char fusername[100], fpassword[100];
    
    
    
    
    	while (1)
    	{
    
    
    		printf("Press 1 for author info\nPress 2 to Log in\n");
    		scanf("%d", &a);
    
    
    		if (a == 1)
    		{
    			printf("Timofey Kononets\n2651763\n");
    		}
    
    
    		if (a == 2)
    		{
    			FILE *file = fopen("passwords1.txt", "r");
    			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);
    		}
    		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", &a);
    
    
    			if (a == 1)
    			{
    				printf("Timofey Kononets\n2651763\n");
    			}
    			else if (a == 2)
    			{
    				printf("Enter student first name\n");
    				scanf("%s", arr[i].fname);
    				printf("Enter student last name\n");
    				scanf("%s", arr[i].lname);
    				i++;
    			}
    			else if (a == 3)
    			{
    				printf("Enter student ID\n");
    				scanf("%d", arr[j].ID);
    				printf("Enter a numeric value for the gradepoint in this class\n");
    				scanf("%lf", arr[j].grades);
    			}
    			else if (a == 4)
    			{
    				for (int k = 0; k < i; k++)
    				{
    					printf("First name: %s   Last Name: %s   GPA: %lf\n", arr[k].fname, arr[k].lname, arr[k].grades);
    				}
    				}
    		}
    		
    		if (a == 0)
    		{
    			exit(0);
    		}
    
    
    
    
    
    
    	}
    	system("pause");
    	return (0);
    }

  15. #15
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    You should start by reorganising your code into logical units, functions, rather than having one huge monolithic main function. Solve each problem in its own function and use main to tie it all together.
    You are still not checking that fopen hasn't returned a null pointer. That is a must. Don't forget error checking.
    Clean up what you already have, then we'll go from there.
    while(2) is a little weird. It'll work fine as true is any non-zero number but while(1) is kind of a convention for an infinite loop. system("PAUSE") is just horrible. You can run your code in a debugger and stick a breakpoint on the return 0 line to hold the program open to see output. Get used to using your debugger, it will become a trusted friend over time.

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