array problem.

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 #define  Icon User is offline

  • Duke of Err
  • member icon

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

Re: array problem.

Posted 08 July 2013 - 09:02 PM

You constructor is not correct you are using local variables of the same name as the ones already declared in the class. The local variables are updated not the class data. The functions of a class can access the data members of a class. So remove or comment out the following array declarations, as shown.

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]; 
}




Can you show us the complete code please?
'

This post has been edited by #define: 08 July 2013 - 09:05 PM

Was This Post Helpful? 0
  • +
  • -

#17 #define  Icon User is offline

  • Duke of Err
  • member icon

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

Re: array problem.

Posted 08 July 2013 - 09:09 PM

Do you know that you need to add Customer.cpp (or Company.cpp) to your project, to build your program?
Was This Post Helpful? 0
  • +
  • -

#18 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 - 09:17 PM

This is what I have so far. using vector.


main.cpp
#include <iostream>
#include "Company.h"
using namespace std;

int main()
{
	Company custList;
	
	//employees' first, last names and salary
	custList.AddEmployee("John", "Johnson", 50000.00);
	custList.AddEmployee("Bob", "Robertson", 65000.00);

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

	
	return 0;
}


Company.h
#include<string>
#include<vector>
using namespace std;

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

private:
	vector<string> fNames;
	vector<string> lNames;
	vector<double> salaries;
};


Company.cpp
#include "Company.h"
#include <iostream> 
#include <string>
using namespace std;

Company::Company()
{
	vector<string> fNames;
	vector<string> lNames;
	vector<double> salaries;
}

//Adds an employee to the internally maintained data
void Company::AddEmployee(string first, string last, double salary)
{
	fNames.push_back(first);  
	lNames.push_back(last);  
    salaries.push_back(salary);
}

//print out each of employees' data
void Company::PrintEmployeeInfo() 
{
	std::cout <<"FNAME  |  LNAME  |  SALARY"<< std::endl;  
	for(int i=0; i<fNames.size(); i++)  
	{  
	std::cout<<fNames[i]<<"     "<<lNames[i]<<"   $"<<salaries[i]<< std::endl;      
	}  
}

//print out employee data that's specified on index, in this case, 2nd employee.
void Company::PrintEmployeeInfo(int index) 
{
	std:cout<<""<<endl;
	std::cout <<"FNAME  |  LNAME  |  SALARY"<< std::endl;  
	std::cout<<fNames[1]<<"     "<<lNames[1]<<"   $"<<salaries[1]<< std::endl; 
}

Was This Post Helpful? 0
  • +
  • -

#19 #define  Icon User is offline

  • Duke of Err
  • member icon

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

Re: array problem.

Posted 08 July 2013 - 10:43 PM

Hi, it doesn't seem too bad.

You don't need to declare the vectors in your constructor because they are already declared in your class.

Company::Company()
{
  //vector<string> fNames;
  //vector<string> lNames;
  //vector<double> salaries;
}




In your first PrintEmployeeInfo you could use unsigned (int) instead of int to stop any warnings (it will still work with the warnings).

//print out each of employees' data
void Company::PrintEmployeeInfo() 
{
  std::cout <<"FNAME  |  LNAME  |  SALARY"<< std::endl;  
  // int
  for(unsigned i=0; i<fNames.size(); i++)
  {  
    std::cout << fNames[i] << "     "
              << lNames[i] <<"   $"
              << salaries[i]<< std::endl;
  }  
}




I don't know why you are struggling with the second PrintEmployeeInfo, I'll just show you a way to do it since you are having difficulty.
1) std::cout needs two colons.
2) When comparing the index value to a vector size, it can be cast to stop warnings - the program will compile without it, although you might wish to test index for less than zero.
3) The index value is passed as a parameter, the index corresponds to one element of the container (vector).

So you can use fNames[index] for example to access the element.

//print out employee data that's specified on index, in this case, 2nd employee.
void Company::PrintEmployeeInfo(int index)
{
  // ::
  std::cout<<""<<endl;

  if( static_cast< unsigned >(index) >= fNames.size())
  {
    cout << "index out of range" << endl;
  }
  else
  {
    // print one record
    std::cout <<"FNAME  |  LNAME  |  SALARY"<< std::endl;
    std::cout << fNames[index] << "     "
              << lNames[index] << "    $"
              << salaries[index] << std::endl;
  }
}



Are you able to run the program?
Was This Post Helpful? 0
  • +
  • -

#20 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 - 11:02 PM

Hi #define,

Yes I gave the OP the code for PrintEmployeeInfo() which included unsigned in post #4

However I had to draw the line about giving the OP the whole thing

as it would have been better if the OP had managed to do the last bit on their own

I have no idea why the OP could not achieve this but C'est La Vie.

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

#21 shin777  Icon User is offline

  • New D.I.C Head

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

Re: array problem.

Posted 09 July 2013 - 12:56 AM

thanks for help. it works. do i have to leave if-else though?
it doesn't look like it said I would need to use it.
Was This Post Helpful? 0
  • +
  • -

#22 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: array problem.

Posted 11 July 2013 - 01:32 AM

View Postshin777, on 09 July 2013 - 03:56 AM, said:

thanks for help. it works. do i have to leave if-else though?
it doesn't look like it said I would need to use it.


It always good to design your code to be 'crash proof' on 'invalid user input' ...

What if the user were to enter an 'index out of valid range' ?



Also ...

your program design spec's, using 3 parallel vectors, seems some-what odd, to begin with :)

A data struct or class ( a C++ struct or class ) ... would (commonly) hold all three of your data elements

Here is an example you might like to keep for future ref...
(of how to use a vector of class objects / or struct's ) ...

// classCountry_vector.cpp //

#include <iostream>
#include <sstream> // re. istringstream obj...
#include <string>
#include <vector>

#include <cctype> // re. tolower

using namespace std;

// 3 utility functions follow that are used here ...

string getString( const string& msg )
{
    cout << msg << flush;
    string tmp;
    getline( cin, tmp );
    return tmp;
}

char takeInChar( const string& msg )
{
    cout << msg << flush;
    string reply;
    getline( cin, reply );
    if( reply.size() ) return reply[0];
    // else ...
    return 0;
}

bool more()
{
    if( tolower( takeInChar( "\nMore (y/n) ? " )) == 'n' ) return false;
    // else ...
    return true;
}




class Country
{
public:
    // handles both default and value ctor's ...
    Country( string cap = "", string pri = "",unsigned pop = 0 )
        : capital( cap ), primaryLang( pri ), population( pop ) {}

    void takeIn();
    void show() const ;

private:
    string capital;
    string primaryLang;
    unsigned population;

    friend ostream& operator << ( ostream& os, const Country& c );
} ;

// friend function def'n ...
ostream& operator << ( ostream& os, const Country& c )
{
    os << c.capital << ", " << c.primaryLang << ", " << c.population;
    return os;
}

void Country::takeIn()
{
    string tmp;
    for( ; ; ) // loop forever ... until 'break'
    {
        capital = getString( "Enter the capital: " );
        primaryLang = getString( "Enter the primary language: " );

        tmp = getString( "Enter the population (of country): " );
        istringstream iss( tmp ); // construct 'iss' object from 'tmp'
        iss >> population; // get unsigned value (or 0 if bad data input)

        cout << "You entered: " << *this << endl; // show 'this' object

        if( tolower( takeInChar( "Ok (y/n) ? " )) == 'y' ) break;

        cout << "\nData was NOT accepted ... here we go again ... \n";
    }
}


void Country::show() const
{
    cout << capital << ", " << primaryLang << ", " << population;
}





int main()
{
    // construct an empty vector container to hold Country obj's
    vector< Country > myVecOfCountries;
    
    Country tmp;
    
    cout << "Enter city and info for countries you wish to visit: \n";
    do
    {
        tmp.takeIn();
        myVecOfCountries.push_back( tmp );
    }
    while( more() );
         

    cout << "\nshowing back method 1 ... \n";
    for( size_t i = 0; i < myVecOfCountries.size(); ++i )
         cout << myVecOfCountries[i] << endl;

    cout << "\nshowing back method 2 ... \n";
    for( size_t i = 0; i < myVecOfCountries.size(); ++ i )
    { myVecOfCountries[i].show(); cout << endl; }
}


This post has been edited by David W: 11 July 2013 - 01:35 AM

Was This Post Helpful? 0
  • +
  • -

#23 shin777  Icon User is offline

  • New D.I.C Head

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

Re: array problem.

Posted 11 July 2013 - 03:30 AM

Thank you. I will keep that in mind. This is first time for me to take any programming class so it's very hard for me and get used to any concept. I know I will eventually get better but well.. at this point, I am just horrible. Hopefully, I get a lot better in next few months. :)
Was This Post Helpful? 0
  • +
  • -

#24 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: array problem.

Posted 11 July 2013 - 04:00 AM

View Postshin777, on 11 July 2013 - 06:30 AM, said:

Thank you. I will keep that in mind. This is first time for me to take any programming class so it's very hard for me and get used to any concept. I know I will eventually get better but well.. at this point, I am just horrible. Hopefully, I get a lot better in next few months. :)


There is a fairly steep learning curve at the beginning ... until you start to get a little comfortable with the basics.

There is NO short-cut to writing code ... even if you see a lot of great example code.

Take your program ... or the one I sent ...and add to it.

Write the data to file.

Read back the data from file.

Sort the data (on various fields in the data record).

Search for a record in the vector of data.

Edit a record in the vector of data.

Delete a record in the vector of data ...


When all the above are fairly straight-forward for you to code ... you will have gained a good start :)

This post has been edited by David W: 11 July 2013 - 04:06 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2