Thread: got this running but...

  1. #1
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33

    got this running but...

    I just created this class and I've got it running in main however it never escapes the first function call. help.

    Code:
    //Class definition for personType
    #ifndef PERSON_TYPE_H
    #define PERSON_TYPE_H
    
    const int N_S = 20;
    const int D_S = 500;
    
    class personType
    {
    public:
    	personType();
    	void setName();
    	int setAge();
    	void setDescription();
    	~personType();
    private:
    	char* name;
    	int age;
    	char* description;
    
    };
    
    #endif 
    
    #include <iostream>
    #include "PersonType.h"
    
    using namespace std;
    
    personType::personType()//Default constructor
    {
    	name = NULL;
    	age = 0;
    	description = NULL;
    }
    
    
    void personType::setName()
    {
    	char temp[N_S];
    	
    	cout << "Please enter the person's name." << endl;
    	cout << ">>>";
    	for(int i = 0;i < N_S;i++ )
    		cin >> temp[i];
    
    	cout << endl;
    	return;
    }
    
    int personType::setAge()
    {
    	int temp;
    	cout << "Please enter the person's age." << endl;
    	cout << ">>>";
    	cin >> temp;
    	if(!cin)
    	{
    		cin.clear();
    		cin.ignore(100, '\n');
    		cout << "Invalid input.";
    	}
    	age = temp;
    	return 0;
    }
    
    void personType::setDescription()
    {
    	char temp[D_S];
    	
    	cout << "Please type the person's description." << endl;
    	cout << ">>>";
    	for(int i = 0;i < D_S;i++)
    		cin >> temp[i];
    	for(int j = 0; j < strlen(temp); j++)
    		description[j] = temp[j];
    	cout << endl;
    	return;
    }
    personType::~personType()
    {
    	delete []name;
    	delete []description;
    }
    
    #include <iostream>
    #include "PersonType.h"
    
    
    using namespace std;
    
    int main()
    {
    	personType person;
    	person.setName();
    	cout << " ";
    	person.setAge();
    	cout << " ";
    	person.setDescription();  
    
    	return 0;
    }
    Are unmarried people a kind of "Global Variable"?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It will, but first you need to type in N_S (20) nonwhitespace characters.

    The string handling is way off, anyway. If at all possible, use the std::string class and many problems will go away. setName does not even attempt to set the name member of the class, whereas setDescription tries to, but fails to allocate any memory for the description member.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    okay I'll look at the two string functions but I have to use C-strings for the upcoming class I'm taking. lost all my class code c++ basic and advanced in one big drive scrub.
    Are unmarried people a kind of "Global Variable"?

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. I will recommend you to read about accessors and mutators regarding classes. As setName() is only allowed to set the name of private member, so you should better get the name from main and pass it to that function, which will be the real object oriented approach.
    2. Use std::strings or std::vector instead.....
    3. If you don't know about these, you can also use cin.getline().
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  5. #5
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    Thank you, 777.
    Are unmarried people a kind of "Global Variable"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-25-2010, 09:23 AM
  2. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  3. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 11:08 AM
  4. Monitor a running instance of MS Word
    By BobS0327 in forum C# Programming
    Replies: 0
    Last Post: 07-18-2008, 12:40 PM
  5. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM