Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I was wondering if anyone could help me with this C programming assignmen

ID: 3822276 • Letter: H

Question

Hello, I was wondering if anyone could help me with this C programming assignment. I have been having lot of trouble with it. I included a brief description, details about the assignment, and specific implementation requirements. Brief Description: This program will ask the user to enter details about a class of students, one student at a time. These details include first name, last name, age, and grade. The user should be able to add up to 10 students, which will be stored in an array of structs. Details: When your program first runs, it should print out print out your name, the course number (“CS102-01”), and the programming assignment name (“Programming Assignment #4”), each on a new line. Then, the program should skip a line (i.e., print a blank line), and print a brief description of what the program does. After the description, the program should ask the user how many students they wish to enter, up to 10. If a user enters a number less than 1 or greater than 10, the program should keep asking the user the number of students until the number is between 1 and 10. Once this information is known, the program should proceed to ask for each student’s first name, last name, age, and grade. Once all the student information has been entered by the user, the program should output all the student information to a file called “students.txt”. For each student entered, this file should contain 4 lines of information. The first line should be the first name. The second line should be the last name. The third line should be the age. The fourth line should be the grade. Also, add a feature to your program which reads in an existing “students.txt” and allows the user to add additional students to it. Your program should, before asking the user to input the number of students, ask the user if they wish to load an existing file. If the user says yes, your program should read in the existing file, and then ask the user how many additional students they wish to add (but in any case the user should not be allowed to have more than 10 students, including existing and new). If the user declines to load the existing “students.txt” file, your program should proceed as normal. Specific Implementation Requirements (THESE THINGS MUST BE PRESENT OR POINTS WILL BE DEDUCTED!): 1. An array of structs should be used to store the students in memory. The structs should each contain two character arrays (for the first and last name), one integer (for the age), and one float or double (for the grade). 2. The code which writes the data to the output file should not be in main. 3. A comment block should be present before each function, explaining what the function does. 4. Comments should be present within each function, explaining any possibly confusing code. 5. The program should work correctly. This includes, among other things: 1. Your program should properly compile and run without crashing. 2. Your program should properly store names, ages, and grades without any loss of data (e.g., if I enter a grade of 9.4, but your program only stores 9, you’ve lost data) 6. The code that reads the data from the input file should not be in main. Thanks in advance!

Explanation / Answer

#include <stdio.h>
#include <string.h>
#define APPEND 0
#define WRITE 1

struct student {
   char   first_name[50];
   char   last_name[50];
   int   age;
   char   grade[5];
};

void read_from_file();
void read_from_console();
void print_details(struct student student_array[], int size);
void write_to_file(char filename[], struct student student_array[], int size, int mode);

int main( ) {

   printf("Zubaeyr ");
   printf("CS102-01 ");
   printf("Programming Assignment #4 ");
   printf("Description : This program allows the user to store and view student details, to and from text files or console ");  
  
   char choice;
   printf("Would you like to load from a file? (Y/N) : ");
   scanf("%c", &choice);

   /* select based on the user choice */
   if (choice == 'Y')  
       read_from_file();
   else if (choice == 'N')
       read_from_console();      
   else
       printf("Wrong choice :( ");

   return 0;
}

/*
* 1. This method asks the user for number of students between 1 to 10
* and then takes in the data for each student from console.  
* 2. Writes the data of each student to the console and to "students.txt" file
*/
void read_from_console() {

   /* Get a valid number of students as input */
   int size = -1;
   while (size >10 || size<1) {
       printf("Enter the number of students (1-10): ");
       scanf("%d", &size);
       if (size > 10 || size < 1)
           printf("Value must be between 1-10 ");
   }
  
   /* Declaring an array of sturct student */
   struct student student_array[size];

   int i;
   for (i=0; i<size;i++) {
       printf(" Enter details of %dth student ", (i+1));      
       printf("Enter FName : ");
       scanf("%s", student_array[i].first_name);
       printf("Enter Lname : ");
       scanf("%s", student_array[i].last_name);
       printf("Enter Age   : ");
       scanf("%d", &student_array[i].age);
       printf("Enter Grade : ");
       scanf("%s", student_array[i].grade);
   }
  
   /* 1. Output on to the console
   * 2. Write to a file
   */
   print_details(student_array, size);
   write_to_file("students.txt", student_array, size, WRITE);
}


/*
* This method writes the given records to the given output
* file in either appending or writing mode
*
*/
void write_to_file(char filename[], struct student student_array[], int size, int mode) {
   FILE *fp;

   if (mode == APPEND)
       fp=fopen(filename, "a");
   else
       fp=fopen(filename, "w");

   if(fp == NULL)
       return;

   int i;  
   for (i=0; i<size;i++) {
       fprintf(fp, "%s ", student_array[i].first_name);
       fprintf(fp, "%s ", student_array[i].last_name);
       fprintf(fp, "%d ", student_array[i].age);
       fprintf(fp, "%s ", student_array[i].grade);
   }
   fclose(fp);
}

/*
* This method takes in an array of student details and prints them to the console.
*/
void print_details(struct student student_array[], int size) {
   printf(" ---------------OUTPUT STUDENT DETAILS------------------- ");
   int i;  
   for (i=0; i<size;i++) {
       printf(" Details of %dth student ", (i+1));      
       printf("FName : %s ", student_array[i].first_name);
       printf("LName : %s ", student_array[i].last_name);
       printf("Age   : %d ", student_array[i].age);
       printf("Grade : %s ", student_array[i].grade);
   }
}

/**
* 1. This method reads in the existing data from the "students.txt" file and also
* gets additional data from the user.
* 2. It then writes the details back to the file and also outputs to the console.      
*/
void read_from_file() {
   FILE *fp;
   fp=fopen("students.txt", "r");
   if (fp == NULL)
       return;
  
   int i=0, size = 0;
   struct student student_array[10];
  
   for (i=0; i<10; i++) {
       if (fscanf(fp, "%s", student_array[i].first_name)==EOF) {
           break;
       }
       fscanf(fp, "%s", student_array[i].last_name);
       fscanf(fp, "%d", &student_array[i].age);
       fscanf(fp, "%s", student_array[i].grade);
       size++;
   }
  
   printf(" Read in details of %d students ", size);

   /* Get a valid number of students as input */
   int new_size = -1;
   while ((new_size + size) >10 || (new_size+size)<1) {
       printf("Enter the number of students : ");
       scanf("%d", &new_size);
       if ((new_size + size) >10 || (new_size+size)<1)
           printf("Err : Total number of students are more than 10 ");
   }
     fclose(fp);

   for (i=size; i<(size+new_size);i++) {
       printf(" Enter details of %dth student ", (i+1));      
       printf("Enter FName : ");
       scanf("%s", student_array[i].first_name);
       printf("Enter Lname : ");
       scanf("%s", student_array[i].last_name);
       printf("Enter Age   : ");
       scanf("%d", &student_array[i].age);
       printf("Enter Grade : ");
       scanf("%s", student_array[i].grade);
   }
  
   /* 1. Output on to the console
   * 2. Write to a file
   */
   print_details(student_array, size+new_size);
   write_to_file("students.txt", student_array, (size+new_size), WRITE);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote