Vectors, Structs, and either not setting or retrieving string variable

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1637 Views - Last Post: 25 April 2018 - 07:23 AM Rate Topic: -----

#1 SloanTheSloth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 18

Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 11:56 AM

I'm not sure where the exact issue is. The part of the program I'm on is a class that will manage a list of Employees. The employees are held in a certain file, and are to be retrieved upon starting the application. If the file is empty, a default admin is created with username admin, password 0000.

I made a function to load in the employees, and was attempting to test it. I wanted to use a friend function for testing purposes, so it can access and double check private items in the EmployeeManager class.


This is the current class definition:
  class EmployeeManager {
	public:
		EmployeeManager(string filename); // Constructor
		EmployeeManager(); //Default constructor
		bool verifyLogin(string username, string password); //Verifies login information
		bool changePassword(string username, string oldPass, string newPass); //change password
		bool isAdmin(string username); //Verify if user is admin
		bool addEmployee(string username, string password, bool isAdmin); //add Employee
		bool deleteEmployee(string username);
		void displayEmployees(); //Displays employee list. Might overload << instead.
		friend void test_loadEmployees(); //Test function
	private:
		//Struct to hold employee information.
		struct Employee { 
			string username;
			string password;
			bool admin; //True if admin, false if not
		};
		typedef Employee* EmployeePtr; //Points to employees. Used for dynamic array.
		//EmployeePtr employeeList;
		vector<EmployeePtr> employeeList;
		string employeeFile; //File of employees
		void loadEmployees(); //Loads employees into employeeList
		void saveEmployees(); //save employeeList into employee file
		static const string DEFAULT_FILE;
};



The class contains a struct for employees. Originally, employeeList was of the Employee type, but I wasn't sure if that was causing an issue, so now I made a pointer for Employee, and the employeeList is full of employeePtrs.

This is my current function for "loadEmployees"

//Loads employees from employeeFile. If no employees, create default admin with password 0000.
//If fails to open, display error message.
void EmployeeManager::loadEmployees() {
	ifstream inStream;
	inStream.open((char*)employeeFile.c_str());
	//If cannot find or open, display error.
	if (inStream.fail()) {
		cout << "File open failed. Please use staff.txt\n";
	}
	//Retrieve employee info
	//MIGHT NEED TO COME BACK IF EMPLOYEES ARE REPEATING
	//if so neeed to make "new" temp Emp.
	EmployeePtr tempEmp;
	int tempBool;
	while (!inStream.eof()) {
		tempEmp = new Employee;
		inStream >> tempEmp->username;
		inStream >> tempEmp->password;
		inStream >> tempBool;
		//Sets admin status. Saved as 0 or 1. 0 = not admin, 1 = admin.
		if (tempBool == 0) {
			tempEmp->admin = false;
		}
		else {
			tempEmp->admin = true;
		}
		employeeList.push_back(tempEmp);
	}
	//If nothing in file, create default admin
	if (employeeList.empty()) {
		tempEmp = new Employee;
		tempEmp->username = "admin";
		tempEmp->password = "0000";
		tempEmp->admin = true;
		employeeList.push_back(tempEmp);
	}
	//For testing
	cout << employeeList.size();
	cout << employeeList[0]->username << endl;
	cout << employeeList[0]->password << endl;
	cout << employeeList[0]->admin << endl;
}




I made it start outputing the stuff to try and see where the issue was (in my test function or in this function).

When it prints it out, well, it doesn't. It prints out "1" a blank line, and then "0".

This leads me to believe the issue is with the function and not the testing function. For some reason, username and password are definitely not getting set.

Is This A Good Question/Topic? 0
  • +

Replies To: Vectors, Structs, and either not setting or retrieving string variable

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6166
  • View blog
  • Posts: 21,268
  • Joined: 05-May 12

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 12:01 PM

Can you show us the contents of the employee file?
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5471
  • View blog
  • Posts: 17,024
  • Joined: 25-December 09

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 12:26 PM

Also posted here.
Was This Post Helpful? 2
  • +
  • -

#4 SloanTheSloth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 18

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 01:23 PM

View PostSkydiver, on 23 April 2018 - 12:01 PM, said:

Can you show us the contents of the employee file?


The current employeeFile is empty. The test I was running was just to see if it would add the default employee if the file is empty.
Was This Post Helpful? 0
  • +
  • -

#5 SloanTheSloth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 18

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 05:06 PM

On another forum someone found out my issue. I was calling inStream.eof, but I hadn't actually attempted to pull anything from the file yet, so eof couldn't be detected.

Here's the link to his solution
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 05:10 PM

Quote

When it prints it out, well, it doesn't. It prints out "1" a blank line, and then "0".


Thats hardly surprising.....

If your file is empty your adding a blank entry to your vector employeeList.

employeeList size is therefore '1' which you printout, then password is blank.... therefore a blank line

and finally because tempBool is blank you set tempAdmin to false hence the '0'.

Makes perfect sense to me the computer is doing exactly what you told it to do.!!
Was This Post Helpful? 0
  • +
  • -

#7 SloanTheSloth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 18

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 05:45 PM

Another person fixed the issue for me.

The idea was right, I was just calling the inStream.eof too early. I was using it to check for the end of the file, but it doesn't work until you attempt an input. Anyway, after I fixed that everything else worked. My above reply has the actual link to the comment that fixed it if you wanted to see.

This post has been edited by ndc85430: 23 April 2018 - 10:31 PM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5471
  • View blog
  • Posts: 17,024
  • Joined: 25-December 09

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 08:01 PM

By the way, even though you think the program is correct, I suggest you rethink a couple of issues.

First look at this snippet:
		typedef Employee* EmployeePtr; //Points to employees. Used for dynamic array.
		//EmployeePtr employeeList;
		vector<EmployeePtr> employeeList;


Why are you using a pointer instance instead of just a normal instance? IMO, that snippet should look like:

		//typedef Employee* EmployeePtr; //Points to employees. Used for dynamic array.  // Get rid of this garbage, and note your comment is wrong.
		//EmployeePtr employeeList;
		//vector<EmployeePtr> employeeList;
                vector<Employee> employeeList;  // No need for a pointer just use a normal instance.



This change will mean you need to change a bit of the program because you won't need to use dynamic memory and without the dynamic memory you won't have the massive memory leak you presently have.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 SloanTheSloth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 18

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 08:14 PM

I'm glad you brought this up. Originally I had it as a normal thing, not a pointer, but when it wasn't working I thought maybe I had to do the pointer thing (obviously that wasn't the answer).

You could say I'm also confused when to use pointers. Do you mind explaining what scenario I would be using pointers instead of just using the instances?

This post has been edited by ndc85430: 23 April 2018 - 10:30 PM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5471
  • View blog
  • Posts: 17,024
  • Joined: 25-December 09

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 08:24 PM

Quote

You could say I'm also confused when to use pointers.

Rarely. Pointers should be avoided as much as possible, and when it is not possible to avoid them you should use smart pointers. And this program is not one of those rare moments, use regular non-pointer instances.

Edit: Also, IMO, never hide a pointer with a typedef. And note a typedef doesn't create an instance it creates a "type".

Jim

This post has been edited by jimblumberg: 23 April 2018 - 08:26 PM

Was This Post Helpful? 1
  • +
  • -

#11 SloanTheSloth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 18

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 08:53 PM

That definitely makes it alot easier then! I'm in a required course for my degree and we learned about pointers recently, and the typedef stuff. I don't like using it because it seems unnecessary, but the way the teacher talked about it made it sound like it was alot more common than it sounds like it really is.

Thanks for your input! :)/>

This post has been edited by ndc85430: 23 April 2018 - 10:30 PM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#12 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 801
  • View blog
  • Posts: 3,271
  • Joined: 13-June 14

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 10:32 PM

SloanTheSloth, there's no need to quote the previous post in its entirety. Please just use the "Reply" button in future, or when you need to quote, only quote the small part of the post that you want to reply to.
Was This Post Helpful? 1
  • +
  • -

#13 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 23 April 2018 - 10:56 PM

View PostSloanTheSloth, on 24 April 2018 - 12:45 AM, said:

The idea was right, I was just calling the inStream.eof too early. I was using it to check for the end of the file, but it doesn't work until you attempt an input. Anyway, after I fixed that everything else worked. My above reply has the actual link to the comment that fixed it if you wanted to see.


Everyone here is well aware of the problems in your code...

The only one who isn't aware is you.

Apparently you still think your ideas are right. Well they are not.. im sorry to tell you..

As Jim has already pointed out you have a memory leak and the way you have implemented everything is awkward and certainly not in the spirit of C++.
Was This Post Helpful? 0
  • +
  • -

#14 SloanTheSloth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 18

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 24 April 2018 - 04:32 AM

That's certainly an encouraging way to help a student! Thanks for your help. Now I will thrive as a programmer.
Was This Post Helpful? 0
  • +
  • -

#15 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 24 April 2018 - 04:40 AM

Oh for goodness sake we are trying to help you...

Given the lack of information about the assignment I would approach the problem something like this....

Perhaps not exactly like this but certainly something like this.

#include <iostream>
#include <string>
#include <stdbool.h>
#include <vector>
#include <sstream>
#include <fstream>

class Employee
{
public:
    Employee() {};
    void set_Employee(std::string Username, std::string Password, bool Is_Admin)
    {
        username = Username;
        password = Password;
        is_Admin = Is_Admin;
    }
    friend std::ostream &operator<<(std::ostream& output, std::vector<Employee> List)
    {

        int list_Size = List.size()-1;

        if(list_Size <= 0)
        {
            list_Size = 1;
        }
        for(int index =0; index<list_Size; index++)
        {
            output << List[index].username << " " << List[index].password << " " << List[index].is_Admin << std::endl;
        }

        return output;
    }
protected:
    std::string username;
    std::string password;
    bool is_Admin;
};


class Employee_Manager: public Employee
{
public:
    Employee_Manager(std::string fileName);
    void Print_List();
private:
    void add_Employee(Employee& employee);
    std::vector <Employee> Employee_List;

};



Employee_Manager::Employee_Manager(std::string fileName)
{
    std::ifstream myEmployeeFile;
    myEmployeeFile.open(fileName.c_str());
    if(!myEmployeeFile)
    {
        std::ofstream myFile;
        myFile.open(fileName.c_str());
        myFile.close();
    }
    myEmployeeFile.close();

    myEmployeeFile.open(fileName.c_str());
    if(myEmployeeFile.peek() == std::ifstream::traits_type::eof())
    {
        // file is empty fill it with data
        Employee employee;
        employee.set_Employee("admin", "0000", true);
        add_Employee(employee);
        myEmployeeFile.close();
        std::ofstream myFile;
        myFile.open(fileName.c_str());
        myFile << Employee_List;
        myFile.close();
    }
    else
    {
        std::string user;
        std::string pass;
        bool admin;
        while(myEmployeeFile.peek() != std::ifstream::traits_type::eof())
        {
            myEmployeeFile >> user >> pass >> admin;
            Employee employee;
            employee.set_Employee(user, pass, admin);
            add_Employee(employee);
        }
    }
    myEmployeeFile.close();
};



void Employee_Manager::add_Employee(Employee& employee)
{
    Employee_List.push_back(employee);
}

void Employee_Manager::Print_List()
{
    std::cout << Employee_List;
}
int main()
{
    Employee_Manager myEmployeeManager("database.dat");
    myEmployeeManager.Print_List();
    return 0;
}



Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3