array problem.

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1327 Views - Last Post: 11 July 2013 - 04:00 AM Rate Topic: -----

#1 shin777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-June 13

array problem.

Posted 07 July 2013 - 02:32 AM

I've been on this for hours now and can't figure out how to do it. I couldn't find anything similar on books or class notes. Can you please help me on this? I have to use array. Here's instruction.
Posted Image

Customer.h
#include<string>
#include<array>
using namespace std;

class Company
{
public:
	Customer();
	void AddEmployee(string first, string last, double salary);
	void PrintEmployeeInfo();
	void PrintEmployeeInfo(int index);

private:
	array<string, 2> fName;
	array<string, 2> lName;
	array<double, 2> salaries;
	array<int, 2> employee;
};


Main.cpp
#include <iostream>
#include "Customer.h"

using namespace std;

int main()
{
	Customer custList;
	
	custList.AddEmployee("John", "Johnson", 50000.00);
	custList.AddEmployee("Bob", "Robertson", 65000.00);

	custList.PrintEmployeeInfo();
	custList.PrintEmployeeInfo(1);

	
	return 0;
}



Customer.cpp
#include "Customer.h"
#include <iostream>  //for printing
#include <array>
using namespace std;

Customer::Customer()
{
	array<string, 2> fName;

	fName[0] = "John";
	fName[1] = "Bob";

	array<string, 2> lName;

	lName[0] = "Johnson";
	lName[1] = "Robertson";

	array<double, 2> salaries;
	salaries[0] = 50000;
	salaries[1] = 65000;

	array<int, 2> employee;
	employee[0];
	employee[1]; 
}

void Customer::AddEmployee(string first, string last, double salary)
{
	employee[0] = (fName[0], lName[0], salaries[0]);
	employee[1] = (fName[1], lName[1], salaries[1]);
}

void Customer::PrintEmployeeInfo()
{
	cout<<endl;
}

void Customer::PrintEmployeeInfo(int index)
{

}



Ok.. I used Customer instead of Company on this code and on my Customer.h, class is Customer not Company. There's no edit button on forum so I couldn't fix it. :(

This is as far as I could get. I am completely stuck now.

Is This A Good Question/Topic? 0
  • +

Replies To: array problem.

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: array problem.

Posted 07 July 2013 - 02:58 AM

I think you are getting yourself a bit confused,

I think you should go back to your book and read up on classes...

and how to overload member functions from a base class...

you would probably be better off using vectors as well...

then your

void Company::AddEmployee(string first, string last, double salary)

could look like

void Company::AddEmployee(string first, string last, double salary)
{
   MyEmployeeFNameVector.push_back(first);

   MyEmployeeLNameVector.push_back(last);
   
   MyEmployeeSalaryVector.push_back(salary);
}



That will keep an acurate record of all the data and make things simpler.

Best Wishes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 shin777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-June 13

Re: array problem.

Posted 07 July 2013 - 03:27 AM

ok.. but how do you make expected result print out on here?
void Customer::PrintEmployeeInfo()
{
	
}

void Customer::PrintEmployeeInfo(int index)
{

}


so far, i get weird numbers or errors when I use cout<<AddEmployee blah blah<<endl;
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: array problem.

Posted 07 July 2013 - 06:28 AM

Well,

First of all PrintEmployeeInfo()

should print all employee's Info

so

void Company::PrintEmployeeInfo()
{
  std::cout << "FNAME | LNAME      | SALARY"<< std::endl;
  std::cout << std::endl;
  for(unsigned int i=0; i<MyEmployeeFNameVector.size(); i++)
  {
    std::cout << MyEmployeeFNameVector[i] << MyEmployeeLNameVector[i] <<        
    MyEmployeeSalaryVector[i] << std::endl;
  }
}



Then you want an overloaded version of this function which takes an int in the argument list and returns that vector associated with that int.

Regards

Snoopy.

This post has been edited by snoopy11: 07 July 2013 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 shin777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-June 13

Re: array problem.

Posted 07 July 2013 - 11:27 AM

^ Thank you. I think I got up till now. except for last part. How do you make it print only one of choice in
void Customer::PrintEmployeeInfo(int index)
{

}



:(
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: array problem.

Posted 07 July 2013 - 11:54 AM

Oh for goodness sake,

I am not going to do the whole assignment for you..

Think , Look at the function I gave you which prints all the available employee information,

and then think how you can go from printing all the info to printing one set

Time for you to do some work on your own.

Snoopy.
Was This Post Helpful? 3
  • +
  • -

#7 shin777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-June 13

Re: array problem.

Posted 07 July 2013 - 02:45 PM

void Company::PrintEmployeeInfo(int index)
{
	std::cout <<"FNAME  |  LNAME  |  SALARY"<< std::endl;  
	for(int i=1; i < 2; i++)  
	{  
	std::cout<<fNames[i]<<"     "<<lNames[i]<<"   $"<<salaries[i]<< std::endl;      
	}  
}


this is how i did. as u can see, if i set (int i = 1; i < 2; i++) it only prints out index 2 and if set it to (int = 0; i < 1; i++) it prints out index 1 only.
Does it look right? Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: array problem.

Posted 07 July 2013 - 11:30 PM

No,

It does not look right...

What you want to do is print out a single index of the vector

now what could you use as an index I wonder ??


Snoopy.
Was This Post Helpful? 0
  • +
  • -

#9 shin777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-June 13

Re: array problem.

Posted 08 July 2013 - 05:39 PM

that's what i was wondering.
all the examples we did in class was with simple ones like this
#include<iostream>
#include<array>
using namespace std;

int main()
{
	array<int, 4> myArray;

	for(size_t i = 0; i < myArray.size(); i++)
	{
		myArray[i] = i*3;
	}

	for(int item : myArray)
	{
		cout<<item<<endl;
	}
	
	return 0;
}



and this was the only thing that looks anything similar to this problem. It is like they teach me 5+5=10 and ask me how to 5^25 without calculator on exam. :(
I went over every single notes, lecture material and couldn't find single one that might help me. :(
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: array problem.

Posted 08 July 2013 - 05:53 PM

Ok,

Please review the following link

http://www.cplusplus...or/vector/data/

maybe it will give you ideas...

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,735
  • Joined: 19-February 09

Re: array problem.

Posted 08 July 2013 - 06:18 PM

Instead of the for loop you can access the elements of the array like so :

#include<iostream>
#include<array>
using namespace std;

int main()
{
  array<int, 4> myArray;

  // instead of for loop
  // manually use index number
  myArray[0] = 0*3;
  myArray[1] = 1*3;
  myArray[2] = 2*3;
  myArray[3] = 3*3;

  // print array elements
  cout << myArray[0] <<endl;
  cout << myArray[1] <<endl;
  cout << myArray[2] <<endl;
  cout << myArray[3] <<endl;
	
  return 0;
}



Arrays are basically multiple elements of a data type next to each other in memory, that can be accessed using the square brackets and an index number.

Similar array tutorial

Arrays

'

This post has been edited by #define: 08 July 2013 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#12 shin777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-June 13

Re: array problem.

Posted 08 July 2013 - 07:36 PM

yeah.. but all these examples are just out of single page, not 3 page ones where I have to go back and forth between main.cpp, customer.h, customer.cpp

if I do it all on single main.cpp, i understand, but it's really confusing me when i go back and forth. I am just not getting this at all.
Was This Post Helpful? 0
  • +
  • -

#13 shin777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-June 13

Re: array problem.

Posted 08 July 2013 - 07:43 PM

I think I should add something to
void Company::AddEmployee(string first, string last, double salary)
{
	fNames.push_back(first);  
	lNames.push_back(last);  
    salaries.push_back(salary);
}


that part there to make last part work. It says AddEmployee(string first, string last, double salary)
but data in there are 3 seperate ones not in single ( ).
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: array problem.

Posted 08 July 2013 - 08:21 PM

No No No....

Dont add anything

Look you are given the prototype....

void Company::PrintEmployeeInfo(int index)

all we are saying is use index inside the square brackets no
for loops are needed for this..... just index

I dont know how to make it any clearer.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#15 shin777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-June 13

Re: array problem.

Posted 08 July 2013 - 08:36 PM

void Company::PrintEmployeeInfo(int index) 
{
	std:cout<<""<<endl;
	std::cout <<"FNAME  |  LNAME  |  SALARY"<< std::endl;  
	std::cout<<fNames[1]<<endl;
}


obviously above code only print out first name of 2nd employee.
what I don't get is, do is there way to print out whole thing in one go or do I have to make it like

std::cout<<fNames[1]<<lNames[1]<<salaries[1]<<endl;

to print out the whole thing? Is that what question is asking?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2