-2

I have code for a 'zombie translator' as an example from my professor. From what I can tell it takes a string of english words and applies a few rules to it via functions. It currently uses strcpy and strcat to do this, however it will not compile even if I change them to strcpy_s. Without including the other functions (for the sake of space), here is my main function as an example

int main()
{
char english[MAX];
char zombie[MAX];
char zombie_word[MAX];
int pos_e; /* Current position in english line of text */
int pos_z; /* Current position in line of translated zombie text */

while (1) {
    pos_e = 0;
    pos_z = 0;
    strcpy(zombie, "");

    cout << ("Enter English text: ");
    cin >> english;

    /* This loop translates the line from english to zombie. */
    do
    {
        get_next_word(english, &pos_e, zombie, &pos_z);

        translate_word(english, &pos_e, zombie_word, &pos_z);

        strcat(zombie, zombie_word);

    } while (pos_e < strlen(english));

    print_translation(zombie);
}
return 0;
}

So more specifically, what should i do to the line strcat(zombie, zombie_word); to make it compile properly in Visual Studio 2015?

It's not for a grade, I just really want to be able to understand this before the midterm, and it's a bit difficult to play around with it. I would prefer not to have to disable it through _CRT_SECURE_NO_WARNINGS so that I know what to do if I need to do something similar.

Perhaps changing the char variables into strings or something like that? I've been looking around for awhile and can't find the actual process. Thank you very much for any assistance, I greatly appreciate your time.

lana
  • 33
  • 6
  • 1
    Can you share what the problem was with strcpy_s? A better solution for c++ would be std::string, but you might need to know how to use strcpy_s at some stage too. – The Dark Mar 02 '17 at 23:02
  • In C++ the right answer is almost always to convert the `char*` to `std::string`, but just out of interest when you replaced `strcat`and friends with the _s versions, what went wrong? Did you read the [documentation page](https://msdn.microsoft.com/en-us/library/d45bbxx4.aspx) to learn the differences between the two? – user4581301 Mar 02 '17 at 23:02
  • My error seems to only exist within the other functions, I get the message strcpy_s Error: no instance of overloaded function "strcpy_s" matches the argument list. – lana Mar 02 '17 at 23:09
  • 2
    Note that there is a [proposal N1967](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm) that the C committee should deprecate the `_s` functions and keep the originals. :-) – Bo Persson Mar 02 '17 at 23:10
  • You can't just change `strcpy` to `strcpy_s`, they have different parameters - `strcpy_s` requires you to pass the size of the target buffer through as parameter 2. – The Dark Mar 02 '17 at 23:11
  • Your code is incomplete; in particular, it seems to be missing at least one `#include`. Please [edit] your code so it's a [mcve] of your problem, then we can try to reproduce and solve it. You should also read [ask]. – Toby Speight Mar 03 '17 at 13:49

1 Answers1

1

From Microsoft: strncat_s

You need to include the length of the array to prevent buffer overflow dangers.

The API is:

errno_t strncat_s(  
   char *strDest,  
   size_t numberOfElements,  
   const char *strSource,  
   size_t count  
);

numberOfElements is size of destination array.

TheGreatContini
  • 6,429
  • 2
  • 27
  • 37
  • A bit more than that. `strncpy_s` has a batch of additional options on what to do on overflow, truncate, crash, call custom handler... – user4581301 Mar 02 '17 at 23:06
  • ```strncpy_s(zombie, "");``` now yields Error: no instance of overloaded function "strncpy_s" matches the argument list. I apologize for misunderstanding, I need to put the length of the array inside of the paranthesis? – lana Mar 02 '17 at 23:06