reading and writing a file

i ask the user to input his/her info and that writes to a file then on the choice 2 its going to show what should be stored in the file but it gives nothing close to what i need.

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
#include <iostream>
#include <fstream>


using namespace std;

 void addRecord(fstream &);
 void displayRecord(fstream &);
 void editRecord(fstream &);
 void deleteRecord(fstream &);  
// declare a structure for the record 
// array sizes 

const int NAME_SIZE =50, STUDENTID_SIZE = 50; 

struct info
{
       char name[NAME_SIZE];
       char studentID[STUDENTID_SIZE];
       int age;
       double GPA;
};

int main() 
{
    info person; // to hold info about person
    char again; // to hold y or n 
    int choice; 
    fstream people;

cout <<"Student Database Management Options\n";
cout << "---------------------------------------"<< endl;
cout << "(1)Add Student Record\n";
cout << "(2)Display Student Record\n";
cout << "(3)Edit Student Record\n";
cout << "(4)Delete Student Record \n";
cout << "---------------------------------------"<<endl;
cout << "Select Your Choice: "<< endl;;
cin >> choice;

while (choice < 1 || choice > 5)
{
      cout << "Please Re-Enter your choice (1)(2)(3)(4)" << endl;
      cin >> choice;
}
          

 void addRecord(fstream & );
{
     if ( choice ==1)
     {
          fstream people("people.txt", ios::out | ios::binary);
           //open file for binary output
          
          cout << "Enter the following information needed" << endl;
          cout << "Name:"<< endl;
          cin >> person.name;
          cout << endl;
          cout << "Age" << endl;
          cin >> person.age;
          cout << endl;
          cout << "GPA" << endl;
          cin >> person.GPA;
          cout << endl;
          cout << "Student ID" << endl;
          cin >> person.studentID;
          // write the contents to the file
          people.write(reinterpret_cast<char*>(&person),sizeof(person));
          } 
}
void displayRecord(fstream &);
{
     if (choice == 2)
     {
                people.open("peopel.txt", ios::in | ios::binary);
                
                
                cout << endl;
                cout << "Here is the file on Record:"<<endl;
                people.read(reinterpret_cast<char *>(&person),sizeof(person));
                
                
                if (!people.eof())
                {
                      cout << "Name:"<< endl;
                      cout << person.name << endl;
                      cout << endl;
                      cout << "Age" << endl;
                      cout << person.age << endl;
                      cout << endl;
                      cout << "GPA" << endl;
                      cout << person.GPA << endl;
                      cout << endl;
                      cout << "Student ID" << endl;
                      cout << person.studentID << endl;
                }
     cout << endl;
     cout << "Thats all the data in the file!\n";
     people.close();
     
     }
}

        






    system("PAUSE");
    return 0;
} 
Not a bad program.... Lacking comments.

Change
1
2
// while (choice < 1 || choice > 5)
while (choice < 1 || choice > 4)


and change
1
2
//                people.open("peopel.txt", ios::in | ios::binary);
                people.open("people.txt", ios::in | ios::binary);



I would also give your int's and doubles values, but that's just to keep you from debugging something silly later.

1
2
       int age=0;
       double GPA=0;
Last edited on
Topic archived. No new replies allowed.