So, I'm into about 650 lines of some code I'm working on. So I'll try to explain instead of flooding this post. Say I have some code like this:
int main() {
int i, j;
char data[INPUT_BUFF];
printf("Gimme something: ");
fgets(data, INPUT_BUFF, stdin);
for (j = 0; j < data[INPUT_BUFF]; j++){
switch(data[j]){
case 0:
if(j == 'e'){
atoi(data);
data[j] = ranit(e_array);
sprintf( data, "%d", data );
}
case 1:
if(j == 'm'){
atoi(data);
data[j] = ranit(m_array);
sprintf( data, "%d", data );
}
case 2:
if(j == 'n'){
atoi(data);
data[j] = ranit(n_array);
sprintf( data, "%d", data );
}
case 3:
if(j == 'o'){
atoi(data);
data[j] = ranit(o_array);
sprintf( data, "%d", data );
}
case 4:
if(j == 'y'){
atoi(data);
data[j] = ranit(y_array);
sprintf( data, "%d", data );
}
}
}
for (i = 0; i < INPUT_BUFF; i++){
printf("%x\n", (int)data[(int)i]);
}
return 0;
}
I have a switch statement in the above that only checks for the characters in the word 'money'. The entire alphabet would have been too much to put here. The function ranit() grabs a random element out of the array it is past. That function has been checked and works perfect. In the case all the elements are numbers. I've tried to do this a lot of different ways, but what I want to do is after checking for a matching character, replace it with a random element in from the corresponding arrays that contain integers.
I tried using atoi() to make the 'data' character array able to hold integers. That way I wouldn't get an error about mismatching types. At some point I could have sworn itoa worked with gcc, but I now see this is no longer part of the standard library. I tried using sprintf() to convert the data array back to chars for the next iteration of the loop.
When I run the code I notice the random numbers aren't displayed, but the hex equivalents for the ascii values of the word 'money':
$ ./test
Gimme something: money
6d
6f
6e
65
79
I'm unsure if I'm assigning the values wrong or if sprintf won't allow for the same source and destination values. All the examples I found would put this in a new variable or array name, but I need to have it in the same for the next loop iteration. So maybe I should be using something else?
No warnings or errors from the compiler. Just strictly a logical error at this point. Any suggestions much appreciated.