Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unable to transfer data to pointer

Status
Not open for further replies.

XcyborgX

Programmer
Apr 4, 2002
1
0
0
US

int enterID(char *input, int count)
{

int IdImage;
cout<<&quot;\nEnter ID#: &quot;;
cin>>IdImage;
cout<<IdImage; // the data is correct here
if (IdImage <10)

{
input[count]='#';
count+=1;
input[count]=IdImage; //the problem is here I think
cout<<&quot;input[count]= &quot;<<input[count]; //this line
//displays a happy face
count+=1;
input[count]='.';
count+=1;
return count;
}
}

Here is a part of my program. The program runs fine. BUT I'm trying insert &quot;IdImage&quot; number &quot;input[count]&quot; but it always display a happy face.
 
The problem here is that first you are are dealing with a string

such as
char *welcome = {'h','e','l','l','o'};

First of all this line input[count]='#';
will overwrite the number in the string referring to count
for instance if 0, now your string is
{'#','e','l','l','o'};
or 2
{'h','e','#','l','o'};
This is not a good idea with strings, as you can never be sure that you will always work the way you want to and is overly complicated. You should use strcat instead.

The second and most important problem is that you have to explicity convert the integer to a string before you can put it into a string.
Each letter and symbol has an integer equivalent, like (this is not exact), 71 = a, 189 = ~, 63 = $.

So you are setting the string to an integer value that will be a character of the integers equivalent, and furthermore flush the rest of the string. so now your
*welcome = {'h','e','l','l','o'};
is now
*welcome = {'$'} or {'~'};

This would be much better code although i haven't tested it

char *buffer;
strcpy(input,&quot;#&quot;);

_itoa( Idimage, buffer, 10 );
//convert integer to string
strcat(input,buffer); //add the buffer onto the string

cout<<input;
strcat(input,&quot;.&quot;); //add the . to input string

return strlen(input);


//i dont if this will work but i hope its in the right direction
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top