So i tried to create a username and password for admins using c languange, in the code here the index of array inpstring determines what is username and password, for even (and 0) numbers are username, and odd numbers are password.
#include <stdio.h>
#include <string.h>
void Listofadmins(int index, char inpstring[][50]){
if(index == 0){
strcpy(inpstring[0], "Carl01");
strcpy(inpstring[1], "SVm6u&N591s2");
} else if(index == 1){
strcpy(inpstring[2], "Devoth754");
strcpy(inpstring[3], "eeKS7@8%0@T6");
} else if(index == 2){
strcpy(inpstring[4], "David439");
strcpy(inpstring[5], "l$z7eqV2aD00");
} else if(index == 3){
strcpy(inpstring[6], "Matt208");
strcpy(inpstring[7], "h9Je2#Ri3or$");
}
}
void main()
{
int j, k = 0, y = 1;
char str[100][100];
for(j = 0, k, y; j < 4; j , k = 2, y = 2){
listofadmins(j, str[10]);
printf("%s\n%s", str[k], str[y]);
}
}
When i tried to run the code, there is no output and just blank. I thought that the listofadmins function will copy the string in inpstring array to the main program array str. Did you know where is the problem?
CodePudding user response:
A few issues ...
- Using a 2D array for this is problematic. Better to create a
struct
- Doing an
if/else
ladder inListofadmins
along withstrcpy
is needlessly complicated - Using fixed size
char
arrays instead ofchar *
complicates things.
Here is some simplified code:
#include <stdio.h>
#include <string.h>
struct user {
const char *user;
const char *pw;
};
struct user users[] = {
{ "Carl01", "SVm6u&N591s2" },
{ "Devoth754", "eeKS7@8%0@T6" },
{ "David439", "l$z7eqV2aD00" },
{ "Matt208", "h9Je2#Ri3or$" }
};
int
main(void)
{
for (size_t i = 0; i < sizeof(users) / sizeof(users[0]); i) {
struct user *user = &users[i];
printf("User: %s Pw: %s\n",user->user,user->pw);
}
return 0;
}
Here is the program output:
User: Carl01 Pw: SVm6u&N591s2
User: Devoth754 Pw: eeKS7@8%0@T6
User: David439 Pw: l$z7eqV2aD00
User: Matt208 Pw: h9Je2#Ri3or$
CodePudding user response:
I changed the call parameters for 'listofadmins' to match the declariation of 'str' truning inpstring[][50]
into inpstring[][100]
.
And when calling the function I removed the brackets after 'str' so str[10]
into str
.
The resulting code is this
#include <stdio.h>
#include <string.h>
void listofadmins(int index, char inpstring[][100]){
if(index == 0){
strcpy(inpstring[0], "Carl01");
strcpy(inpstring[1], "SVm6u&N591s2");
} else if(index == 1){
strcpy(inpstring[2], "Devoth754");
strcpy(inpstring[3], "eeKS7@8%0@T6");
} else if(index == 2){
strcpy(inpstring[4], "David439");
strcpy(inpstring[5], "l$z7eqV2aD00");
} else if(index == 3){
strcpy(inpstring[6], "Matt208");
strcpy(inpstring[7], "h9Je2#Ri3or$");
}
}
void main()
{
int j, k = 0, y = 1;
char str[100][100];
for(j = 0, k, y; j < 4; j , k = 2, y = 2){
listofadmins(j, str);
printf("%s\n%s", str[k], str[y]);
}
}
The output is:
Carl01
SVm6u&N591s2Devoth754
eeKS7@8%0@T6David439
l$z7eqV2aD00Matt208
h9Je2#Ri3or$
I may recommend to change the printf for better readabilty. Hope i could help.