-3

all! I'm trying to find the best way to accept user input, timestamp it, then place it into a file with correct formatting (One timestamp/input per line. What is the best way to timestamp then give over to put it all in a file? Thanks!

Heres my barebones code as an example:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;

//void passwordCheck()
//{
//
//}

string timestamp(string passwordInput1, string passwordInput2, string passwordInput3, string passwordInput4, string passwordInput5, string passwordInput6, string passwordInput7, string passwordInput8, string passwordInput9, string passwordInput10)
{
time_t now = time(0);

char* dt = ctime(&now);

cout << "The local date and time is: " << dt << endl;

tm *gmtm = gmtime_s(&now);
dt = asctime(gmtm);

}

//void saveToFile()
//{
//
    //cout << "I've saved 10 passwords for you." << endl;
//}

int main()
{
    ofstream outputToFile;
    outputToFile.open("manageMyPasswords.txt");

    // Look for easier/cleaner way to do this.
    string passwordInput1, passwordInput2, passwordInput3, passwordInput4, passwordInput5, passwordInput6, passwordInput7, passwordInput8, passwordInput9, passwordInput10;

    // Put into a function then call back here.
    cout << "Welcome to Password Manager!" << endl;
    cout << "     Enter your first password" << endl;
    getline (cin, passwordInput1);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput2);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput3);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput4);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput5);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput7);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput8);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput9); 
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput10);


    //void saveToFile();







    system("pause");
    return 0;
}

1 Answers1

1

As I understood your question you need sth like this:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <map>

int main(int argc, char** argv)
{
    typedef std::multimap<time_t, std::string> Passwords;
    Passwords passwords;

    short counter = 1;

    std::cout << "Enter passwords or \"quit\" to stop" << std::endl;
    for (std::string line;;)
    {
        std::cout << "Enter password " << counter << " -> ";
        getline(std::cin, line);
    
        if (line.empty())
        {
            continue;
        }
        else if (line.compare("quit") == 0)
        {
            break;
        }

        passwords.insert(std::pair<time_t, std::string>(time(0), line));

        counter++;
    }

    std::ofstream outputFile;
    outputFile.open("manageMyPasswords.txt", std::ios::trunc);

    for (Passwords::const_iterator it = passwords.begin(); it != passwords.end(); it++)
    {
        outputFile << it->first << " " << it->second << "\n";
    }

    outputFile.close();

    return 0;

}

It will wait for users input infinitely, until "quit" is not entered. After that it will dump timestamps with password line by line.

P.S. I've used multimap, because you can have duplicate timestamps.

Update

It seems from your code that you need human-readable time. Use this function to retrieve current date and time:

// Returns current date-time formatted like [YYYY-MM-DD][HH:mm:ss]
const std::string GetCurrentDateTime()
{
    time_t currentTime = time(0);
    struct tm localDateTime;
    char buf[80];

    localtime_s(&localDateTime, &currentTime);
    strftime(buf, sizeof(buf), "[%Y-%m-%d][%X]", &localDateTime);

    return buf;
}
Community
  • 1
  • 1
sashadereh
  • 223
  • 1
  • 15
  • `typedef`? If you're not using C++11 multimap doesn't guarantee order of duplicates, so your multimap approach may actually backfire if a duplicate occurs. Seems like `std::vector>` would be sufficient. – kfsone Apr 23 '16 at 19:12
  • This works exactly as I need, I did not think to do it like this, thank you so much!! – Connor Sanders Apr 23 '16 at 19:32