-
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!??!?!
-
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.
-
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?
-
post some code, it's not likely because it's a constructor. It's probably because you did the assignment wrong.
-
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;
}
-
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.
-
i am new to C++...can you rephrase that ? thanks so much.
-
here, wait...more code. this may help.
class Pile
{
private:
FlatElement store[100];
int size;
public:
Pile (void);
void Input(void);
}
-
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.
-
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?
-
can you zip the project and attach it? It's much easier to work with that way.
-
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
-
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?
-
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.
-
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