I used a string called "comando
" as the input. I copy the first word of "comando
" into "comandoParz
" and I use "comandoParz
" as the parameter to call a specific function.
The first call works fine, but the second one gives the same output as the first one.
Maybe it's because I need to empty the array "comando
" and "comandParz
" but I tried a few solutions and none of them seemed to work.
I'll include the full code below.
(Sorry if I added too much code; I'm still trying to figure out the best way to post here.)
I tried adding strcpy(comandoParz, "")
and strcpy(comando, "")
(they are not active in the code I posted below) and the first input works but the other ones don't give any output.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void addrel() {
printf("\nAGGIUNGI RELAZIONE:\n\n");
}
void delrel() {
printf("\nELIMINA RELAZIONE:\n\n");
}
void addent() {
printf("\nAGGIUNGI UTENTE:\n\n");
}
void delent() {
printf("\nELIMINA UTENTE:\n\n");
}
int main() {
int i = 0;
char comando[100];
char comandoParz[10];
START:
/*strcpy(comando, "");
strcpy(comandoParz, "");*/
printf("Input: ");
fgets(comando, sizeof(comando), stdin);
while(comando[i] != '\0') {
comandoParz[i] = comando[i];
i++;
}
i++;
if(strcmp(comandoParz, "delent\n") == 0) {
delent();
} else {
if(strcmp(comandoParz, "addent\n") == 0) {
addent();
} else {
if(strcmp(comandoParz, "addrel\n") == 0) {
addrel();
} else {
if(strcmp(comandoParz, "delrel\n") == 0) {
delrel();
}
}
}
}
goto START;
}
For example, the first input may be "addrel" and the output will be "AGGIUNGI RELAZIONE:". The second input may be "delent" and the answer should be "ELIMINA UTENTE:" but it will be "AGGIUNGI RELAZIONE" as the first call.