c++ Student Records

I am working on this c++ code, and It is menu driven. You are supposed to add a student record, display it, search for an id number, exit the program, and delete a record. My program compiles and runs, but when I got to display the data it goes crazy, I can add a student record, but it does not save to the file rather crashes, and the student ID can not be found int he search. I have been working on this forever and cant seem to come to a solution.
My program as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Define pre-fompiled headers

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <stdio.h>

using namespace std;

int check = 0;   // Input validation statement.
// int final = 0;  // Input validation statement.
int limit = 99;
struct StudentRec  // Structure to define variables in the Student Records.
{
    string Name;                        // String data for the Name of studnet.
    string ID;                         // String name for the ID # of the student.
    double GPA;                       // Define GPA of students.
    bool compare(StudentRec valid)   // Validate data
    {
        if(Name == valid.Name && ID == valid.ID && GPA == valid.GPA) // If statment to validate data
            return true;                                            // If correct, return true
                return false;                                      // If incorrect, return false.
    }
};

void read_a_record(StudentRec students[100], int &limit)  // Void function for reading the .txt file of the student records.
{
    // Define string variables.
    StudentRec cow;
    int check = 0;
    string full, // Full name of the student
    lastnm,      // Last name of the student.
    firstnm,     // First name of student.
    id,          // ID number of student
    name;        // Name.
    double gpa;  // Define gpa.
    ifstream file; // Pre-pare opening of file.
    file.open("StudentRecords.txt"); // Open StudentRecords.txt
    if (!file) // Validation statement
    {
        cout << "Can't open input file " << endl; // Tell user that the file could not be read correctly.
        exit(1); // End program.
    }
    for(int x = 0;x <= check; x++)  // Make sure data is not displayed past amont of students.
    {
        file >> firstnm >> lastnm >> id >> gpa; // Call variables.
    }
    name = lastnm + ", " +firstnm;
    // Take variable from struct
    cow.Name = name;   // Name
    cow.ID = id;      // ID
    cow.GPA = gpa;   // GPA
    file.close();    // Close file
    check++;        // Add 1 to check
}

void print_a_record(StudentRec show) // Function to display data
{
    // Display data in orderly and readable fashion.  Left center data.
    cout
    << left << setw(15) << show.Name
    << setw(11) << show.ID
    << setw(7) << fixed << setprecision(2) <<  show.GPA << endl;
}
// Function to call Menu to display to user.

// Function to end program if user enters 5.
int end()
{
    return 0;
}
void addsrecord (StudentRec students[100], int &limit)
{
StudentRec apple;

string full; // Full name.
string lname; // Last name
string fname; // First name
string id; // ID
double gpa; // GPA

int location=limit+1;
if(location == 99)
{
    cout << "Error, array has been exceded!!" << endl;
    return;
}
limit ++;

cout << "Please enter the students last name.." << endl;
    cin >> lname;
cout << "Please enter the students first name.." << endl;
    cin >> fname;
    full = lname + ", " + fname;
    apple.Name = full;
cout << "Please enther the student Id number beginning with M" << endl;
    cin >> id;
    apple.ID = id;
cout << "Please enter the students GPA" << endl;
    cin >> gpa;
    apple.GPA = gpa;
students[location]= apple;

}
void disall (StudentRec students[100], int limit)
{

    int x;                                // Define variable
                               
    
    // Display all definition of data. Left center all data and display variables in
    cout << left << setw(5) << "No." << left << setw(15) << "Name" << setw(11) << "ID" << setw(7) << "GPA" << endl;
    cout << "--------------------------------------------------------------------\n";
    for(x = 0; x <= limit; x++)          // For statement to display 1 through 10, but is x has to be limited by number of studnets.
    {
        cout << x+1 << ".   ";          // Display 1 through 10 with a ".", with for statment parameters, will only diaplay till 7.
        print_a_record(students[x]);   // Call print_a_function to display all data read from file.
    }

}
void delsrecord ()
{
}

void findsrecord(StudentRec students[100], int limit)
{
    string input;
    cout << "Enter the ID number of the student you wish to find (M########) " << endl;
    cin >> input;
    // loop to compare strings for the search
    int count;
    for (count=0; count <= limit; count++)
    {
        if(students[count].ID == input)
        {
        cout << "Student has been found." << endl;
        cout << "Name: " << students[count].Name << endl << "GPA: " << students[count].GPA << endl <<
        "ID: " << students[count].ID << endl;
        return;
        }
        cout << count << limit << endl;
        if (count==limit)
        {
            cout << "Can't find student!" << endl;
        }
        else
        {
            continue;
        }
    }
}
void save (StudentRec students [], int limit)
{
    ofstream file;
    file.open("StudentRecords.txt");
    string temper; // A convertName for students[p].Name
    for(int p =0; p <=limit; p++)
    {
        file << students[p].Name<< " " << students[p].ID << " " << students[p].GPA << endl;
    }
    file.close ();
}
// Void statement to display menu to user.
void displayMenu(StudentRec students [100])
{
    cout << "Student Records / Shawn Razek / Copyright 2010" << endl;
    cout << endl;
    cout << "Please Choose:" << endl;
    cout << "(1) Add a student record" << endl;
    cout << "(2) Delete a student record" << endl;
    cout << "(3) Find a studnet's information" << endl;
    cout << "(4) Display all information in the database" << endl;
    cout << "(5) Exit Program" << endl;
    int input;
    cin >> input;

    // Input validation.
    while (input < 1 || input > 5)
    {
        cout << "You did not enter a correct choice!!" << endl;
            cin >> input;
    }
    // Switch statement to call function user has chosen.
    switch(input)
    {
        case 1: addsrecord(students,limit);
            save(students,limit);
        break;
        case 2: delsrecord;
        break;
        case 3: findsrecord(students,limit);
        break;
        case 4: disall(students, limit);
        break;
        case 5: end ();
        break;
        default: cout << "You did not enter a correct choice!!" << endl;
        break;
    }
}


int main()  // Open up program
{
char loop;
StudentRec students[100];
read_a_record(students, limit);
do
{
    displayMenu(students);
    cout << "To continue, Please press Y, otherwise any key to quit" << endl;
    cin >> loop;
}
while(loop =='Y' || loop =='y');
{
return 0;
}

return 0;
}



StudentRecords.txt file as follows...

Jone Adams M123456 3.65
Mike Smith M452355 2.65
Mary Ann M456712 4.00
Allen Brown M123456 3.65
Mike Davis M452355 2.65
Mary Wilson M456712 4.00
Allen Harris M123456 3.65



Topic archived. No new replies allowed.