0

I'm currently writing a program that can convert several mathematical functions, like converting floats to cosines, natural logs, etc. I'm also include a way to convert a date format of m/dd/yyyy, to the format of mm-dd-yyyy. I'm trying to token c-string by the '/' and then concatenate it back together with '-'. The problem is visual studio's gives me a C4996 error code, and says my function may be unsafe. I'm aware I can turn this off so they don't give me these kinds of warnings, but when I run the program, the strtok function doesnt appear to do anything.

#include <iostream>
#include <cstring>
using namespace std;

//KEEP IN MIND THIS IS ONLY ONE PART OF THE PROGRAM, I have more includes, and this is under the main function. (I only included relevant includes)

//If they enter 4 to convert a cstring date.
    if (input == 4) {
        cout << "You entered: change the format of a cstring date" << endl;
        cout << "Please enter a date in the format of mm/dd/yyyy: " << endl;
        char str[12];   //C-String to hold input
        cin >> str;
        //Create an char[] to hold the answer
        char newStr[32];
        // Returns first token  
        char* token = strtok(str, "/");
        // Keep printing tokens while one of the 
        // delimiters present in str[]. 
        while (token != NULL)
        {
            strcat_s(newStr, token);    //Cat it to string
            strcat_s(newStr, "-");  //Add the -
            token = strtok(NULL, "/");
        }

        cout << "Your new Date is: " << newStr;
        return 0;
    }

Even when I bypass the error message, and run the program, enter for example: 11/22/3333 I recieve an output of 11/22/3333

0 Answers0