Need C++ help: pointing to an object?

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
I'm trying to find the length of a number to make sure it isn't >5 or <3. I tried

unsigned int *ptrIDNumber = objEmp1.iIDNumber;
unsigned int iIDLength = strlen(ptrIDNumber);

but I get a syntax error saying I can't convert 'unsigned int' to 'unsigned int*'. I also get another syntax error that says I can't convert parameter 1 from 'unsigned int *' to 'const char*'. I don't understand this and I was hoping someone could help me out.
-Nut
 

Spydermag68

Platinum Member
Apr 5, 2002
2,616
99
91
class Money
{
public:
// Constructors
Money( void ); //Initializes to $0.00
Money( float value ); //Initializes to value
Money( long dollars, long cents = 0,
bool negative = false ); //Initializes to dollars.cents
Money( const Money& copy ); //Makes a copy


private:

long pennies;
};

void main()
{
Money greenback1;
int i = sizeof(greenbback1);
std::cout << "Size of GreenBack1: " << i << std::endl;
}


Is this what you are looking for? This example returns the size of my Money class which is the size of a long.
 

GL

Diamond Member
Oct 9, 1999
4,547
0
0
In that first line of code, you are creating a pointer but not giving that pointer an address. It appears objEmp1.iIDNumber is an unsigned integer but you need to set the pointer to the memory address of objEmp1.iIDNumber.

In the second line of code, the problem is that strlen() is a function which takes a parameter of type const *char - otherwise known as a C-style string. objEmp1.iIDNumber is not of type const *char and so you must first convert it to that type using some other function which is very likely found in the header file <string.h>.

C++ can get very confusing at times, so it's good to have a good fundamentals overview at hand while coding. Here's a decent one you can check out that will explain in detail why you can't do what you'd like to do: C++ Tutorial
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
unsigned int *ptrIDNumber = objEmp1.iIDNumber;

ptrIDNumber is a pointer to an integer whereas objEmp1.iIDNumber is (from the looks of it) an integer. The correct syntax would be:

unsigned int *ptrIDNumber = &objEmp1.iIDNumber;


unsigned int iIDLength = strlen(ptrIDNumber);
strlen is used to find the length of a string but you are giving it a pointer to an integer. What exactly are you trying to do? Count the digits in a string representation of that number?


 

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
Yeah, I'm trying to find out how many digits they enter. Remeber, I'm a newb so bare with me and thanks GL for the link, much appreciated.
 

Spydermag68

Platinum Member
Apr 5, 2002
2,616
99
91
You could make a member function that returns the length.

int objEmp1::GetLength( void ) const
{
int base10 = 10;
int length = 1;
int temp = iIDNumber

if (temp < 0)
temp *= -1;
while (base10 < temp)
{
base10 *= 10;
length++;
}
return (length);
};