Ok i have a problem which i cant seem to figure out why it is not woriking,

First off When i do Enter in some Details and want to enter Records to the end of File it give me the choice of enter in the Details.

When display the Array its only showing me the the Details i add to the End of the File.

And Also its not Add the Record to the end of the file

#include<iostream>
#include<string>
#include<sstream> 
#include<fstream> 

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, j;
struct Course stArray1[6];
int N_STUDENT, i=0;    
struct Student stArray[4];

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

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.close();
    }

    
    else cout << "Unable to open File";


/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
                
                        //MENU
    
                        
    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 Student to End of File" << endl;
		cout << "3) Sort Using Merge Sort" << endl;
		cout << "4) Search Sorted Array Using Binary Search" << endl;
		cout << "5) Add the Array to a Stack" << endl;
		cout << "6) Display the Array" << endl;
		cout << "7) Exit" <<endl;
		cout << "=======================================" << endl;
		cout << "Enter choice: ";
		cin >> choice;
		
        
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/

              
                //DISPLAY THE ARRAY 
                
                
     if (choice == 1) 
            
        {
           
            for(int i=0; i < N_STUDENT; i++)
               {
             
                   cout << "Student  First Name:" << " " << stArray[i].student_firstname << endl;         //Read out Student Name
                   cout << "Student Second Name:" << " "  << stArray[i].student_lastname << endl;           //Read out Course Address
                   cout << "STUDENT Address: " << "  " <<  stArray[i].Address << endl;            //Read out Course Code
                   
               }
           
         cout << "    " << endl; 
            for (int j =0; j < N_Module; j++)
    {
        cout << "Module Name:" << stArray1[j].modules << endl;
	cout << "Enter Module Marks:" << stArray1[j].marks << endl;
        cout << " " << endl;
        
    }
            
    j=0;
    
    }
            
        
     
        
        
        
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
                 // ADD STUDENT TO END OF FILE
                
         
     else if (choice == 2 )
     {
         //int a;
         //int b;
         
         fstream myFile("Student.text", ios::app );
          myFile.seekp(ios::end);
          
          if(!myFile.good())
          {
          for(i =0; 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; 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.close();
     }
     }

Recommended Answers

All 2 Replies

I cannot see the problem, but then again it is hard to read your code because you seem to have ignored modulation. In case you do not know, modulation is the practice of chunking your code together into separate functions so that they are easier to read. For example you could replace your code for showing the main menu with a showMenu() function. And the code that implements each menu option should also be a function. Your main should look more like this:

int main()
{
    initialize();//this is the section of your code up until the menu loop
    int menuChoice=showMenu();
    while (menuChoice>EXIT_CONDITION)//EXIT_CONDITION will be a macro, defined as 7 in your code
    {
        switch (menuChoice)
        {
            case DISPLAY_CONDITION://again DISPLAY_CONDITION is a macro
            displayRecords();
            break;
            //...
            default:
        }
    }
    return cleanUp();//maybe you need to do something to wrap up the program?
}

Writing the initialize() function and the showMenu() function etcetera is your job. This is just a general idea of proper programming style.

commented: That's modulARation. Modulation is changing the frequency of something. +17

I have Return 0; at the end of the Program the Program is all Working Fine, but for some reason its not Add the New Student to end of the File.

And its only showing the Student who i add to end of the file not the whole Array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.