So i have the following code:

//main program
char username[15] = {'r','g','f','i','r','e','f','l','y','2','4'};
char password[9] = {'1','2','3','4','5','6','7','8'};

if(client.Send(15,9,username,password))
{
    printf("Connection Successfully Established");
}
//class file

bool Class_Name::Send(int usize, int psize, char *username, char *password)
{
   
   itoa(usize,sendbuffer,16); // works fine
   itoa(psize,sendbuffer + 1,16); // works fine

   for(int i = 0; i< usize; i++)
   {
       sendbuffer[i+2] = *(username + i); //works fine
   }
    
   int x=2+usize;
   for(int H = 0; H<psize;H++)
   {
       
	   sendbuffer[H+x] = *(password + H); // does not work
   }



    SentBytes = send(client_socket, sendbuffer, strlen(sendbuffer), 0);
    
    if (SentBytes == SOCKET_ERROR)
    {
        return false;
    }
    return true;
}

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

Recommended Answers

All 2 Replies

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:

char username[15] = {'r','g','f','i','r','e','f','l','y','2','4','\0'};
// or
char *username = "rgfirefly24"; //automatically adds the '\0'

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.