Problem with a string (char array) and self made strcpy

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 7
Reputation: ingvarorn is an unknown quantity at this point 
Solved Threads: 0
ingvarorn ingvarorn is offline Offline
Newbie Poster

Problem with a string (char array) and self made strcpy

 
0
  #1
Sep 7th, 2008
I'm doing a assignment for a class at my school and have written this code that follows with little help from DaniWeb users but I have one final touch problem.

The problem is that if I run my strcpy function it always fills out the pzsDestination array to fill out 256 characters so the copy always ends to be 256 char instead of exact copy.

Sample.
If is type in "This is text to copy into another array" then I would get "This is text to copy into another array##################################################################" or until I have 256 char in the array.

Any solutions to that you can see for me, I have tried some ways of doing this and never get the correct answer out of this but my best cuess would not to set the size of the pzsDestination before I use it but then I get error which is (30) : error C2133: 'pszDestination' : unknown size.

Here is the code I have now.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void strcpy( char* pszDestination, char* pszSource, int len = -1);
  5. int strlen(char *pszString);
  6.  
  7. void strcpy( char* pszDestination, char* pszSource, int len)
  8. {
  9. if(len == -1) len = strlen(pszSource);
  10. while(*pszDestination && len)
  11. {
  12. *pszDestination = *pszSource;
  13. pszDestination++;
  14. pszSource++;
  15. len--;
  16. }
  17. }
  18.  
  19. int strlen( char* pszString )
  20. {
  21. int i;
  22. for(i=0; pszString[i]; i++);
  23. return i;
  24. }
  25.  
  26.  
  27. int main()
  28. {
  29. char pszInput[256]; // Used for storing the input
  30. char pszDestination[256]; // Used for destination after srtcpy
  31. int len = -1; // Used for storing the length of the string
  32.  
  33. cout << "Enter string (max 255 letters):" << endl;
  34.  
  35. cin.getline(pszInput,256);
  36.  
  37. cout << endl;
  38.  
  39. cout << "The Number of Charaters in.. " <<endl;
  40.  
  41. cout << pszInput << endl;
  42.  
  43. cout << "is: " << strlen(pszInput) << endl; // Sends the input to strlen to get the size of it.
  44.  
  45. cout << endl;
  46.  
  47. cout << "Now we copy the text from one string to another... " <<endl;
  48.  
  49. cout << "Copying your input to the string called pszDestination" <<endl;
  50.  
  51. strcpy(pszDestination,pszInput,len); // Sends the input to strcpy to copy it tp pszDestination
  52.  
  53. cout << "pszDestination is now: " << pszDestination << endl;
  54.  
  55. cout << endl;
  56.  
  57. system("PAUSE");
  58. return 0;
  59. }

Thanks....
Gvari
Last edited by ingvarorn; Sep 7th, 2008 at 2:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with a string (char array) and self made strcpy

 
0
  #2
Sep 7th, 2008
You forget to copy zero char terminated C string.
Can't resist a temptation:
  1. char* StrCpy(char* pd, const char* ps)
  2. {
  3. char* p = pd;
  4. while ((*p++=*ps++) != 0);
  5. return pd;
  6. }
It's a classic self-made strcpy...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 7
Reputation: ingvarorn is an unknown quantity at this point 
Solved Threads: 0
ingvarorn ingvarorn is offline Offline
Newbie Poster

Re: Problem with a string (char array) and self made strcpy

 
0
  #3
Sep 7th, 2008
Thanks for that.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC