simple constructor

This is a discussion on simple constructor within the C++ Programming forums, part of the General Programming Boards category; guys, i am confused as how to write a constructor. all i want to do is the following: FlatElement::FlatElement(void) { ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    64

    simple constructor

    guys,
    i am confused as how to write a constructor. all i want to do is the following:

    FlatElement::FlatElement(void)
    {
    what do i put in this constructor

    }

    FlatElement::FlatElement(char, char *, int)
    {
    and how do i assign symbol to char
    name to char*
    size to int
    }
    i guess this is simple stuff, i just cna't get it!??!?!

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    there's nothing really that unusual that you need to know to do these things. Just do whatever assignments you need to do in there and trust that it will be called when the object is created.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    it isn't tho...whenever i call the stuff in another function i am getting hideous numbers instead of values.

    i need to assign the (char, *char, int)
    to symbol, name, and size

    how do i do this?

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    post some code, it's not likely because it's a constructor. It's probably because you did the assignment wrong.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    FlatElement::FlatElement(void)
    {

    not sure what to put here?
    }

    FlatElement::FlatElement (char sym, char * descr, int number)
    {
    symbol = sym;
    name = descr;
    relativeSize = number;
    }

    void Pile::Input(void)
    {


    string stuff;
    cout<<"Please enter the sequence of characters: ";
    cin>>stuff;

    const int COIN_COUNT = 6;
    const FlatElement COIN[6]={FlatElement('d', "Dime", 1),
    FlatElement('p', "Penny", 2), FlatElement('n', "Nickel", 3),
    FlatElement('q', "Quarter", 4), FlatElement('h',"Half-dollar", 5) ,
    FlatElement('$', "Dollar", 6)};


    int j = 0;
    for(int count=0;count<stuff.size();count++)
    {
    if(stuff[count]==COIN[count].symbol)
    {
    store[count].name= COIN[j].name;
    store[count].relativeSize= COIN[j].relativeSize;
    store[count].symbol= COIN[j].symbol;
    }

    }
    for(count=0;count<stuff.size();count++)
    cout<< store[count].name << store[count].symbol << store[count].relativeSize<<endl;


    }

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    which value are you getting garbage data for? I would suspect "name" because you are assigning it a constant character pointer. das bad. You need to copy the string into a member string, make "name" a char array and copy it in.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    i am new to C++...can you rephrase that ? thanks so much.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    here, wait...more code. this may help.

    class Pile
    {
    private:
    FlatElement store[100];
    int size;
    public:
    Pile (void);
    void Input(void);
    }

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    name is a char * I assume

    the value you passed in is a constant char array ie: "Hi There" meaning you can't count on this memory the way you are doing. You declared it in a function and that's where it lives, in that function.

    if name where a string:

    char name[64];

    you could copy it over in the constructor:

    strcpy(name, descr);

    and you would have no problem.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    i don't doubt that what you are saying is wrong, but the impression i got with this program is that whatever is inputed into stuff...will be sent over to name. and that data will just be assigned to whatever symbol it is. and the name will be outputted. that is what it is supposed to do? does that help?

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    can you zip the project and attach it? It's much easier to work with that way.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    yeah, cause this one is giving me a headache.... i appreciate it.

    overall, the program takes a string of characters and orders the pile based on size. it can NOT use sorting. you'll see the functions. a lot of my code is wrong and it's only because i am clueless as to what to do.
    thanks, josh
    Attached Files Attached Files

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I'm finding this to be a bad logic problem
    Code:
    for(int count=0;count<stuff.size();count++)
    	{
    		if(stuff[count]==COIN[count].symbol)
    			{
    				store[count].name= COIN[j].name;
    				store[count].relativeSize= COIN[j].relativeSize;
    				store[count].symbol= COIN[j].symbol;
    			}
    
    	}
    This code counts until it finds the right coin. unfortunately "count" is being used as the stuff array index. This means you will have results scattered throughout that array and not in the first few spots as you might expect. get it?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    then what would you suggest? i do follow what you are saying...

    it is supposed to take the input...and assign each character to an element.

  15. #15
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    //this part seems right, counting through the array
    for(int count=0;count<stuff.size();count++)
    	{
    	//let's add a loop for counting through the coin types
    	for(int j=0; j < COIN_COUNT; j++)
    		{
    		if(stuff[count]==COIN[j].symbol)
    			{
    				store[count].name= COIN[j].name;
    				store[count].relativeSize= COIN[j].relativeSize;
    				store[count].symbol= COIN[j].symbol;
    			}
    		}
    
    	}
    this might work, I just ran through it but give it a shot
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Constructor for a class
    By Universe in forum C++ Programming
    Replies: 6
    Last Post: 08-06-2002, 11:31 AM
  4. Arguments up constructor tree?
    By Imperito in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 01:03 AM
  5. Quick ? on copy constructor
    By Traveller in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2002, 11:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21