-3

In my C program i read three string "task_name", "task_trigger" and "task_number".

               char task_name[255];
               char task_trigger[10];
               char task_number[10];

First, i read "task_name" :

               void get(char *prompt, char *str, int size)
               {
                    printf("%s", prompt);

                    fgets(str, size, stdin);

                    if (str[strlen(str) - 1] == '\n')
                    str[strlen(str) - 1] = '\0';

                    fflush(stdin);
               }

               get("Please enter the name for the system task you want", task_name, 255);
               if (strlen(task_name) == 0)
               {
                    strcpy(task_name, "TaskSystem");
               }

After, i read "task_trigger" in a while and if "task_trigger" is equal to 1 i read task_number, when i get task_number, i concate it at the end of task_trigger :

                while (strcmp(task_trigger, "1") != 0)
                {
                    printf("1 - MINUTE - Run the task on specified minutes.\n");

                    if (strcmp(task_trigger, "1") == 0)
                    {
                        strcpy(task_trigger, "/MO MINUTE: ");
                        printf("%s", task_name);
                        get("How many minutes ? : ", task_number, 10);
                        strcat(task_trigger, task_number);
                        break;
                    }
                }

When i run my program, everything is asked fine, but task_name is eual to ":", i don't know why.. What i have shared to you is a part of my program, the error is triggered at strcpy(task_trigger, "/MO MINUTE: ");.

Thanks :)

Mick D
  • 1
  • 1
  • 2
  • 6
    `while(a) if(!a){...}` is never going to do anything. I get the impression that the code you posted is not a [mcve] – Tim Randall Nov 12 '18 at 16:49
  • 7
    `strcpy(task_trigger, "/MO MINUTE: ");` Copying 13 bytes into a 10 byte buffer? – Fred Larson Nov 12 '18 at 16:51
  • 1
    Calculate the count of chars in "/MO MINUTE: " and compare it with task_trigger size. – 273K Nov 12 '18 at 16:51
  • 3
    `fflush(stdin);` is undefined behavior: https://stackoverflow.com/questions/18170410/what-is-the-use-of-fflushstdin-in-c-programming – yano Nov 12 '18 at 16:52
  • You say that " error is triggered at strcpy(task_trigger, "/MO MINUTE: ");" Well, that can't be correct as the code won't be executed (see comment from Tim Randall). However, the result ":" kind of suggest that the code is actually executed (see comment from Fred Larson). So that leads to.... are you sure you have posted the **real** code? – Support Ukraine Nov 12 '18 at 17:08
  • Thanks for the help :) – Mick D Nov 13 '18 at 08:03

1 Answers1

0

Sorry i haven't posted the minimal code ..

            while (strcmp(task_trigger, "1") != 0)
            {
                printf("1 - MINUTE - Run the task on specified minutes.\n");
                get("Please enter the time of recursive call for the system task you want to create to the victim [default : 'minutes']: ", task_trigger, 255);
                if (strcmp(task_trigger, "1") == 0)
                {
                    strcpy(task_trigger, "/MO MINUTE: ");
                    printf("%s", task_name);
                    get("How many minutes ? : ", task_number, 10);
                    strcat(task_trigger, task_number);
                    break;
                }
            }

i have made a mistake at the definition of the size of "task_trigger", 10 byte was not much thanks Fred Larson ...

Mick D
  • 1
  • 1
  • 2