Problem with Adding Record to End of File

Ok i am having a problem with this, for some reason it will not Add the Record to the End of the File.

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
using namespace std;

struct Student
{
    int student_number;    //Student number
    string student_firstname; //Students first Name
    string student_lastname; //Students last Name
    string Address;         //Address of Student
    int Date_Registered; //Date Registered
};

struct Course
{
    string modules; //Student modules
    int marks; //Marks of modules 
};


int N_Module;
struct Course stArray1[6];
int N_STUDENT;
struct Student stArray[4];
int i=0;  
int j=0;
int a=0;
int b=0;

/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/

int main ()
{
    
    
    
    fstream myFile ("Student.text", ios::in | ios::out | ios::binary); 
    if (myFile.is_open())
        
    {
        
    N_STUDENT = -1;
    
    do
    { 	
        cout  << "Enter number of students less than 4" <<endl;
        cin >> N_STUDENT;
        cout << endl;
        
    }
    
    while(N_STUDENT > 4 || N_STUDENT<0);
    
    for(i; i < N_STUDENT; i++)
        
    { 
    cout << " Enter Student First Name: ";
    cin >> stArray[i].student_firstname;
    myFile << "STUDENT FIRST NAME:" << stArray[i].student_firstname;
    
    cout << " Enter Student Last Name: ";
    cin >> stArray[i].student_lastname;
    myFile << "STUDENT LAST NAME:" << stArray[i].student_lastname;
    
    cout << "Enter Student Address: ";
    cin >> stArray[i].Address;        
	myFile << "STUDENT Address:" << stArray[i].Address;
    N_Module = -1;
    
    do
    { 	cout  << "Enter number of Modules Taken less than 6" <<endl;
        
        cin >> N_Module;
        cout << endl;
    }
    
    while(N_Module > 6 || N_Module < 0);
    
    for (j=0; j < N_Module; j++)
    {
        cout << "Enter Module Name: ";
		cin >> stArray1[j].modules;
		myFile << "Module Name:" << stArray1[j].modules;

    
    cout << "Enter Module Marks: ";
    cin >> stArray1[j].marks; 
	myFile << "Module Marks:" << stArray1[j].marks;

		
    }
    j=0;
    }
    
   
   // myFile.write (reinterpret_cast <const char* > (& N_STUDENT), sizeof (Student));
    //myFile.write (reinterpret_cast <const char*> (& N_Module), sizeof (Course));
    myFile.close();
    }

    
    else cout << "Unable to open File";


    
                        
    bool Exit = false;
    
	while (!Exit)
	{
		int choice;
        
		cout << endl;
        cout << "=======================================" << endl;
        cout << "                                       " << endl;
        cout << " WELCOME TO STUDENT RECORD             " << endl;
		cout << "                                       " << endl;
        cout << "=======================================" << endl;
		cout << "Select Action: " << endl;
		cout << "1) Display Student Details" << endl;
		cout << "2) Add Additional Student to File" << endl;
		cout << "3) Read contents of File" << endl;
		cout << "4) Alter any Student Details" << endl;
		cout << "5) Add the Array to a Stack" << endl;
		cout << "6) Delete a Student Record" << endl;
                cout << "7) Display Students" << endl;
		cout << "8) Exit" <<endl;
		cout << "=======================================" << endl;
		cout << "Enter choice: ";
		cin >> choice;
		
	

                 // ADD STUDENT TO END OF FILE
                
         
     else if (choice == 2 )
     {
         
         
         fstream myFile("Student.text", ios::app );
          myFile.seekp(0, ios::end);
          
          if(!myFile.good())
          {
          for(a; a < N_STUDENT; a++)
    { 
              
    cout << " Enter Student First Name: ";
    cin >> stArray[a].student_firstname;
    myFile << "STUDENT FIRST NAME:" << stArray[a].student_firstname;
    
    cout << " Enter Student Last Name: ";
    cin >> stArray[a].student_lastname;
    myFile << "STUDENT LAST NAME:" << stArray[a].student_lastname;
    
    cout << "Enter Student Address: ";
    cin >> stArray[a].Address;        
	myFile << "STUDENT Address:" << stArray[a].Address;
    N_Module = -1;
    
    do
        
    { 	cout  << "Enter number of Modules Taken less than 6" <<endl;
        
        cin >> N_Module;
        cout << endl;
    }
    
    while(N_Module > 6 || N_Module < 0);
    
    for (b=0; b < N_Module; b++)
    {
        cout << "Enter Module Name: ";
		cin >> stArray1[b].modules;
		myFile << "Module Name:" << stArray1[b].modules;

    
    cout << "Enter Module Marks: ";
    cin >> stArray1[b].marks; 
	myFile << "Module Marks:" << stArray1[b].marks;

		
    }
    b=0;
    }
    
          myFile.close();
     }
          
     }        
                
                
                








your program doesn't compile - seems like you have many basic errors like not having your braces match w.r.t. if-else logic and while loops.

Where is the associated if statement for:

else if (choice == 2 )


on line 135

I tried copying and pasting your example into my ide, and tried reformatting code but gave up aftr realizing all the logicall mismatches.

Program Compiles.. Im running it as we speak,

It writes to file. But when i click 2 and want to add a Record to the End of File it does not

Write it to the end of the file


maybe if you added

}
return 0;
}


I only have copy in the Problem not the whole Program.
I've added what you suggested but still get a mismatch for the else if (choice == 2)

From inspecting code with my eye, I cannot find the associated if - statement for the [else if] statement on line 135.

Can you please point out the line number in your code listing above that is the associated [if] statement for the [else if] you have on line 135?

Please have a look at your code listing above and confirm that it is not a typo, ie (the [else if] statement on line 135).

This [else if] on line 135 also becomes more illegal when one considers that it falls within a while loop and clearly don't find the associated [if] statement to the [else if] on line 135, ie there is no [if] statement between line 107 (start of while loop) and line 135 (start of [else if]).

Herewith my version of your code with clearer indentation:

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
using namespace std;

struct Student
{
    int student_number;    //Student number
    string student_firstname; //Students first Name
    string student_lastname; //Students last Name
    string Address;         //Address of Student
    int Date_Registered; //Date Registered
};

struct Course
{
    string modules; //Student modules
    int marks; //Marks of modules 
};


int N_Module;
struct Course stArray1[6];
int N_STUDENT;
struct Student stArray[4];
int i=0;  
int j=0;
int a=0;
int b=0;

/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/

int main ()
{
   fstream myFile ("Student.text", ios::in | ios::out | ios::binary); 
   if (myFile.is_open())
   {
      N_STUDENT = -1;
    
      do
      { 	
         cout  << "Enter number of students less than 4" <<endl;
         cin >> N_STUDENT;
         cout << endl;
      }
      while(N_STUDENT > 4 || N_STUDENT<0);
    
      for(i; i < N_STUDENT; i++)
      { 
         cout << " Enter Student First Name: ";
         cin >> stArray[i].student_firstname;
         myFile << "STUDENT FIRST NAME:" << stArray[i].student_firstname;

         cout << " Enter Student Last Name: ";
         cin >> stArray[i].student_lastname;
         myFile << "STUDENT LAST NAME:" << stArray[i].student_lastname;

         cout << "Enter Student Address: ";
         cin >> stArray[i].Address;        
         myFile << "STUDENT Address:" << stArray[i].Address;
         N_Module = -1;

         do
         { 	
            cout  << "Enter number of Modules Taken less than 6" <<endl;
            cin >> N_Module;
            cout << endl;
         }
         while(N_Module > 6 || N_Module < 0);
    
         for (j=0; j < N_Module; j++)
         {
            cout << "Enter Module Name: ";
            cin >> stArray1[j].modules;
            myFile << "Module Name:" << stArray1[j].modules;

            cout << "Enter Module Marks: ";
            cin >> stArray1[j].marks; 
            myFile << "Module Marks:" << stArray1[j].marks;
         }

         j=0;
      }
    
   
      //myFile.write (reinterpret_cast <const char* > (& N_STUDENT), sizeof (Student));
      //myFile.write (reinterpret_cast <const char*> (& N_Module), sizeof (Course));
      myFile.close();
   }
   else  cout << "Unable to open File";

                  
   bool Exit = false;
    
	while (!Exit)
	{
		int choice;
        
		cout << endl;
      cout << "=======================================" << endl;
      cout << "                                       " << endl;
      cout << " WELCOME TO STUDENT RECORD             " << endl;
		cout << "                                       " << endl;
      cout << "=======================================" << endl;
		cout << "Select Action: " << endl;
		cout << "1) Display Student Details" << endl;
		cout << "2) Add Additional Student to File" << endl;
		cout << "3) Read contents of File" << endl;
		cout << "4) Alter any Student Details" << endl;
		cout << "5) Add the Array to a Stack" << endl;
		cout << "6) Delete a Student Record" << endl;
      cout << "7) Display Students" << endl;
		cout << "8) Exit" <<endl;
		cout << "=======================================" << endl;
		cout << "Enter choice: ";
		cin >> choice;
		
	

      // ADD STUDENT TO END OF FILE
                
         
      else if (choice == 2 )
      {
         fstream myFile("Student.text", ios::app );
         myFile.seekp(0, ios::end);

         if(!myFile.good())
         {
            for(a; a < N_STUDENT; a++)
            { 
               cout << " Enter Student First Name: ";
               cin >> stArray[a].student_firstname;
               myFile << "STUDENT FIRST NAME:" << stArray[a].student_firstname;

               cout << " Enter Student Last Name: ";
               cin >> stArray[a].student_lastname;
               myFile << "STUDENT LAST NAME:" << stArray[a].student_lastname;

               cout << "Enter Student Address: ";
               cin >> stArray[a].Address;        
               myFile << "STUDENT Address:" << stArray[a].Address;
               N_Module = -1;
   
               do
               {
                  cout  << "Enter number of Modules Taken less than 6" <<endl;

                  cin >> N_Module;
                  cout << endl;
               }
               while(N_Module > 6 || N_Module < 0);
    
               for (b=0; b < N_Module; b++)
               {
                  cout << "Enter Module Name: ";
                  cin >> stArray1[b].modules;
                  myFile << "Module Name:" << stArray1[b].modules;

                  cout << "Enter Module Marks: ";
                  cin >> stArray1[b].marks; 
                  myFile << "Module Marks:" << stArray1[b].marks;
               }
               b=0;
            }
    
            myFile.close();
         }
     }        
   }
   return 0;
}




Please excuse my indentation w.r.t. the while loop - I tried editing my post but had no luck.

The important thing tho - is that you can point me to the asso ciated [if] statement to the [else if] statement on line 135 in your listing.

Alternatively, can you mail me your .cpp file?
Topic archived. No new replies allowed.