Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Help with vectors of class
New Posts  All Forums:Forum Nav:

C++ Help with vectors of class

post #1 of 17
Thread Starter 
READ EDITED SPOT DISREGARD TILL THERE.

I don't know to much about deconstructors. What I am trying to do is say I have array[50]; and I only need to deconstruct array[6], how would I type that into my program properly.

Would it just be:
Code:
array[6].~deconstruct

I am fully aware that the deconstructor is just ~(name of class) so please don't get mad about that.

Also if that is correct what happens to everything in [7] - [49]? Will they move down so [7] becomes [6]? or will [6] just be empty and ill need to rearrange it myself?

Thanks for the help guys.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Edit~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Okay I am having problems now that I am trying to implement vectors instead of normal arrays.
I am going to post my code in full but the places I am having there errors are in lines 139-149. Please help, I know my code isn't finished but I don't want to go farther till I figure out the vectors

Here is my code:
Code:
//Employee.cpp

// INCLUDED LIBRARY'S
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "EmployeeInfo.h"
using namespace std;

// GLOBAL CONSTSANTS
const string HEADER = "*********************************";
const int ZERO_INITIALIZATION = 0;
const int EMPLOYEE_ARRAY = 100;

// PROGRAM PROTOTYPES
void runPrgMenu(vector <EmployeeInfo> *employee, bool isLooping, int totCount);
void sortEmployeeData(vector <EmployeeInfo> *employee, int totCount);
int setEmployeeData_LocalFile(vector <EmployeeInfo> *employee, ifstream& finEmployee);
int setEmployeeData_Keyboard(vector <EmployeeInfo> *employee);

int main()
{
        // DEFINITIONS
        ifstream finEmployee("employee.dat");         // Declares input file
        
        vector <EmployeeInfo> employee (100);

//      EmployeeInfo employee[EMPLOYEE_ARRAY];          // Creates room for 100 objects

        int     totCount(0);

        bool isLooping = true;

        // VALIDATES INPUT FILE
        if(!finEmployee)
        {
                system("CLS");
                cout << HEADER << endl;
                cout << "Error:  Unable to find input file" << endl;
                cout << "Program terminating" << endl;
                cout << HEADER << endl;
                system("PAUSE");
                return 1;
        }

        // INFORMATION GATHERING
        cout << HEADER << endl;
        cout << "Currently Gathering input files" << endl;
        cout << HEADER << endl;
        totCount = setEmployeeData_LocalFile(&employee, finEmployee);
        finEmployee.close();
        system("PAUSE");

        // INFORMATION SORTING
        system("CLS");
        cout << HEADER << endl;
        cout << "Currently sorting employee's last names alphabetically" << endl;
        cout << "Please wait" << endl;
        cout << HEADER << endl;
        sortEmployeeData(&employee, totCount);
        system("PAUSE");

        // RUN PROGRAMS MENU
        runPrgMenu(&employee, isLooping, totCount);

        // EXIT ROUTINE
        cout << endl << endl;
        system("PAUSE");
        return 0;
}

void runPrgMenu(vector <EmployeeInfo> *employee, bool isLooping, int totCount)
{
        int choice;

        while(isLooping == true)
        {
                system("CLS");
                cout << HEADER << endl;
                cout << "Employee Information" << endl;
                cout << HEADER << endl;
                cout << "Set Employee Data:" << endl;
                cout << "Will Add more entries if previously done" << endl;
                cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
                cout << "[1] - Manual keyboard input" << endl;
                cout << "[2] - Through local input file (employee.dat)" << endl;
                cout << "[3] - File through other directory" << endl;
                cout << endl;
                cout << "Edit Employee Information" << endl;
                cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
                cout << "[4] - Last Name" << endl;
                cout << "[5] - First Name" << endl;
                cout << "[6] - Monthly Salary" << endl;
                cout << "Search Employee Information" << endl;
                cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
                cout << "[7] - Individual (first last)" << endl;
                cout << endl;
                cout << "List Employees Information" << endl;
                cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
                cout << "[8] - All Employees Information Alphabetically" << endl;
                cout << "[9] - Last Names starting with letter (user input)" << endl;
                cout << endl;
                cout << "Delete Employee Information" << endl;
                cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
                cout << "[10] - Single Entry" << endl;
                cout << "[11] - All Employees" << endl;
                cout << endl;
                cout << "Close Program" << endl;
                cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
                cout << "[0] - All data will be lost" << endl;
                cout << HEADER << endl;
                cout << "Please input choice:  " << endl;
                cin >> choice;

                switch(choice)
                {
                case 0:
                        isLooping = false;
                        break;
                }               //End Switch statement
        }               // End while menu loop
}               // End runPrgMenu()


int setEmployeeData_LocalFile(vector <EmployeeInfo> *employee, ifstream& finEmployee)
{
        // DEFINITIONS
        string  lastName,                       //Temp storage for lName_
                        firstName;                      //Temp storage for fName_

        int             errorCount(0),          //Error count while inputing data
                        totCount(0);            //Total entries attempted
        
        double  payRate;                        //Temp storage for monthlySal_

        
        for(int i = ZERO_INITIALIZATION;
                finEmployee >> lastName, finEmployee >> firstName, finEmployee >> payRate;
                i++, totCount++)
        {
                if(i > employee->size())
                        employee->push_back(EmployeeInfo);

                employee[i].setEmployeeLName(lastName);
                employee[i].setEmployeeFName(firstName);
                employee[i].setEmployeeMSal(payRate);
        }               // End for loop(data input)
        
        if(errorCount > ZERO_INITIALIZATION)
        {

                cout << "Error:  Can only hold a maximum of " << EMPLOYEE_ARRAY;

                if(EMPLOYEE_ARRAY == 1)
                        cout << " entry." << endl;
                else
                        cout << " entries." << endl;
        
                cout << "        The input file contained " << totCount;
                
                if(totCount == 1)
                        cout << " entry." << endl;
                else
                        cout << " entries." << endl;
                
                cout << "        Therefore there";
                
                if(errorCount == 1)
                        cout << " was " << errorCount << " entry";
                else
                        cout << " where" << errorCount << " entries";
        
                cout << " not entered." << endl << endl;
        }               // End if(errorCount > ZERO_INITIALIZATION)
        
        else
                cout << "Total employees:  " << totCount << endl;

        return totCount;
}               // End function setEmployeeData()

void sortEmployeeData(vector <EmployeeInfo> *employee, int totCount)
{
        int     i(0),           //Temporary storage for sorting
                j(0);           //Temporary storage for sorting
                
        EmployeeInfo    key;    //Used for location in array

        for(j = 1; j < totCount; j++)
        {
                key = employee[j];

                for(i = j - 1; 
                        (i >= 0) 
                        && ((employee[i].getEmployeeLName() + employee[i].getEmployeeFName()) > (key.getEmployeeLName() + key.getEmployeeFName()));
                        i--)
                {
                        employee[i + 1] = employee[i]; 
                }

                employee[i + 1] = key;
        }
}

int setEmployeeData_Keyboard(vector <EmployeeInfo> *employee)
{
        string  lName,          //Employee's last name (temp storage)
                        fName;          //Employee's first name (temp storage)

        int             payRate;        //Employee's pay rate (temp storage)

        cout << HEADER << endl;
        cout << "Please enter employee's information as (Last) (First) (Pay rate):  " << endl;
        cout << HEADER << endl;
        cout << ""
}

int setEmployeeData_LocalFile(vector <EmployeeInfo> *employee, ifstream& finEmployee);
{

}

Here is my class.h:
Code:
//EmployeeInfo

#include <string>
using namespace std;

class EmployeeInfo
{
public:
        EmployeeInfo();

        void setEmployeeLName(string lName);
        void setEmployeeFName(string fName);
        void setEmployeeMSal(double monthlySal);
        
        string getEmployeeLName() const;
        string getEmployeeFName() const;
        double getEmployeeMSal()  const;
        
        void displayEmployeeInfo() const;

private:
        string lastName_;
        string firstName_;
        double monthlySal_;
};

Here is my class.cpp:
Code:
//EmployeeInfo.cpp

#include <cstdlib>
#include <iostream>
#include <string>
#include "EmployeeInfo.h"
using namespace std;

const string HEADER = "**************************";

EmployeeInfo::EmployeeInfo()
{
        firstName_ = "";
        lastName_ = "";
        monthlySal_ = 0.0;
}

void EmployeeInfo::setEmployeeLName(string lName)
{
        lastName_ = lName;
}

void EmployeeInfo::setEmployeeFName(string fName)
{
        firstName_ = fName;
}

void EmployeeInfo::setEmployeeMSal(double monthlySal)
{
        if(monthlySal < 0)
                monthlySal_ = 0;
        else
        monthlySal_ = monthlySal;
}

string EmployeeInfo::getEmployeeLName() const
{
        return lastName_;
}

string EmployeeInfo::getEmployeeFName() const
{
        return firstName_;
}

double EmployeeInfo::getEmployeeMSal() const
{
        return monthlySal_;
}

void EmployeeInfo::displayEmployeeInfo() const
{
        system("CLS");
        cout << HEADER << endl;
        cout << "*  EMPLOYEE INFORMATION  *" << endl;
        cout << HEADER << endl;
        cout << "Last Name:    " << lastName_ << endl;
        cout << "First Name:   " << firstName_ << endl;
        cout << "Monthly Sal:  " << monthlySal_ << endl;
        cout << HEADER << endl;
        system("PAUSE");
}

Also if you see anything that would improve my readability, or performance, or anything else please let me know, I want to learn. Also please let me know why something isn't working, don't just post code to fix it, because I won't learn from that.

Thanks OCN
Edited by KingAroan - 3/4/12 at 9:51am
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
post #2 of 17
You can't do that. If you have an array of real objects (i.e. not references or pointers), then all of them will have their destructors called when the array leaves scope.

If you want to dynamically create and delete objects, you have to use pointers, and the new and delete operators. However, you shouldn't use an array, but some other data structure. To answer your question: you can't remove an element from an array after it's been created. If you have an array of 50 pointers and you call delete on the 6th element, it'll still be there - the pointer will just be invalid.

It sounds like you need to read my sig, and ask the right question, so someone can come along and point you in the direction of the correct solution to your problem.
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #3 of 17
Thread Starter 
Since I'm just starting then, and I don't know much about vectors and I hate pointers, I'll just have everything behind the object move up in the array to overwrite what was in the array

Sent from my Broodcomb using Tapatalk
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
post #4 of 17
That is bad practice. As I said, try describing the problem you're trying to solve and someone will come along and advise you about possible solutions using best practices.
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #5 of 17
Thread Starter 
Alright, for my class in school we have to make a class to handle employees data, the 3 data members we need are last name first name and monthly salary. Then we have to create a program along side it to test the class. This program must hold up to 100 employees from a file. The teacher must be able to then search that file with employees first then last name to pull up the employees record (so basically the monthly salary is looked up). That's not hard and have had it done for close to a week. I am trying to challenge myself to make a detailed program that can handle just more than the minimum for the project. Our teacher this semester doesn't mind if we challenge ourselves to learn more.

So currently I made the program and am adding stuff to it. I have a menu that will decide what functions to use. I am also adding options to add employees through keyboard input, and a file not locale to where the program is located. Those are not hard either.

My program since it only stores up to 100 employees per the teachers request, I am using an insertion sort algorithm to make searching the names easier with a binary search.

My problem is that I want to be able to delete employees say if they get "fired" and be able to add employees at anytime right after. Also be able to delete every employee if such a case where to arise. That last one I hope would never be needed in the real world but you never know. It would be needed on other objects though.

Currently I am using an array of 100 for this. What is a better way? To

Sent from my Broodcomb using Tapatalk
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
post #6 of 17
A std::vector smile.gif Just learn about it, it's exactly what you need. If you use pointers or just move objects within the array (this will just copy them), you'll just end up implementing a very simple vector.
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #7 of 17
Thread Starter 
I can also research things that I don't know yet as long as it doesn't need a special library not built in.

Sent from my Broodcomb using Tapatalk
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
post #8 of 17
Thread Starter 
I know a little about vectors but ill read up on it thanks.

Sent from my Broodcomb using Tapatalk
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
post #9 of 17
Thread Starter 
It looks like vector's re-size automatically if there is not enough room to store the value, how would I stop that?
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
post #10 of 17
Well, why would you want to? I don't think your teacher will complain if it can hold more than 100, but you can always just hide that fact from the user, preventing them from adding more.
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Help with vectors of class