Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ help.

Options
  • 28-07-2011 9:55pm
    #1
    Closed Accounts Posts: 2,663 ✭✭✭


    ok, I found this little Assignment online, and to keep me going and up to shape on my Programming Skills. by the looks of things i lost it but any way.

    I trying to alter Details and add a Record to end of a File.

    here is my Code.
    #include <iostream>
    #include<string>
    #define N_STUDENT 10 //Max size of Students
    //#define STACK_SIZE  10 // Max size of the Stack
    
    //*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
            
    using namespace std;
    int x[N_STUDENT];
    struct  student
    {
        int student_number;              // Student number of student
        string studentname;             // Name of student
        string Address;                //Address of Student
        int date_registered;           // Date Registered
        string CourseName;            // Name of Course 
        int Marks;                     // Course Mark 
    };
    
    student record[N_STUDENT];
    //*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
            
    
    int main ()
    {
            
        for (n = 0; n < N_STUDENT;n++)
        {
               while((cout<<"Enter Student Number: ")&&(!(cin>>record[n].student_number)||record[n].student_number<0)){
                cout<<"Invalid Input! Please Enter Student Number "<<endl;
                cin.clear();
                cin.ignore(1000,'\n'); 
            }
            
            cout << "Enter Student  Name: ";
            cin >> record[n].studentname;
            
            cout << "Enter Student Address: ";
            cin >> record[n].Address;        
            
            while((cout<<"Enter Date: ")&&(!(cin>>record[n].date_registered)||record[n].date_registered<0)){
                cout<<"Invalid Input! Please Enter Date"<<endl;
                cin.clear();
                cin.ignore(1000,'\n'); 
            }
            
            cout << "Enter Course Name: ";
            cin >> record[n].CourseName;
            
                while((cout<<"Enter Marks: ")&&(!(cin>>record[n].date_registered)||record[n].date_registered<0)){
                cout<<"Invalid Input! Please Enter Marks"<<endl;
                cin.clear();
                cin.ignore(1000,'\n'); 
        }
    //*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
            
         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) Save All Records to Text File" << endl;
            cout << "2) Add Additional Students to File" << endl;
            cout << "3) Read and Display the Contents of File" << endl;
            cout << "4) Alter any Attributes of a Student Detail" << endl;
            cout << "5) Add the Array to a Stack" << endl;
            cout << "6) Display Studetns who have not been deleted" << endl;
            cout << "7) Exit" <<endl;
            cout << "=======================================" << endl;
            cout << "Enter choice: ";
            cin >> choice;
        
    //*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
                    
            
            if (choice == 7) // Quits Program..
                
                
            {
                cout<< "Exit Program" << endl;
                Exit = true; 
            }
            
            
    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/   
          else if (choice == 6) // Display Students who have not been deleted
          {
               
          }
            
    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/   
            else if (choice == 4) // Alter Attributes of a Student Detail 
            
            {
                 void updateDetails(fstream & file) //this is the update function
    {
                 
    S rec;
    long len; 
    int regNumb;
    char option;
    
    file.open("student.txt", ios::in|ios::out| ios::binary);
    
    len = sizeof(S);
    
    cout<<"Enter registration number of student to be update:"<<endl;
    cin>>regNumb;
    
    file.seekg (0,ios::beg); //0 bytes from beginning of file(ios::beg) 
    file.read(reinterpret_cast<char*>(&rec),len);
    
    while (!( file.eof())) 
    { 
    if (regNumb == rec.regisNumb)
    {
    cout<<"What kind of attribute do you want to update?"<<endl;
    
    menu (option);
    
    cout<<"A -Registration number"<<endl;
    cout<<"B -Name of the student"<<endl;
    cout<<"C -Student address"<<endl;
    cout<<"D -Date of registration"<<endl;
    cout<<"E -Name of the modules"<<endl;
    cout<<"F -Mark of modules"<<endl;
    cout<<"G -Exit"<<endl;
    
    switch(option)
    
    {
    case 'A' :
    {
    cout<<"Enter registration number:"<<endl;
    cin>>rec.regisNumb;
    break;
    }
    case 'B' :
    {
    cout <<"Enter name of student:"<<endl;
    cin.getline(rec.name, MAXNAME);
    break;
    }
    case 'C':
    {
    cout <<"Enter student address:"<<endl;
    cin.getline(rec.address,30);
    break;
    }
    case 'D':
    { 
    cout <<"Enter day of registration:"<<endl; 
    cin>>rec.datRegi.day;
    cout <<"Enter month of registration:"<<endl; 
    cin>>rec.datRegi.month;
    cout <<"Enter year of registration:"<<endl; 
    cin>>rec.datRegi.year;
    break;
    }
    case 'E':
    {
    cout <<"Enter name of modules:"<<endl;
    for(int j=0;j<rec.arraysize; j++) 
    {
    cin>>rec.modArray[j].moduleName;
    cout<<endl;
    }
    break;
    }
    case 'F':
    {
    cout <<"Enter module marks:"<<endl;
    for(int j=0;j<rec.arraysize; j++) 
    {
    cout<<rec.modArray[j].moduleMark<<endl;
    }
    break;
    }
    case 'G':
    {
    cout<<"Exit"<<endl;
    break;
    } 
    } 
    
    int pos = file.tellg();
    
    int offset = pos-len; //one record less in size
    
    file.seekp(offset, ios::beg); 
    
    file.write( reinterpret_cast<char *>( &rec),sizeof(S)); 
    
    return;
    }
    file.read( reinterpret_cast<char *>( &rec), sizeof(S));
    }
    cout<<"Student not found"<<endl;
    file.close();
    }
    } 
    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/   
          else if (choice == 5) // Add to Stack
          {
               
          }
            
    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/ 
    
    
          else if (choice == 3) // Read and Display the Contents of File
          {
               
          }
            
    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/   
          else if (choice == 2) // Add Additional Students to File
          {
                S rec;
    
                ofstream og;
    
                og.open(FILENAME,   ios:: out |ios::app|ios:: binary);
    
    
          for (n = 0; n < N_STUDENT;n++)
        {
               while((cout<<"Enter Student Number: ")&&(!(cin>>record[n].student_number)||record[n].student_number<0)){
                cout<<"Invalid Input! Please Enter Student Number "<<endl;
                cin.clear();
                cin.ignore(1000,'\n'); 
            }
            
            cout << "Enter Student  Name: ";
            cin >> record[n].studentname;
            
            cout << "Enter Student Address: ";
            cin >> record[n].Address;        
         
                 while((cout<<"Enter Date: ")&&(!(cin>>record[n].date_registered)||record[n].date_registered<0)){
                cout<<"Invalid Input! Please Enter Date"<<endl;
                cin.clear();
                cin.ignore(1000,'\n'); 
            }
            
            cout << "Enter Course Name: ";
            cin >> record[n].CourseName;
            
                while((cout<<"Enter Marks: ")&&(!(cin>>record[n].date_registered)||record[n].date_registered<0)){
                cout<<"Invalid Input! Please Enter Marks"<<endl;
                cin.clear();
                cin.ignore(1000,'\n'); 
    }
    
        og.write(reinterpret_cast<char *>( &rec), sizeof(STUREC)); 
    
        og.close();
    
    }
          }
            
    
    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/   
          else if (choice == 1) // Save All Records to Text File
          {
               
          }
            
    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
    

    It doesnt Stat on the Assignment, about Stacks, but to keep me up to things im going add a Binary Search, and a Stack into the Program to see how it all plans out. could some one help me on the Alter and Adding to end of file please.


Advertisement