Structures Help

Hello, I have this code where I have finished and have tried reopening the project and running it again before I submit it for college. When I run the code in Code Blocks it complies and displays the menu prompt. I was able to go through the options on the menu perfectly fine. But for some reason when I type option 4 now it is not displaying the text from the text file anymore. I tried using debugging mode and it is saying cannot open file and gives a file name from the compiler folder. Is there a problem with my code or is just CodeBlocks malfunctioning.

Here is the information from the text file within the two lines.
__________________________________________
Jone,Adams M123456 3.65
Mike,Smith M452355 2.65
Mary,Ann M456712 4
Allen,Brown M123456 3.65
Mike,Davis M452355 2.65
Mary,Wilson M456712 4
Allen,Harris M123456 3.65
_________________________________________

Thank you for taking a look at this problem too.


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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include<fstream>
#include<iomanip>
#include<iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;

ifstream myfile;//Open file
ofstream myfile2;//File to send data too
int recordArray = 0;//This will track the number of records

struct StudentRec//Define structure for student recs
{
    string Name;//holds name of the student
    string ID;//holds the id
    double GPA;//holds the gpa
};

const int ARRAY_SIZE =100;//Define the size of student records
StudentRec Records[ARRAY_SIZE]; //the number of student records

//boolean function that returns true or false representing that a record was read
bool read_a_record (StudentRec& Records)
{
    string firstName, lastName;//these two will hold the first and last name
    string ID;
    double GPA;

    myfile.open ("StudentRecords.txt");//open file
    //only activates if the file opens
    if(myfile.is_open())
    {
        bool recordWasRead = myfile>>firstName>>lastName>>ID>>GPA;//read the file
        if(!recordWasRead)
        {
            return false;
        }
        Records.Name = firstName+","+lastName;//add the two names to make a whole name
        Records.ID = ID;
        Records.GPA = GPA;
        cout<<Records.Name<<"\t"<<Records.ID<<"\t"<<Records.GPA<<"\n";
        return true;
    }

    else
    {
        cout<<"File could not be opened.";
        return false;
    }
}

//loads the file for first use in the program
void LoadDatabase()
{
    myfile.open ("StudentRecords.txt");//open file
    int i=0;//will be the counter
    if(myfile.is_open())//activates if it is open
    {
        while(read_a_record(Records[i]))//while they can read
        {
            i++;//increment counter
            recordArray++;//increament global counter
        }
    }
    else
        cout<<"Unable to open file.";
    myfile.close();//close file
}

//function to save the new database
void saveDataBase()
{
    myfile2.open ("StudentRecords.txt");// open file
    int i = 0;
    if(myfile2.is_open())
    {
        while(i<ARRAY_SIZE)//while the i is less than the number of records
        {
            if(i<ARRAY_SIZE)
            {
                if(read_a_record(Records[i]))
                {
                    //sends the name, id, and gpa aligned correctly
                    myfile2<<left<<setw(18)<<Records[i].Name;//saves the name to the file
                    myfile2<<left<<setw(10)<<Records[i].ID;//saves the id to the file
                    myfile2<<left<<setw(10)<<Records[i].GPA<<endl;//saves the gpa to the file
                }
            }
            i++;
        }
    }
    myfile.close();
}

//function to get the record wanting to be inserted
void Insert_Record(StudentRec& Records)
{
    myfile.open ("StudentRecords.txt");//open file
    if(myfile.is_open())//activates if open
    {
        string fullName;//will hold the full name
        string studentID;//will hold the id
        double studentGPA;//will hold the gpa

        //gets the user input of variables above
        cout<<"\nWhat is the student's full name? ";
        getline(cin,fullName);
        getline(cin,fullName);
        cout<<"\nWhat is the student's ID? ";
        getline(cin,studentID);
        cout<<"\nWhat is the student's GPA? ";
        cin>>studentGPA;
        cout<<endl;

        //will go if the global is less then the size of the records
        if (recordArray<ARRAY_SIZE)
        {
            Records.Name = fullName;//assign full name
            Records.ID = studentID;//assign id
            Records.GPA = studentGPA;//assign gpa
            recordArray++;
        }
        else
            cout<<"The records are full.";
    }
    else
    {
        cout<<"File cannot be opened.";
    }
    myfile.close();
}

//will be implemented later
void Delete_Record()
{

}

//function will search through records
void Find_Record()
{
    int i;//counter
    string findID;//id that will found

    //get user input for findID
    cout<<"ID Search: ";
    getline(cin,findID);
    getline(cin,findID);

    //loads through the records for id
    for(i=0;i<ARRAY_SIZE;i++)
    {
        if(Records[i].ID == findID)
        {
            //display student id
            cout<<"\n";
            cout<<"The student has been found.\n";
            cout<<"Name: "<<Records[i].Name<<"\n";
            cout<<"ID: "<<Records[i].ID<<"\n";
            cout<<"GPA: "<<Records[i].GPA<<"\n";
            cout<<"\n";
            break;//stop after found
        }
    }
    cout<<"RECORD NOT FOUND.\n";
}

//prints a student record when called
void Print_Records(StudentRec Records)
{
    //prints the name, id, and gpa aligned correctly
    cout<<left<<setw(18)<<Records.Name;
    cout<<left<<setw(10)<<Records.ID;
    cout<<left<<setw(10)<<Records.GPA<<endl;
}

//display menu for the user
void displayMenu()
{
    cout<<"Lab5B_AlvaWebster/Alva Webster/DONT YOU COPY THIS"<<endl<<endl;
    cout<<"Please choice: "<<endl;
    cout<<"(1) Add a student record"<<endl;
    cout<<"(2) Delete a student record"<<endl;
    cout<<"(3) Find a student's infomation"<<endl;
    cout<<"(4) Display all information in the database"<<endl;
    cout<<"(5) Exit Program"<<endl<<endl;
    cout<<"CHOICE: ";
}

//finally the main function to call all functions
int main()
{
    LoadDatabase();//load the file into the records
    int choice;//assign the choice variable for the menu
    do//do while statement that stops when the user chooses five
    {
        displayMenu();//call menu function to display inital menu
        cin>>choice;//get user input for choice

        //calls certain function depending on users choice
        switch(choice)
        {
            //case 1, the user inserts a new record
            case 1:
                Insert_Record(Records[recordArray]);
                break;

            //case 2, will delete a record, implement later
            case 2:
                Delete_Record();
                break;

            //case 3, find a record based off an id
            case 3:
                Find_Record();
                break;

            //case 4, display the records of a file.
            case 4:
                //Header for display
                cout<<"No.      Name              ID        GPA\n";
                cout<<"----------------------------------------\n";

                myfile.open ("StudentRecords.txt");//open file
                int rec_num=0;//keeps track oft the rec number
                int i=0;//counter to loop through

                while(i<ARRAY_SIZE)//while the i is less than the number of records
                {
                    if(i<ARRAY_SIZE)
                    {
                        cout<<++rec_num<<"\t";//displays number of record
                        Print_Records(Records[i]);//print the record
                    }
                    else
                        break;
                    i++;
                }
                break;
                myfile.close();
        }
    }while(choice!=5);//if the choice is equal to five stop program
    saveDataBase();
    cout<<"Good Bye!\n";
    return 0;
}
Your main problem is that you are opening an already opened filestream in multiple places. That will set failbit and all subsequent input operations will fail (that means your read_a_reacor function will always return false). That mistake was made mainly because of use of global variables. Open file in main() and pass a reference to already opened stream to your functions. Also your Record parameters in functions shadows global Record variable. Either rename something or move global Record in some local space (main is a good candidate).
I changed the ifstream problem but the code still isn't display the records. I think its a problem with the load record function and the read a record function. Any new suggestions?
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include<fstream>
#include<iomanip>
#include<iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;

struct StudentRec//Define structure for student recs
{
    string Name;//holds name of the student
    string ID;//holds the id
    double GPA;//holds the gpa
};


int recordArray = 0;//This will track the number of records
const int ARRAY_SIZE =100;//Define the size of student records
StudentRec Records[ARRAY_SIZE]; //the number of student records

//boolean function that returns true or false representing that a record was read
bool read_a_record (StudentRec& Records, ifstream& myfile)
{
    string firstName, lastName;//these two will hold the first and last name
    string readstudentID;
    double readstudentGPA;
    //only activates if the file opens
    if(myfile.is_open())
    {
        bool recordWasRead = myfile>>firstName>>lastName>>readstudentID>>readstudentGPA;//read the file
        if(recordWasRead)
        {
            Records.Name = firstName+","+lastName;//add the two names to make a whole name
            Records.ID = readstudentID;
            Records.GPA = readstudentGPA;
            cout<<Records.Name<<"\t"<<Records.ID<<"\t"<<Records.GPA<<"\n";
            return true;
        }
        return false;
    }

    else
    {
        cout<<"File could not be opened.";
        return false;
    }
}

//loads the file for first use in the program
void LoadDatabase(ifstream& myfile)
{
    int i=0;//will be the counter
    if(myfile.is_open())//activates if it is open
    {
        while(read_a_record(Records[i],myfile))//while they can read
        {
            i++;//increment counter
            recordArray++;//increament global counter
        }
    }
    else
        cout<<"Unable to open file.";
}

//function to save the new database
void saveDataBase(ifstream& myfile)
{
    ofstream myfile2;//File to send data too
    myfile2.open("StudentRecords2.txt");
    int i = 0;
    if(myfile2.is_open())
    {
        while(i<ARRAY_SIZE)//while the i is less than the number of records
        {
            if(i<ARRAY_SIZE)
            {
                if(read_a_record(Records[i],myfile))
                {
                    //sends the name, id, and gpa aligned correctly
                    myfile2<<left<<setw(18)<<Records[i].Name;//saves the name to the file
                    myfile2<<left<<setw(10)<<Records[i].ID;//saves the id to the file
                    myfile2<<left<<setw(10)<<Records[i].GPA<<endl;//saves the gpa to the file
                }
            }
            i++;
        }
    }
}

//function to get the record wanting to be inserted
void Insert_Record(StudentRec& Records, ifstream& myfile)
{
    if(myfile.is_open())//activates if open
    {
        string fullName;//will hold the full name
        string studentID;//will hold the id
        double studentGPA;//will hold the gpa

        //gets the user input of variables above
        cout<<"\nWhat is the student's full name? ";
        getline(cin,fullName);
        getline(cin,fullName);
        cout<<"\nWhat is the student's ID? ";
        getline(cin,studentID);
        cout<<"\nWhat is the student's GPA? ";
        cin>>studentGPA;
        cout<<endl;

        //will go if the global is less then the size of the records
        if (recordArray<ARRAY_SIZE)
        {
            Records.Name = fullName;//assign full name
            Records.ID = studentID;//assign id
            Records.GPA = studentGPA;//assign gpa
            recordArray++;
        }
        else
            cout<<"The records are full.";
    }
    else
    {
        cout<<"File cannot be opened.";
    }
}

//will be implemented later
void Delete_Record()
{

}

//function will search through records
void Find_Record()
{
    int i;//counter
    string findID;//id that will found

    //get user input for findID
    cout<<"ID Search: ";
    getline(cin,findID);
    getline(cin,findID);
    bool found = false;
    //loads through the records for id
    for(i=0;i<ARRAY_SIZE;i++)
    {
        if(Records[i].ID == findID)
        {
            //display student id
            cout<<"\n";
            cout<<"The student has been found.\n";
            cout<<"Name: "<<Records[i].Name<<"\n";
            cout<<"ID: "<<Records[i].ID<<"\n";
            cout<<"GPA: "<<Records[i].GPA<<"\n";
            cout<<"\n";
            found = true;
            break;//stop after found
        }
    }

    if(found == false)
        cout<<"No Record Found."<<"\n";
}

//prints a student record when called
void Print_Records(StudentRec Records)
{
    //prints the name, id, and gpa aligned correctly
    cout<<left<<setw(18)<<Records.Name;
    cout<<left<<setw(10)<<Records.ID;
    cout<<left<<setw(10)<<Records.GPA<<endl;
}

//display menu for the user
void displayMenu()
{
    cout<<"Lab5B_AlvaWebster/Alva Webster/DONT YOU COPY THIS"<<endl<<endl;
    cout<<"Please choice: "<<endl;
    cout<<"(1) Add a student record"<<endl;
    cout<<"(2) Delete a student record"<<endl;
    cout<<"(3) Find a student's infomation"<<endl;
    cout<<"(4) Display all information in the database"<<endl;
    cout<<"(5) Exit Program"<<endl<<endl;
    cout<<"CHOICE: ";
}

//finally the main function to call all functions
int main()
{
    ifstream myfile;
    myfile.open("StudentRecords.txt");

    LoadDatabase(myfile);//load the file into the records
    int choice;//assign the choice variable for the menu
    do//do while statement that stops when the user chooses five
    {
        displayMenu();//call menu function to display inital menu
        cin>>choice;//get user input for choice

        //calls certain function depending on users choice
        switch(choice)
        {
            //case 1, the user inserts a new record
            case 1:
                Insert_Record(Records[recordArray],myfile);
                break;

            //case 2, will delete a record, implement later
            case 2:
                Delete_Record();
                break;

            //case 3, find a record based off an id
            case 3:
                Find_Record();
                break;

            //case 4, display the records of a file.
            case 4:
            {

                //Header for display
                cout<<"No.      Name              ID        GPA\n";
                cout<<"----------------------------------------\n";

                int rec_num=0;//keeps track oft the rec number
                int i=0;//counter to loop through
                while(i<ARRAY_SIZE)//while the i is less than the number of records
                {
                    if(i<ARRAY_SIZE)
                    {
                        cout<<++rec_num<<"\t";//displays number of record
                        Print_Records(Records[i]);//print the record
                    }
                    else
                        break;
                    i++;
                }
                break;
            }
            case 5:
                break;
            default:
                cout<<"Please enter a valid choice.\n\n";
        }
    }while(choice!=5);//if the choice is equal to five stop program
    saveDataBase(myfile);
    myfile.close();
    cout<<"Good Bye!\n";
    return 0;
}
It works for me:
How to reproduce problem you have with your code?
Lab5B_AlvaWebster/Alva Webster/DONT YOU COPY THIS

Please choice:
(1) Add a student record
(2) Delete a student record
(3) Find a student's infomation
(4) Display all information in the database
(5) Exit Program

CHOICE: 1

What is the student's full name? wa

What is the student's ID? ew

What is the student's GPA? 22

Lab5B_AlvaWebster/Alva Webster/DONT YOU COPY THIS

Please choice:
(1) Add a student record
(2) Delete a student record
(3) Find a student's infomation
(4) Display all information in the database
(5) Exit Program

CHOICE: 1

What is the student's full name? ASd

What is the student's ID? 234

What is the student's GPA? 44

Lab5B_AlvaWebster/Alva Webster/DONT YOU COPY THIS

Please choice:
(1) Add a student record
(2) Delete a student record
(3) Find a student's infomation
(4) Display all information in the database
(5) Exit Program

CHOICE: 4
No.      Name              ID        GPA
----------------------------------------
1       wa                ew        22
2       ASd               234       44
3                                   0
4                                   0
5                                   0
6                                   0
7                                   0
8                                   0
9                                   0
10                                  0
11                                  0
12                                  0
13                                  0
14                                  0
15                                  0
16                                  0
17                                  0
18                                  0
19                                  0
20                                  0
21                                  0
22                                  0
23                                  0
24                                  0
25                                  0
26                                  0
27                                  0
28                                  0
29                                  0
30                                  0
31                                  0
32                                  0
33                                  0
34                                  0
35                                  0
36                                  0
37                                  0
38                                  0
39                                  0
40                                  0
41                                  0
42                                  0
43                                  0
44                                  0
45                                  0
46                                  0
47                                  0
48                                  0
49                                  0
50                                  0
51                                  0
52                                  0
53                                  0
54                                  0
55                                  0
56                                  0
57                                  0
58                                  0
59                                  0
60                                  0
61                                  0
62                                  0
63                                  0
64                                  0
65                                  0
66                                  0
67                                  0
68                                  0
69                                  0
70                                  0
71                                  0
72                                  0
73                                  0
74                                  0
75                                  0
76                                  0
77                                  0
78                                  0
79                                  0
80                                  0
81                                  0
82                                  0
83                                  0
84                                  0
85                                  0
86                                  0
87                                  0
88                                  0
89                                  0
90                                  0
91                                  0
92                                  0
93                                  0
94                                  0
95                                  0
96                                  0
97                                  0
98                                  0
99                                  0
100                                 0
Lab5B_AlvaWebster/Alva Webster/DONT YOU COPY THIS

Please choice:
(1) Add a student record
(2) Delete a student record
(3) Find a student's infomation
(4) Display all information in the database
(5) Exit Program

CHOICE: 5
Good Bye!
Yah the first three function work correctly and the display records works but the problem is the display records function needs to display all the records in the text file that it reads. The text file is already populated with information on students.
__________________________________________
Jone,Adams M123456 3.65
Mike,Smith M452355 2.65
Mary,Ann M456712 4
Allen,Brown M123456 3.65
Mike,Davis M452355 2.65
Mary,Wilson M456712 4
Allen,Harris M123456 3.65
_________________________________________

The display records needs to display these and the ones the user adds. then send that data to a file.
Notice that your file is opened only for reading, not for writing.
You can use std::fstream myfile("StudentRecords.txt", std::ios::in | std::ios::out); to open it in both modes. Note that you need to use myfile.seekp(0, std::ios::end); to set output cursor to the end of file befor you will write anything to the end, and myfile.seekg(0, std::ios::beg); to set input cursor tothe beginning before you start reading from file.
But if the file was only for reading would it still have the problem of displaying all of the records within the file.
Problem with your input file:
After myfile>>firstName>>lastName>>readstudentID>>readstudentGPA; your variable will be:
1
2
3
4
firstName = Jone,Adams
lastName = M123456 
readstudentID = 3.65 
readstudentGPA = Mike,Smith //Whoops it is clearly not a double, failing... 

Operator>> reads space-delimited values
Also you read your values until fail, but you never clear failbit. That means that once it set, all other operations will be counted as failed too.
You are the best person I have talked to about this!!! Thank you so much. That is such a simple fix! I can now move on with my life in college. Thank you so much for all your time and answering my questions. I am so glad there are people like you on this website. Thank you for consistently answering my questions. I wish I could shake your hand
Topic archived. No new replies allowed.