Im trying to add users to a student database.
When i try to add it lets me enter 4 of 5 items then crashes.
Any ideas?
Code below:
Code:#include <iostream.h> #include <conio.h> #include <ctype.h> #include <stdio.h> #include <string.h> #define NO_OF_STUDENTS 20 struct student_type { char reg[20]; char surname[25]; char firstname[25]; char phone; int credit; }; void initialise_data(student_type[]); void add_car(student_type[]); void show_all_cars(student_type[]); void save_data(student_type[]); void show_car(student_type[], int); void main() { student_type cars[NO_OF_STUDENTS]; char choice; // Load car file into database initialise_data(cars); // Display menu and get user choice do { cout << "\n\n" << "\nAdd a Student \t\t\tA" << "\nShow all Students \t\tS" << "\nFind a Student \t\t\tF" << "\n Quit \t\t\tQ" << "\n\nChoice:- "; cin >> choice; choice = toupper(choice); switch (choice) { case 'A': add_car(cars); break; case 'S': show_all_cars(cars); break; } } while (choice != 'Q'); } void initialise_data(student_type cars[]) // Loads car database from file { int i, count = 0; FILE *in_file; for (i = 0; i < NO_OF_STUDENTS; i++) cars[i].phone = 0; in_file = fopen("cars.dat", "rb"); if (in_file == NULL) cout << "Starting a new data file\n"; else // If the file did open - read the data // After this 'count' will contain the // number of car records read. count = fread(cars, sizeof(student_type), NO_OF_STUDENTS, in_file); fclose(in_file); cout << "\nNo of Students on file is " << count; } void add_car(student_type cars[]) // Create a new car record in the database { int next; // Count how many cars already in database for (next = 0; next < NO_OF_STUDENTS && cars[next].phone > 0; next++); if (next < NO_OF_STUDENTS) // If database is not full, add a car { cout << "\nRegistration Number: "; cin >> cars[next].reg; cout << "Surname: "; cin >> cars[next].surname; cout << "First Name: "; cin >> cars[next].firstname; cout << "Phone: "; cin >> cars[next].phone; cout << "Credits: "; cin >> cars[next].credit; } else // If database is full, give message cout << "No room for any more Students\a\n"; // Database has changed so save it save_data(cars); } void show_car(student_type cars[], int position) // Display any car knowing its array position { cout << "\n\nRegistration Number:- " << cars[position].reg; cout << "\nSurname:- " << cars[position].surname; cout << "\nFirst Name:- " << cars[position].firstname; cout << "\nPhone:- " << cars[position].phone; cout << "\nCredits:- " << cars[position].credit; } void show_all_cars(student_type cars[]) // Display all cars by calls to show_car() { for (int i = 0; i < NO_OF_STUDENTS && cars[i].phone > 0; i++) show_car(cars, i); } void save_data(student_type cars[]) // Save database to disk after each change { FILE *out_file; int i; out_file = fopen("cars.dat", "wb"); for (i = 0; i < NO_OF_STUDENTS && cars[i].phone > 0; i++) fwrite(&cars[i], sizeof(student_type), 1, out_file); fclose(out_file); }



LinkBack URL
About LinkBacks


