Beginner problem

Discussion in 'C++' started by SparkyCH, Oct 18, 2006.

  1. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Okay what i want to do is:
    Code the implementation of a Person class (2 person object are equal if they have the same name)
    (b) Code the implementation of a Worker class
    (c) The program must read in the name, age and workplace (comma delimited) from the console in to a Worker object and store in a vector.
    What i have done so far is:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Person
    {
    public:
    	string n;
    public:
    	Person(string n)
    	{
    		this->n = n;
    	}
    	bool operator==(const Person& obj) const;
    	Person();
    
    };
    bool Person::operator ==(const Person& obj) const
    {
    	if(obj.n != this->n)
    	{
    		return false;
    	}
    	else
    	{
    		return true;
    	}
    }
    class Worker
    {
    public:
    	void setInfo(string, int, string);
    	void getInfo(string&, int&, string&) const;
    	Worker(string, int, string);
    	Worker();
    
    private:
    	string name;
    	int age;
    	string workPlace;
    };
    I am completely lost on part b and c i dont know what to do. ANy help gr app. Thanks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    Can you point as to where you are stuck because its very difficult to go through the entire code.
     
  3. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    I have managed to figure out what i needed i would like to know how would i read a files values into my program that are comma deliminated. Like:
    "David",23,"XPlace"
    And does it matter if there are spaces when you reading in the information
    ANy help greatly appreciated,
     
  4. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Well i got most of everything working with a little work. Like this:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    
    using namespace std;
    
    class Person
    {
    protected: string o_name;
    
    public:
    	Person(string name)
    	{
    		this->o_name = name;
    	}
    	bool operator==(const Person& obj) const;
    	bool operator!=(const Person& obj) const;
    };
    
    bool Person::operator ==(const Person& obj) const
    {
    	return (obj.o_name == this->o_name);
    }
    bool Person::operator !=(const Person &obj) const
    {
    	return (obj.o_name != this->o_name);
    }
    
    
    class Worker: public Person
    {
    friend ostream& operator<<(ostream&, const Worker &); 
    friend istream& operator>>(istream&, Worker &); 
    
    private:
    	string workPlace;
    	int age;
    public:
    	Worker(string name, string w_wP, int w_a): Person(name)
    	{
    		workPlace = w_wP;
    		age = w_a;
    	}
    	string getName() const;
    	string getWorkPlace() const;
    	int getAge() const;
    	void print() const;
    };
    string Worker::getName() const
    {
    	return o_name;
    }
    string Worker::getWorkPlace() const
    {
    	return workPlace;
    }
    int Worker::getAge() const
    {
    	return age;
    }
    void Worker::print() const
    {
    	cout << "Name = " << o_name
    		<< "; WorkPlace = " << workPlace
    		<< "; Age = " << age;
    }
    ostream& operator<<(ostream& osObject, const Worker& worker) 
    { 
       osObject <<"Name = " << worker.o_name << "; Work Place = " << worker.workPlace << "; Age = " << worker.age; 
       return osObject; 
    } 
    istream& operator>>(istream& isObject, Worker& worker) 
    { 
       isObject >> worker.o_name >> worker.workPlace >> worker.age; 
       return isObject; 
    } 
    
    
    int main()
    {
    	string text = "", text1 = "";
    	int count = 0;
    
    	ifstream in;
    	ofstream out;
    
    	in.open("one.txt");
    	out.open("two.txt");
    	
    
    	string w_name, w_name2;
    	string w_workPlace, w_workPlace2;
    	int w_age, w_age2;
    	char ch;
    
    	cout << "Enter name, work place and age, comma seperated : ";
    	getline(cin, w_name, ',');
    	getline(cin, w_workPlace, ',');
    	cin >> w_age;
    
    	cin.get(ch);
    
    	cout << "Enter name, work place and age, comma seperated : ";
    	getline(cin, w_name2, ',');
    	getline(cin, w_workPlace2, ',');
    	cin >> w_age2;
    
    	cin.get(ch); 
    
    
    	Worker w1(w_name, w_workPlace, w_age);
    	Worker w2(w_name2, w_workPlace2, w_age2);
    
    	if(w1 == w2)
    	{
    		cout << "They have the same name" << endl;
    	}
    	else if(w1 != w2)
    	{
    		cout << "They do not have the same name" << endl;
    	}
    	vector<Worker> w;
    	w.push_back(w1);
    	w.push_back(w2);
    	cout << "Size of vector : " << w.size() << endl;
    	w1.print();
    	cout << endl;
    	cout << w1 << endl;
    	
    	cout << "First element : " << w.front() << endl;
    	cout << "Last element : " << w.back() << endl;
    
    	
    	in.close();
    	out.close();	
    
    	return 0;
    }
    Does anyone know how i can enter stuff from a file (problem being, the stuff in the folder will be comma deliminated) coz i have to store it in a worker object then store in a vector once i have read it in. ANy help gret appre
     
    Last edited: Oct 21, 2006
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    ifstream in;
    in.read(buf,Num);
    Where buffer is where the content is read and num is the no of bytes read. So if I am getting you correctly this should read it from the file.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    I probably missed on this post. If you read it using >> operator. Try using getline.
     
  7. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    sorry but i dont understand im new, but how will that read infomration comma sperated it does not work for me
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    Why it shouldn't. Here is the sample code
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    #define N 100
    
    int main()
    {
    	char buf[N];
    
    	ifstream in;
    
    	in.open("a.txt");
    	in.getline(buf,N);
    
    	cout<<buf << endl;
    	
    	return 0;
    }
     
  9. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    no i get that its just i want to use the data to store in an object.
    like :
    "darrel","xWork",34
    Then store darrel xWork and 34 in the object
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    After you read it into buf convert it to whatever object you wish.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice