10 Replies - 14878 Views - Last Post: 03 November 2011 - 08:59 PM Rate Topic: -----

#1 C++ newb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-September 11

Deleting struct from array of structs problem!

Posted 02 November 2011 - 04:36 PM

Hi guys, I thought this code would work, but it still doesn't! The search section works fine, it's just when it doesn't find a match it just stops! What I'd like is that if a match to a student ID is not found, the user has the option to add new student information to the record. Any ideas? Relevant code:
    case 6://add a student record
        {
        int first = 0;
        int last = NUMRECS;
        int mid = (first + last)/2;
        int studentCount, newStdCourseCode, newStdCreditPoint;
        studentCount = NUMRECS;
        char decision3, option2;
        string newStdName;
        do
        {
        cout << "Please type in the student ID number\nof the student you want to input,\nthen press enter.\n";
        cin >> stdIDInput;
        bool found = false;
        while (first <= last && !found)//search for a student ID match
        {
            if (stdRec[mid].studentID == stdIDInput)
            {
                found = true;
                cout << "This student ID already exists in the system.\n";
                cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
                cout << setiosflags(ios::left);
                cout << setw(12) << stdRec[mid].studentID
                     << setw(21) << stdRec[mid].studentName
                     << setw(13) << stdRec[mid].courseCode
                     << setw(5)  << stdRec[mid].creditPoint << endl;
            }
            else if (stdRec[mid].studentID > stdIDInput)
            {
                last = mid -1;
            }
            else
            {
                first = mid +1;
            }
        }
        if(!found)//***HERE I don't know why this doesn't work! Any suggestions???
        {
            cout << "Would you like to add the student to the records? Y/N\n";
            cin >> decision3;
            if(decision3 == 'Y')
            {
                last = NUMRECS;
                studentCount++;
                stdRec[last+1].studentID = stdIDInput;
                cout << "Please input the new student's name.\n";
                cin >> newStdName;
                stdRec[last+1].studentName = newStdName;
                cout << "Please input the new student's course code.\n";
                cin >> newStdCourseCode;
                stdRec[last+1].courseCode = newStdCourseCode;
                cout << "Please input the new student's credit points.\n";
                cin >> newStdCreditPoint;
                stdRec[last+1].creditPoint = newStdCreditPoint;
                cout << "The student's information has now been added to the records.\n";
                cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
                cout << setiosflags(ios::left);
                cout << setw(12) << stdRec[last+1].studentID
                     << setw(21) << stdRec[last+1].studentName
                     << setw(13) << stdRec[last+1].courseCode
                     << setw(5)  << stdRec[last+1].creditPoint << endl;
             }
        }
        cout << "Would you like to search and/or add another student record? Y/N\n";
        cin >> option2;
        if(option2 == 'N')
        {
            break;
        }
        }while (option2 == 'Y');
        pause ();
        break;
        }


Any direction would be much appreciated. I am quite new to C++ so any explanations as to why it doesn't work would be awesome too! Please and thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Deleting struct from array of structs problem!

#2 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 455
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: Deleting struct from array of structs problem!

Posted 02 November 2011 - 06:35 PM

From what I can see your code is not searching for the record at all, it will just check position mid.

    while (first <= last && !found)//search for a student ID match
    {
        if (stdRec[mid].studentID == stdIDInput)
        {
            found = true;
            cout << "This student ID already exists in the system.\n";
            cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
            cout << setiosflags(ios::left);
            cout << setw(12) << stdRec[mid].studentID
                 << setw(21) << stdRec[mid].studentName
                 << setw(13) << stdRec[mid].courseCode
                 << setw(5)  << stdRec[mid].creditPoint << endl;
        }
        else if (stdRec[mid].studentID > stdIDInput)
        {
            last = mid -1;
        }
        else
        {
            first = mid +1;
        }
    }



Somewhere in this code you have to recalculate mid so that mid will always be between first and last.
Was This Post Helpful? 0
  • +
  • -

#3 C++ newb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-September 11

Re: Deleting struct from array of structs problem!

Posted 02 November 2011 - 08:37 PM

View PostKarel-Lodewijk, on 02 November 2011 - 06:35 PM, said:

From what I can see your code is not searching for the record at all, it will just check position mid.

    while (first <= last && !found)//search for a student ID match
    {
        if (stdRec[mid].studentID == stdIDInput)
        {
            found = true;
            cout << "This student ID already exists in the system.\n";
            cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
            cout << setiosflags(ios::left);
            cout << setw(12) << stdRec[mid].studentID
                 << setw(21) << stdRec[mid].studentName
                 << setw(13) << stdRec[mid].courseCode
                 << setw(5)  << stdRec[mid].creditPoint << endl;
        }
        else if (stdRec[mid].studentID > stdIDInput)
        {
            last = mid -1;
        }
        else
        {
            first = mid +1;
        }
    }



Somewhere in this code you have to recalculate mid so that mid will always be between first and last.


Ahhhhh, thanks for clearing that up! Also, my program keeps crashing when the user opts to add the input to the record. What I'm trying to do is allow the user to add a new student record to the system. If the user inputs a student ID already existing in the system, it doesn't accept the ID, but if it's a new student ID, it asks the user if they want to add the student to the system. When the user opts 'Y', it crashes! It's not supposed to! Here's the relevant code:
    case 6://add a student record
        {
        int first = 0;
        int last = NUMRECS;
        int mid;
        int studentCount, newStdCourseCode, newStdCreditPoint;
        studentCount = NUMRECS;
        char decision3, option2, optAgain5, optAgain6, optAgain7, optAgain8, optAgain9, optAgain10, optAgain11, optAgain12;
        string newStdName;
        do
        {
        cout << "Please type in the student ID number\nof the student you want to input,\nthen press enter.\n";
        cin >> stdIDInput;
        bool found = false;
        while (first <= last && !found)//checks if there's a match
        {
            mid = (first + last)/2;
            if (stdRec[mid].studentID == stdIDInput)
            {
                found = true;
                cout << "This student ID already exists in the system.\n";
                cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
                cout << setiosflags(ios::left);
                cout << setw(12) << stdRec[mid].studentID
                     << setw(21) << stdRec[mid].studentName
                     << setw(13) << stdRec[mid].courseCode
                     << setw(5)  << stdRec[mid].creditPoint << endl;
            }
            else if (stdRec[mid].studentID > stdIDInput)
            {
                last = mid -1;
            }
            else
            {
                first = mid +1;
            }
        }
        if (!found)
        {
            cout << "Would you like to add the student to the records? Y/N\n";
            cin >> decision3;
            if(decision3 == 'Y')//***HERE!!! This is when it crashes if you opt 'Y' and I don't know how to fix it!
            {
                last = NUMRECS;
                studentCount++;
                if((stdIDInput > "17000010") && (stdIDInput < "99999999"))
                {
                    stdRec[last+1].studentID = stdIDInput;
                    cout << "Please input the new student's name.\n";
                    getline (cin, newStdName);
                    stdRec[last+1].studentName = newStdName;
                    do
                    {
                        cout << "\n\nPlease input the new student's course code (the SCM offers courses 3506, 3633, 3634 \nand 3639).\n";
                        cin >> newStdCourseCode;
                        
                        if ((newStdCourseCode == 3506) || (newStdCourseCode == 3633) || (newStdCourseCode == 3634) || (newStdCourseCode == 3639)) 
                        {
                            stdRec[last+1].courseCode = newStdCourseCode;
                        }
                        else
                        {
                            cout << "Not a valid course code. SCM only offers courses 3506, 3633, 3634 or 3639.\n"
                                 << "Would you like to try again? Y/N";
                            cin >> optAgain5;
                            if(optAgain5 == 'N')
                            {
                                break;
                            }
                        }
                    }while (optAgain5 = 'Y');
                    
                    do
                    {
                        cout << "Please input the new student's credit points. Valid credit points can range from 0-240 as multiples of 10.\n";
                        cin >> newStdCreditPoint;
                        if ((newStdCreditPoint == 0) || (newStdCreditPoint == 10) || (newStdCreditPoint == 20) || (newStdCreditPoint == 30) || (newStdCreditPoint == 40) || (newStdCreditPoint == 50) || (newStdCreditPoint == 60) || (newStdCreditPoint == 70) || (newStdCreditPoint == 80) || (newStdCreditPoint == 90) || (newStdCreditPoint == 100) || (newStdCreditPoint == 110) || (newStdCreditPoint == 120) || (newStdCreditPoint == 130) || (newStdCreditPoint == 140) || (newStdCreditPoint == 150) || (newStdCreditPoint == 160) || (newStdCreditPoint == 170) || (newStdCreditPoint == 180) || (newStdCreditPoint == 190) || (newStdCreditPoint == 200) || (newStdCreditPoint == 210) || (newStdCreditPoint == 220) || (newStdCreditPoint == 230) || (newStdCreditPoint == 240))
                        {
                            stdRec[last+1].creditPoint = newStdCreditPoint;
                        }
                        else
                        {
                            cout << "Not a valid credit point. Valid credit points can range from 0-240 as multiples of 10.\n"
                                 << "Would you like to try again? Y/N\n";
                            cin >> optAgain9;
                            if (optAgain9 == 'N')
                            {
                                break;
                            }
                        }
                    }while (optAgain9 = 'Y');
                    
                    cout << "The student's information has now been added to the records.\n";
                    cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
                    cout << setiosflags(ios::left);
                    cout << setw(12) << stdRec[last+1].studentID
                         << setw(21) << stdRec[last+1].studentName
                         << setw(13) << stdRec[last+1].courseCode
                         << setw(5)  << stdRec[last+1].creditPoint << endl;
                }
                if((stdIDInput > "16666666") && (stdIDInput < "17000010"))
                {
                    stdRec[last+1].studentID = stdRec[last].studentID;
                    stdRec[last+1].studentName = stdRec[last].studentName;
                    stdRec[last+1].courseCode = stdRec[last].courseCode;
                    stdRec[last+1].creditPoint = stdRec[last].creditPoint;
                    stdRec[last].studentID = stdIDInput;
                    cout << "Please input the new student's name.\n";
                    getline (cin, newStdName);
                    stdRec[last].studentName = newStdName;
                    do
                    {
                        cout << "\n\nPlease input the new student's course code (the SCM offers courses 3506, 3633, 3634 \nand 3639).\n";
                        cin >> newStdCourseCode;
                        
                        if ((newStdCourseCode == 3506) || (newStdCourseCode == 3633) || (newStdCourseCode == 3634) || (newStdCourseCode == 3639)) 
                        {
                            stdRec[last].courseCode = newStdCourseCode;
                        }
                        else
                        {
                            cout << "Not a valid course code. SCM only offers courses 3506, 3633, 3634 or 3639.\n"
                                 << "Would you like to try again? Y/N";
                            cin >> optAgain6;
                            if(optAgain6 == 'N')
                            {
                                break;
                            }
                        }
                    }while (optAgain6 = 'Y');
                    
                    do
                    {
                        cout << "Please input the new student's credit points. Valid credit points can range from 0-240 as multiples of 10.\n";
                        cin >> newStdCreditPoint;
                        if ((newStdCreditPoint == 0) || (newStdCreditPoint == 10) || (newStdCreditPoint == 20) || (newStdCreditPoint == 30) || (newStdCreditPoint == 40) || (newStdCreditPoint == 50) || (newStdCreditPoint == 60) || (newStdCreditPoint == 70) || (newStdCreditPoint == 80) || (newStdCreditPoint == 90) || (newStdCreditPoint == 100) || (newStdCreditPoint == 110) || (newStdCreditPoint == 120) || (newStdCreditPoint == 130) || (newStdCreditPoint == 140) || (newStdCreditPoint == 150) || (newStdCreditPoint == 160) || (newStdCreditPoint == 170) || (newStdCreditPoint == 180) || (newStdCreditPoint == 190) || (newStdCreditPoint == 200) || (newStdCreditPoint == 210) || (newStdCreditPoint == 220) || (newStdCreditPoint == 230) || (newStdCreditPoint == 240))
                        {
                            stdRec[last].creditPoint = newStdCreditPoint;
                        }
                        else
                        {
                            cout << "Not a valid credit point. Valid credit points can range from 0-240 as multiples of 10.\n"
                                 << "Would you like to try again? Y/N\n";
                            cin >> optAgain10;
                            if (optAgain10 == 'N')
                            {
                                break;
                            }
                        }
                    }while (optAgain10 = 'Y');
                    
                    cout << "The new student has now been added to the record as below.\n";
                    cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
                    cout << setiosflags(ios::left);
                    cout << setw(12) << stdRec[last].studentID
                         << setw(21) << stdRec[last].studentName
                         << setw(13) << stdRec[last].courseCode
                         << setw(5)  << stdRec[last].creditPoint << endl;
            }
            if((stdIDInput > "15000000") && (stdIDInput < "16666666"))
            {
                stdRec[last+1].studentID = stdRec[last].studentID;
                stdRec[last+1].studentName = stdRec[last].studentName;
                stdRec[last+1].courseCode = stdRec[last].courseCode;
                stdRec[last+1].creditPoint = stdRec[last].creditPoint;
                stdRec[last].studentID = stdRec[last-1].studentID;
                stdRec[last].studentName = stdRec[last-1].studentName;
                stdRec[last].courseCode = stdRec[last-1].courseCode;
                stdRec[last].creditPoint = stdRec[last-1].creditPoint;
                stdRec[last-1].studentID = stdIDInput;
                cout << "Please input the new student's name.\n";
                getline (cin, newStdName);
                stdRec[last-1].studentName = newStdName;
                do
                    {
                        cout << "\n\nPlease input the new student's course code (the SCM offers courses 3506, 3633, 3634 \nand 3639).\n";
                        cin >> newStdCourseCode;
                        
                        if ((newStdCourseCode == 3506) || (newStdCourseCode == 3633) || (newStdCourseCode == 3634) || (newStdCourseCode == 3639)) 
                        {
                            stdRec[last-1].courseCode = newStdCourseCode;
                        }
                        else
                        {
                            cout << "Not a valid course code. SCM only offers courses 3506, 3633, 3634 or 3639.\n"
                                 << "Would you like to try again? Y/N";
                            cin >> optAgain7;
                            if(optAgain7 == 'N')
                            {
                                break;
                            }
                        }
                    }while (optAgain7 = 'Y');
                
                do
                    {
                        cout << "Please input the new student's credit points. Valid credit points can range from 0-240 as multiples of 10.\n";
                        cin >> newStdCreditPoint;
                        if ((newStdCreditPoint == 0) || (newStdCreditPoint == 10) || (newStdCreditPoint == 20) || (newStdCreditPoint == 30) || (newStdCreditPoint == 40) || (newStdCreditPoint == 50) || (newStdCreditPoint == 60) || (newStdCreditPoint == 70) || (newStdCreditPoint == 80) || (newStdCreditPoint == 90) || (newStdCreditPoint == 100) || (newStdCreditPoint == 110) || (newStdCreditPoint == 120) || (newStdCreditPoint == 130) || (newStdCreditPoint == 140) || (newStdCreditPoint == 150) || (newStdCreditPoint == 160) || (newStdCreditPoint == 170) || (newStdCreditPoint == 180) || (newStdCreditPoint == 190) || (newStdCreditPoint == 200) || (newStdCreditPoint == 210) || (newStdCreditPoint == 220) || (newStdCreditPoint == 230) || (newStdCreditPoint == 240))
                        {
                            stdRec[last-1].creditPoint = newStdCreditPoint;
                        }
                        else
                        {
                            cout << "Not a valid credit point. Valid credit points can range from 0-240 as multiples of 10.\n"
                                 << "Would you like to try again? Y/N\n";
                            cin >> optAgain11;
                            if (optAgain11 == 'N')
                            {
                                break;
                            }
                        }
                    }while (optAgain11 = 'Y');
                
                cout << "The new student has now been added to the record as below.\n";
                cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
                cout << setiosflags(ios::left);
                cout << setw(12) << stdRec[last-1].studentID
                     << setw(21) << stdRec[last-1].studentName
                     << setw(13) << stdRec[last-1].courseCode
                     << setw(5)  << stdRec[last-1].creditPoint << endl;
            }
            if((stdIDInput > "0") && (stdIDInput < "15000000"))
            {
                stdRec[last+1].studentID = stdRec[last].studentID;
                stdRec[last+1].studentName = stdRec[last].studentName;
                stdRec[last+1].courseCode = stdRec[last].courseCode;
                stdRec[last+1].creditPoint = stdRec[last].creditPoint;
                stdRec[last].studentID = stdRec[last-1].studentID;
                stdRec[last].studentName = stdRec[last-1].studentName;
                stdRec[last].courseCode = stdRec[last-1].courseCode;
                stdRec[last].creditPoint = stdRec[last-1].creditPoint;
                stdRec[last-1].studentID = stdRec[last-2].studentID;
                stdRec[last-1].studentName = stdRec[last-2].studentName;
                stdRec[last-1].courseCode = stdRec[last-2].courseCode;
                stdRec[last-1].creditPoint = stdRec[last-2].creditPoint;
                stdRec[last-2].studentID = stdIDInput;
                cout << "Please input the new student's name.\n";
                getline (cin, newStdName);
                stdRec[last-2].studentName = newStdName;
                do
                    {
                        cout << "\n\nPlease input the new student's course code (the SCM offers courses 3506, 3633, 3634 \nand 3639).\n";
                        cin >> newStdCourseCode;
                        
                        if ((newStdCourseCode == 3506) || (newStdCourseCode == 3633) || (newStdCourseCode == 3634) || (newStdCourseCode == 3639)) 
                        {
                            stdRec[last-2].courseCode = newStdCourseCode;
                        }
                        else
                        {
                            cout << "Not a valid course code. SCM only offers courses 3506, 3633, 3634 or 3639.\n"
                                 << "Would you like to try again? Y/N";
                            cin >> optAgain8;
                            if(optAgain8 == 'N')
                            {
                                break;
                            }
                        }
                    }while (optAgain8 = 'Y');
                
                do
                    {
                        cout << "Please input the new student's credit points. Valid credit points can range from 0-240 as multiples of 10.\n";
                        cin >> newStdCreditPoint;
                        if ((newStdCreditPoint == 0) || (newStdCreditPoint == 10) || (newStdCreditPoint == 20) || (newStdCreditPoint == 30) || (newStdCreditPoint == 40) || (newStdCreditPoint == 50) || (newStdCreditPoint == 60) || (newStdCreditPoint == 70) || (newStdCreditPoint == 80) || (newStdCreditPoint == 90) || (newStdCreditPoint == 100) || (newStdCreditPoint == 110) || (newStdCreditPoint == 120) || (newStdCreditPoint == 130) || (newStdCreditPoint == 140) || (newStdCreditPoint == 150) || (newStdCreditPoint == 160) || (newStdCreditPoint == 170) || (newStdCreditPoint == 180) || (newStdCreditPoint == 190) || (newStdCreditPoint == 200) || (newStdCreditPoint == 210) || (newStdCreditPoint == 220) || (newStdCreditPoint == 230) || (newStdCreditPoint == 240))
                        {
                            stdRec[last-2].creditPoint = newStdCreditPoint;
                        }
                        else
                        {
                            cout << "Not a valid credit point. Valid credit points can range from 0-240 as multiples of 10.\n"
                                 << "Would you like to try again? Y/N\n";
                            cin >> optAgain12;
                            if (optAgain12 == 'N')
                            {
                                break;
                            }
                        }
                    }while (optAgain12 = 'Y');
                
            }
            else
            {
                cout << "Not a valid student ID. It cannot be less than zero and more than eight digits long.\n";
            }
        }
    }
        cout << "Would you like to search and/or add another student record? Y/N\n";
        cin >> option2;
        if(option2 == 'N')
        {
            break;
        }
        }while (option2 == 'Y');
        
        pause ();
        break;
    }

Any and all advice would be much appreciated, please and thank-you!
Was This Post Helpful? 0
  • +
  • -

#4 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Deleting struct from array of structs problem!

Posted 02 November 2011 - 10:53 PM

You make like to see the examples here ...
http://developers-heaven.net/forum/index.php/topic,2019.0.html

sorts, searches, deletes from an array, etc...
Was This Post Helpful? 0
  • +
  • -

#5 C++ newb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-September 11

Re: Deleting struct from array of structs problem!

Posted 03 November 2011 - 12:28 AM

View PostDavid W, on 02 November 2011 - 10:53 PM, said:

You make like to see the examples here ...
http://developers-heaven.net/forum/index.php/topic,2019.0.html

sorts, searches, deletes from an array, etc...


Uhhh... well it certainly looked helpful, but as a beginner, I need things explained to me because I don't understand :(
Was This Post Helpful? 0
  • +
  • -

#6 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Deleting struct from array of structs problem!

Posted 03 November 2011 - 12:50 AM

If you need to code for yourself a function to erase an element in an array, a common way to delete a struct ( a record ) in an array is to copy down all the records above it ... then to decrement the size counter of the array ... (Note: the capacity variable is not changed.)

Reading/coding the simple stuff first, like the example of an array of integers, can help ... because the same method applies to an array of struct.

This post has been edited by David W: 03 November 2011 - 01:16 AM

Was This Post Helpful? 0
  • +
  • -

#7 C++ newb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-September 11

Re: Deleting struct from array of structs problem!

Posted 03 November 2011 - 02:28 AM

View PostDavid W, on 03 November 2011 - 12:50 AM, said:

If you need to code for yourself a function to erase an element in an array, a common way to delete a struct ( a record ) in an array is to copy down all the records above it ... then to decrement the size counter of the array ... (Note: the capacity variable is not changed.)

Reading/coding the simple stuff first, like the example of an array of integers, can help ... because the same method applies to an array of struct.

Ah! I kind of figured out how to delete a structure a little while ago, but would it be possible if you could advise me on how to increase the set size of an array? Like if I had a const int MAXRECS variable which controlled the maximum possible amount of student records (structs in the array), and if I had another const int NUMRECS which controlled the actual amount of student records I currently had. How could I increase NUMRECS if I wanted to add a student record? I've tried making NUMRECS just an int type and not const, but I got errors straight away! Any direction would be much, much appreciated! Please and thank-you!
Was This Post Helpful? 0
  • +
  • -

#8 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Deleting struct from array of structs problem!

Posted 03 November 2011 - 10:13 AM

You need two variables, (maybe use global variables at first, to keep it simple),
int size = 0, cap = 0; // to track size and capacity of array
const int begin_cap = 1; // set this to 1 or greater to reflect the expected size needed

//This example is for reading a file of int's into a growing dynamic array of integers...
int* ary = 0;
int x;
ifstream fin( "yourFileName.txt" );
while( fin >> x )
{
   if( size  == cap ) // expand array ...
   {
       if( cap != 0  ) cap += cap; // double capacity
       else cap  = begin_cap;
       int* nary = new int[cap]; // get new expanded array space
       for( int i = 0; i < size; ++i ) nary[i]  = ary[i]; // copy old to new 
       delete [] ary; // free old dynamic memory
       ary = nary; // update ary address to new memory
   }
   ary[size] = x; // put next value into array
   ++ size; // update size counter
}


You might also like to see the links at this DIC thread...
http://www.dreamincode.net/forums/topic/253725-dynamically-allocates-an-array/page__pid__1477172__st__0&#entry1477172

and/or here ...(see examples of dynamic arrays 4. and 5. near end)
http://developers-heaven.net/forum/index.php/topic,2019.msg2923.html#msg2923

Note: you could put most of the above into a function, and just call ...
Here is an example of a possible function prototype ...
push_back( int*& ary, int x );

Or ... If you want to create a new type...
typedef int* pIntAry;
then a possible function prototype might look like this ...
push_back( pIntAry& ary, int x);

And, as mentioned below, unless you 'need' to code your own dynamic array,
the STL vector is designed to handle all your requests for a dynamic array of any struct (or class) and there are many functions available such as CT mentioned below...

This post has been edited by David W: 03 November 2011 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,945
  • Joined: 08-August 08

Re: Deleting struct from array of structs problem!

Posted 03 November 2011 - 12:50 PM

If you're using C++ and you need to be able to delete and sort an array of structs then why aren't you using a vector of structs instead?
Was This Post Helpful? 1
  • +
  • -

#10 C++ newb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-September 11

Re: Deleting struct from array of structs problem!

Posted 03 November 2011 - 02:21 PM

View PostCTphpnwb, on 03 November 2011 - 12:50 PM, said:

If you're using C++ and you need to be able to delete and sort an array of structs then why aren't you using a vector of structs instead?

Well we were able to choose between using an array of structs or parallel arrays... I chose the former so... yeah.

View PostDavid W, on 03 November 2011 - 10:13 AM, said:

You need two variables, (maybe use global variables at first, to keep it simple),
int size = 0, cap = 0; // to track size and capacity of array
const int begin_cap = 1; // set this to 1 or greater to reflect the expected size needed

//This example is for reading a file of int's into a growing dynamic array of integers...
int* ary = 0;
int x;
ifstream fin( "yourFileName.txt" );
while( fin >> x )
{
   if( size  == cap ) // expand array ...
   {
       if( cap != 0  ) cap += cap; // double capacity
       else cap  = begin_cap;
       int* nary = new int[cap]; // get new expanded array space
       for( int i = 0; i < size; ++i ) nary[i]  = ary[i]; // copy old to new 
       delete [] ary; // free old dynamic memory
       ary = nary; // update ary address to new memory
   }
   ary[size] = x; // put next value into array
   ++ size; // update size counter
}


You might also like to see the links at this DIC thread...
http://www.dreamincode.net/forums/topic/253725-dynamically-allocates-an-array/page__pid__1477172__st__0&#entry1477172

and/or here ...(see examples of dynamic arrays 4. and 5. near end)
http://developers-heaven.net/forum/index.php/topic,2019.msg2923.html#msg2923

Note: you could put most of the above into a function, and just call ...
Here is an example of a possible function prototype ...
push_back( int*& ary, int x );

Or ... If you want to create a new type...
typedef int* pIntAry;
then a possible function prototype might look like this ...
push_back( pIntAry& ary, int x);

And, as mentioned below, unless you 'need' to code your own dynamic array,
the STL vector is designed to handle all your requests for a dynamic array of any struct (or class) and there are many functions available such as CT mentioned below...

Ah! Well I tried to stick far away from pointers and vectors because we hadn't learned it yet! I swapped some things around and was able to make NUMRECS not a const int and also, someone suggested I use this:
const int MAXRECS = 20;  // or however many you need
StudentRecord StdRec[MAXRECS];  // all empty at this point
int NUMRECS = 0;  // there are no filled records in the array

...

// user wants to add a record
if (NUMRECS == MAXRECS) {
    cout << "Sorry, there's no more room to add a new student record" << endl;
}
else {
    // find insertIndex where to add a new record so that the array stays sorted:
    // such that stdRec[insertIndex-1].studentID < stdIDInput < stdRec[insertIndex].studentID
    // move records with (ID > newID) up one position -- opposite of when you delete
    for (int i = NUMRECS-1;  i >= insertIndex;  i--)
        stdRec[i+1] = stdRec[i];
    // copy data into freed-up record
    stdRec[insertIndex].studentID = stdIDInput;
    ...
    // update current number of used records
    NUMRECS += 1;
}


Note, not my code, foundhere.

But I'm not so sure how to find insertIndex. Would I be able to use the comparative operators < and > Like in my original code? Like if I said:
if(stdIDInput >=0 && stdIDInput < 15000000)
{
     insertIndex = 1; //or someting like that?
}


Any suggestions?

This post has been edited by C++ newb: 03 November 2011 - 02:22 PM

Was This Post Helpful? 0
  • +
  • -

#11 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Deleting struct from array of structs problem!

Posted 03 November 2011 - 08:59 PM

Ok ... so you are limited to using an array with the max size fixed at compile time.

You first need your array in sorted order, before you can insert a new element in sorted order.

So, you might like to see this insert sort for an array ...
http://developers-heaven.net/forum/index.php/topic,487.0.html

This post has been edited by David W: 03 November 2011 - 09:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1