Thread: Now whats wrong with this thing...

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

    Now whats wrong with this thing...

    Have this working a little better but, as before, I don't understand why it freezes after the first function call...Help, Please.

    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();//default constructor
    	personType(char*, int, char*);//constructor
    	void setName(char* aName);
    	int setAge(int anAge);
    	void setDescription(char* aDescription);
    	~personType();
    private:
    	char* name;
    	int age;
    	char* description;
    
    };
    
    #endif 
    
    #include <iostream>
    #include "PersonType.h"
    
    using namespace std;
    
    personType::personType()
    {	
    	name = NULL;
    	age = 0;
    	description = NULL;
    }
    
    personType::personType(char* name, int age, char* description)//Constructor with parameters
    {
    	setName(name);
    	setAge(age);
    	setDescription(description);
    
    }
    
    
    void personType::setName(char* aName)
    {
    	char temp[N_S];
    	
    	cout << "Please enter the person's name." << endl;
    	cout << ">>>";
    	for(int i = 0;i < N_S;i++ )
    	{
    		cin >> temp[i];
    		break;
    	}
    	for(int j = 0; j < strlen(temp); j++)
    	{
    		description[j] = temp[j];
    		break;
    	}
    	cout << endl;
    	return;
    }
    
    int personType::setAge(int anAge)
    {
    	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* aDescription)
    {
    	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;
    
    const int L_S = 20;
    
    int main()
    {
    	int i = 0;
    
    	char tempN[N_S];
    	int tempA;
    	char tempD[D_S];
    
    	personType person;
    	personType personList[L_S];
    
    	person.setName(tempN);
    	person.setAge(tempA);
    	person.setDescription(tempD);
    	
    	personList[i] = person;
    
    
    	return 0;
    }
    Are unmarried people a kind of "Global Variable"?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    1. personType::setName takes char* argument but does nothing with it
    2. tempN and tempA in main() are not initialized
    3. char represents a SINGLE character, not a string
    4. use std::string, your whole program needs to be rewritten

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So, so many things...

    Code:
    void personType::setName(char* aName)
    {
    	char temp[N_S];
    	
    	cout << "Please enter the person's name." << endl;
    	cout << ">>>";
    	for(int i = 0;i < N_S;i++ )
    	{
    		cin >> temp[i];
    		break;
    	}
    	for(int j = 0; j < strlen(temp); j++)
    	{
    		description[j] = temp[j];
    		break;
    	}
    	cout << endl;
    	return;
    }
    You never allocate memory for your member variables, even though you delete the non-allocated memory in the destructor.
    Why are you reading character-by-character instead of using a stdio function like fgets? (Or better yet, use std::string as suggested by kmdv)
    Why are you breaking out of this silly loop after a single character entry?
    You are not null-terminating the temp variable, which will likely cause problems when you attempt to use a string function like strlen on it. You don't even have room for the null terminator if the person enters N_S (stupid name) characters...and you fix the loop so you don't break out after the first character.
    Why are you setting the description in the setName() method?

  4. #4
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    Have to get used to using c strings for an upcoming class (string not allowed). As for the basic "oops, I didn't see that" stuff, thanks. For the memory allocation...thank you.

    as for my creative aptitude, I've learned that condensing variable names is economical so...
    N_S = NAME_SIZE
    D_S = DESCRIPTION_SIZE
    L_S = LIST_SIZE.....
    Those poor variable names it's not their fault. If you can't say anything nice to a variable then don't say anything at all...
    Are unmarried people a kind of "Global Variable"?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You should always write your programs with an eye toward thinking about the next person who has to read it and figure out how it works. Variable naming helps tons to that end.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Firstly, "length" is better than "size" (size might not be equal to length).
    Macros cannot be scoped and naming them correctly is important (you should even use prefixes).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  2. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  3. What am I doing wrong ?
    By scottjge in forum C Programming
    Replies: 6
    Last Post: 10-01-2009, 11:55 AM
  4. thing
    By twomers in forum C++ Programming
    Replies: 17
    Last Post: 05-01-2006, 10:50 PM
  5. I think somethings wrong
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 06-10-2002, 05:28 PM