Jump to content

I am doing an assignment for college and one of the questions involves concatenating to format a name as "Last, First." I know in C++ you can concatenate using + operator, but our professor wants us to use strcat_s(). This is code that singles out my problem:

#include <iostream>
#include <cstring>

int main() {
    char firstName[20] = "John";
    char lastName[20] = "Smith";
    char fullName[40];
    std::cout << "First Name: " << firstName << std::endl;
    std::cout << "LastName: " << lastName << std::endl;
    strcat_s(fullName, lastName);
    strcat_s(fullName, ", ");
    strcat_s(fullName, firstName);
    std::cout << fullName << std::endl;
    return 0;
}

When I compile and run this code I get the following errors:

‘strcat_s’ was not declared in this scope 

strcat_s(fullName, lastName);
                                ^

If I use strcat() which is an older way to concatenate, everything compiles and runs fine. 

 

I did some research and found out that Microsoft made the function strcat_s(), so I was wondering if maybe it only works on Visual Studio in Windows, but I also found that it was a part of C++ 11.

 

I am currently running Linux Mint and CLion, and compiling in C++ 17. Does anyone know the reason I am getting these errors? 

Link to comment
https://linustechtips.com/topic/870422-c-help-with-strcat_s/
Share on other sites

Link to post
Share on other sites

2 hours ago, gabrielcarvfer said:

 

Try again putting the following define at the start of your code:

#define __STDC_WANT_LIB_EXT1__ 1

 

I put that at the start of my code and I got the same error after compiling and running. My IDE says that #define __STDC_WANT_LIB_EXT1__ 1 was not used.

Link to comment
https://linustechtips.com/topic/870422-c-help-with-strcat_s/#findComment-10796806
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×