unable to add char array of numbers to another char array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 3
Reputation: rgfirefly24 is an unknown quantity at this point 
Solved Threads: 0
rgfirefly24 rgfirefly24 is offline Offline
Newbie Poster

unable to add char array of numbers to another char array

 
0
  #1
Mar 3rd, 2009
So i have the following code:

  1. //main program
  2. char username[15] = {'r','g','f','i','r','e','f','l','y','2','4'};
  3. char password[9] = {'1','2','3','4','5','6','7','8'};
  4.  
  5. if(client.Send(15,9,username,password))
  6. {
  7. printf("Connection Successfully Established");
  8. }

  1. //class file
  2.  
  3. bool Class_Name::Send(int usize, int psize, char *username, char *password)
  4. {
  5.  
  6. itoa(usize,sendbuffer,16); // works fine
  7. itoa(psize,sendbuffer + 1,16); // works fine
  8.  
  9. for(int i = 0; i< usize; i++)
  10. {
  11. sendbuffer[i+2] = *(username + i); //works fine
  12. }
  13.  
  14. int x=2+usize;
  15. for(int H = 0; H<psize;H++)
  16. {
  17.  
  18. sendbuffer[H+x] = *(password + H); // does not work
  19. }
  20.  
  21.  
  22.  
  23. SentBytes = send(client_socket, sendbuffer, strlen(sendbuffer), 0);
  24.  
  25. if (SentBytes == SOCKET_ERROR)
  26. {
  27. return false;
  28. }
  29. return true;
  30. }

Basically the part where i put does not work doesn't input the information from the password const char * to the array and i'm not sure why it doesn't
Last edited by rgfirefly24; Mar 3rd, 2009 at 8:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: unable to add char array of numbers to another char array

 
0
  #2
Mar 3rd, 2009
Strings (if you do it the C way) should have a null character ('\0') to show where the end of the string is. Change your definitions to look like this:
  1. char username[15] = {'r','g','f','i','r','e','f','l','y','2','4','\0'};
  2. // or
  3. char *username = "rgfirefly24"; //automatically adds the '\0'
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: rgfirefly24 is an unknown quantity at this point 
Solved Threads: 0
rgfirefly24 rgfirefly24 is offline Offline
Newbie Poster

Re: unable to add char array of numbers to another char array

 
0
  #3
Mar 3rd, 2009
I have changed it to char *username = "rgfirefly24" and char *password = "12345678" However the 12345678 still will not add to the char sendbufffer[1024].

changed my for loops to strcat and it works fine
Last edited by rgfirefly24; Mar 3rd, 2009 at 9:15 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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