🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

more pointers

Started by
20 comments, last by pbivens67 1 year, 2 months ago

Why use pointers? Do you know the first three normal forms of database implementations? You can use an ID variable, which “points” to a certain index in the Owners, etc vector. There is no need for actual pointers, right?

Advertisement

quou said:
You're passing a string to your Car, but not the pointers to people.

how do I pass the pointer to people

well with alberth's help I figured out my problem. thanks for all the help.

one quick question, the owner and driver variables only print out the addresses not the values as I want them to do.

	vector<Person*> person = { new Person(55,"Phil"), new Person(61,"Sylvia"), new Person(65,"Jeff") };
	vector<Car*> car = {new Car(("F-100"),new Person(57,"Phil"),new Person(59,"Jeff"))};
	for (const Person* p : person) {
		cout << p->age << " " << p->name << "\n";
	}
	for (const Car* p : car) {
		cout << p->model << " " << (p)->owner << " " << (p)->driver << "\n";
	}

pbivens67 said:
one quick question, the owner and driver variables only print out the addresses not the values as I want them to do.

You are again passing a pointer to the << operator. “owner” is a variable of type Person*, which is a pointer. If you pass a pointer to cout, it prints the address of the element. You need to deference it - meaning the “owner” and “driver” variable., not only the “Person* p” in the loop.

for (const Car* p : car) {
		cout << p->model << " " << *(p->owner) << " " << *(p->driver) << "\n";
	}

Then you need to overload the << operator for those types, but I'm assuming you should have done so. I've also added brackets for clarity to what the * applies, but it wouldn't be strictly necessary.

pbivens67 said:

one quick question, the owner and driver variables only print out the addresses not the values as I want them to do.

	vector<Person*> person = { new Person(55,"Phil"), new Person(61,"Sylvia"), new Person(65,"Jeff") };
	vector<Car*> car = {new Car(("F-100"),new Person(57,"Phil"),new Person(59,"Jeff"))};
	for (const Person* p : person) {
		cout << p->age << " " << p->name << "\n";
	}
	for (const Car* p : car) {
		cout << p->model << " " << (p)->owner << " " << (p)->driver << "\n";
	}

In the person vector loop, when you print p→age and p→name, it prints the actual data (values), which is an int and (indirectly) a string values.

in the car vector loop, when you print the p→model, it works exactly like when you print p→name.

However, looking at your Car class, owner and driver are both pointers to a Person value, not the actual value itself. Pointers are integers that store addresses. In your case, owner and driver variable store the addresses for Person (57, “Phil”) and Person (59, “Jeff”). That's why when you print p→owner and p→driver from a car, they print the addresses of Person object instead of the actual values of what Person supposed to have.

Now, you don't say what values as what you want them to do, but I assume you want access to the owner's/driver's age/name. In that case, you print p→owner→age and/or p→owner→name, same goes to driver.

Edit: 8 minutes late (more or less the same as what Juliean already answered), hopefully helps anyway.

that worked, thanks xrbtrx, I am learning a lot about pointers

how do I use the delete operator with the new operator? here is the new code I am using.

#include <iostream>
#include <string>
#include<vector>

using namespace std;

class BankAccount
{
public:
	BankAccount(string p_first_name, string p_last_name, double p_deposit, double p_balance)
	{
		first_name = p_first_name;
		last_name = p_last_name;
		deposit = p_deposit;
		balance = p_balance;
	}
	string first_name;
	string last_name;
	double deposit;
	double balance;
};

class Employee
{
public:
	Employee(string p_first_employee, string p_last_employee, double p_salary) {}
	BankAccount* first_employee;
	BankAccount* last_employee;
	BankAccount* salary;
};

int main()
{
	int count = 0;
	int bal[10] = {0};
	string last_name[10] = { 0 };
	char quit = NULL;
	string first_employee;
	string last_employee;
	double salary, deposit = 0, balance = 0;
	while (quit != 'y')
	{
		cout << "First Employee Name: ";
		cin >> first_employee;
		cout << endl;

		cout << "Last Employee Name: ";
		cin >> last_employee;
		cout << endl;

		last_name[count] = last_employee;

		cout << "Salary: ";
		cin >> salary;
		cout << endl;

		deposit = salary / 12.0;
		balance += deposit;

		vector<Employee> emp = { Employee(first_employee, last_employee, salary) };

		vector<BankAccount*> bank_acct = { new BankAccount(first_employee,last_employee,deposit,balance) };

		for (const BankAccount* p : bank_acct)
		{
			cout << p->first_name << " " << p->last_name << " " << p->balance << "\n\n";
		}

		cout << "Quit(y/n): ";
		cin >> quit;
		cout << endl;
	}
	
	return 0;
}

This looks very suspect:

class Employee
{
public:
	Employee(string p_first_employee, string p_last_employee, double p_salary) {}
	BankAccount* first_employee;
	BankAccount* last_employee;
	BankAccount* salary;
};

`first_employee` and `last_employee` should probably of type `std::string`, and `salary` of type `double` or some other numeric type.

You don't have a need for pointers in this code, as far as I can see.

This is also not quite right:

char quit = NULL;

`NULL` is probably a macro for “0” and this works, but it is supposed to be used for pointers.

I just checked with my compiler: Using `NULL` in that line produces a warning, and `nullptr` produces an error.

You should probably just use `0` instead of `NULL`. You can also explicitly write 0 with type char like this:

char quit = '\0';

There are other things that are a bit of a mess. I don't know where `[10]` came from; You never update `count`. And many others.

If you want, add some comments describing what you want to do and I'll help you write reasonable code for that.

EDIT: Oh, I missed the error here:

string last_name[10] = { 0 };

That makes no sense and won't compile.

I want to traverse a vector of objects using the for loop, but it only outputs the last value pushed back.

#include <iostream>
#include <string>
#include<vector>

using namespace std;

class BankAccount
{
public:
	BankAccount(string p_first_name, string p_last_name, double p_deposit, double p_balance)
	{
		first_name = p_first_name;
		last_name = p_last_name;
		deposit = p_deposit;
		balance = p_balance;
	}
	string first_name;
	string last_name;
	double deposit;
	double balance;
};

class Employee
{
public:
	Employee(string p_first_employee, string p_last_employee, double p_balance) {}
	BankAccount* first_employee;
	BankAccount* last_employee;
	BankAccount* salary;
	BankAccount* balance;
};

int main()
{
	int count = 0;
	double bal[10] = {0};
	string s_last_name[10];
	char quit = NULL;
	string first_employee;
	string last_employee;
	double salary, deposit = 0, balance = 0;
	vector<Employee> emp;
	while (quit != 'y')
	{
		cout << "First Employee Name: ";
		cin >> first_employee;
		cout << endl;

		cout << "Last Employee Name: ";
		cin >> last_employee;
		cout << endl;

		s_last_name[count] = last_employee;

		cout << "Salary: ";
		cin >> salary;
		cout << endl;

		deposit = salary / 12.0;
		balance += deposit;
		bal[count] = balance;

		emp.push_back(Employee(first_employee, last_employee, balance));

		vector<BankAccount*> bank_acct = { new BankAccount(first_employee,last_employee,deposit,balance) };

		for (const BankAccount* p : bank_acct)
		{
			cout << p->first_name << " " << p->last_name << " " << p->balance << "\n\n";
		}

		count++;

		cout << "Quit(y/n): ";
		cin >> quit;
		cout << endl;
	}

	for (const Employee p : emp)
	{
		cout << first_employee << " " << last_employee << " " << balance << endl;
	}

	if (s_last_name[count] == s_last_name[count++])
	{
		balance = bal[count-=2];
	}
	return 0;
}

This topic is closed to new replies.

Advertisement