Adding Students to a database

This is a discussion on Adding Students to a database within the C++ Programming forums, part of the General Programming Boards category; Im trying to add users to a student database. When i try to add it lets me enter 4 of ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Adding Students to a database

    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);
    }
    Last edited by lofty; 01-10-2005 at 01:17 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,529
    Well step 1 is read the forum anouncements and use [code][/code]Tags
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    sorry about that, fixed it

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I didn't have any problems adding students. However, when you save, be sure to save your count first since you're expecting this when you load the file.
    Code:
    void save_data(student_type cars[])
                                      // Save database to disk after each change
    {
    	FILE *out_file;
    	int i;
    
    	out_file = fopen("cars.dat", "wb");
    
    	// Write out the number of students
    
    	for (i = 0; i < NO_OF_STUDENTS && cars[i].phone > 0; i++)
    		fwrite(&cars[i], sizeof(student_type), 1, out_file);
    
    	fclose(out_file);
    }
    In addition, you probably want to change the void main to an int main, since you'll be bugged about that 'til kingdom come. Also, you may want to change this:
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    to this:
    Code:
    #include <iostream>
    #include <stdio.h>
    /*
    #include <conio.h> //nothing used from this one
    #include <ctype.h> //or this one
    #include <string.h> //or this one
    */
    
    using std::cout;
    using std::cin;
    <iostream.h> is a deprecated header file. <iostream> is the same thing except everything is in the std namespace instead of the global namespace.

    You could also consider using a struct to hold the count and your array, since you don't really need to compute the count.
    Code:
    struct student_db_type
    {
    	student_type cars[NO_OF_STUDENTS];
    	int count;
    };
    Load the count from the file, and then just increment the count when you add a record, and your count will be up-to-date.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 02:44 PM
  4. A Student's Mark Database
    By playstationman1 in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2006, 12:26 PM
  5. Adding records to a 2 dimensional database.
    By colinuk in forum C Programming
    Replies: 7
    Last Post: 01-16-2005, 06:48 PM

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