0

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Destro
  • 41
  • 4
  • 1
    You are forgetting to add the null on the end of the string when you copy it. – stark Aug 03 '19 at 11:19
  • Observation — you really shouldn't be using that `goto`. You might need to write a function and call that function in a loop, but you shouldn't need that sort of `goto` for looping. It's a bad 'code smell'. (There are places to use `goto`; this is not one of them.) – Jonathan Leffler Aug 03 '19 at 23:05
  • I was thinking about using a do { } while(confirmation != 0) or something like that instead of using goto but i thought it was a smarter idea. Why is it considered a bad code smell? – Destro Aug 04 '19 at 15:29
  • GOTO is only considered bad coding practice after 1968. You are free to use it until then. https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf – stark Aug 07 '19 at 19:28
  • Being a self thaught programming student I didn't know that, i'll make sure to keep the "density" of go-to in my programs as low as possible. – Destro Aug 08 '19 at 16:22

1 Answers1

0
  • The code needs to re-initialise i to 0 within the START-loop.

  • A C-string need to be terminated with a 0.

  • The copy-loop needs to take care to not overwrite the destination array when copying.

Taking all of the above into account this

while(comando[i] != '\0') {
    comandoParz[i] = comando[i];
    i++;
} 
i++;

could look like:

i = 0;
while (comando[i] != '\0' 
  && i < (sizeof comandoParz -1)) /* one less for the '\0'-terminator */
{
    comandoParz[i] = comando[i];
    i++;
} 
comandoParz[i] = '\0'; /* place the '\0'-terminator */

To answer you question

how to properly empy a string

You could memset it with '\0':

memset(comandoParz, '\0', sizeof comandoParz);

Although for all methods expecting a C-string putting a '\0' into the array element where you want the string to end would do.

So to "empty" it just do, placing a '\0' in its 1st element:

comandoParz[0] = '\0';
alk
  • 69,737
  • 10
  • 105
  • 255