C++ Help: Can’t get system time to print to screen/outputfile.
Below is my code. Whenever I try to display the system time itonly gives me the memory address at which the c-string is located.Please fix and provide screenshot of output.
//class definition:
class Restaurant
{
public:
short int restaurantNum;
int idNum;
char* mealTime[20];
};
// int main():
int main()
{
std::ofstream outputFile;
outputFile.open(“information.txt”);
Restaurant *patron = new Restaurant;
time_t now = time(0);
char* timeOf = ctime(&now);
timeOf[20] = ”;
patron->mealTime[20] = timeOf;
std::cout << patron->mealTime;
outputFile << patron->mealTime;
}
Solution
Here is code:
#include <iostream>
#include <fstream>
#include <string.h>
#include <time.h>
using namespace std;
class Restaurant
{
public:
short int restaurantNum;
int idNum;
string mealTime;
};
int main()
{
ofstream outputFile;
outputFile.open(“information.txt”);
Restaurant *patron = new Restaurant;
time_t now = time(NULL);
string timeOf = ctime(&now);
patron->mealTime = timeOf;
cout << patron->mealTime;
outputFile << patron->mealTime;
}
information.txt:
Output:
information.txt