The program outputs a menu asking for a new question or to ask a question. If you ask a question, it will search a .tsv text file for the question and return the answer. It will then check a map that is used for time and date, if it exists, it will execute the function intended. If not, it will just print the answer. As for a new question, it simply edits the file and inputs the question then answer. I'd like to expand this program to be bigger in the future, multiple files, maps, and better deduction on questions.
So far, this is what I have: 5 files, a main, time and date header and cpp, and file header and .cpp.
getTime.h
#ifndef H_getTime
#define H_getTime
#include <time.h>
#include <string>
using namespace std;
class getTime {
public:
void printCurrentTime();
void printCurrentDate();
private:
string currentTime;
string currentDate;
string strMonth;
int hours;
int minutes;
int seconds;
int day;
int month;
int year;
time_t now;
struct tm *current;
};
getTime.cpp
#include "getTime.h"
#include <iostream>
using namespace std;
void getTime::printCurrentTime() {
now = time(0);
current = localtime(&now);
hours = current->tm_hour;
minutes = current->tm_min;
seconds = current->tm_sec;
currentTime = to_string(hours) + ":" + to_string(minutes) + ":" + to_string(seconds);
cout << "The current time is: " << currentTime << endl;
}
void getTime::printCurrentDate() {
now = time(0)
current = localtime(&now);
day = current->tm_mday;
month = current->tm_mon;
year = current->tm_year;
switch (month) {
case 0:
strMonth = "Jan";
break;
case 1:
strMonth = "Feb";
break;
case 2:
strMonth = "Mar";
break;
case 3:
strMonth = "April";
break;
case 4:
strMonth = "May";
break;
case 5:
strMonth = "June";
break;
case 6:
strMonth = "July";
break;
case 7:
strMonth = "Aug";
break;
case 8:
strMonth = "Sep";
break;
case 9:
strMonth = "Oct";
break;
case 10:
strMonth = "Nov";
break;
case 11:
strMonth = "Dec";
break;
}
currentDate = to_string(day) + " " + strMonth + " " + to_string(1900 + year);
cout << "The current date is: " << currentDate << endl;
}
fileRW.h
#ifndef H_fileRW
#define H_fileRW
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
class fileRW {
public:
string readFile(string q);
void writeFile(string q, string a);
fileRW();
private:
string inFile;
FILE* fout;
ifstream fin;
string temp1, temp2;
string input;
bool error;
size_t pos;
};
#endif
fileRW.cpp
#include "fileRW.h"
using namespace std;
fileRW::fileRW() {
inFile = "C:\\Path\\To\\TSV\\Text\\File.txt";
}
void fileRW::writeFile(string q, string a) {
string outString = q + "\t" + a;
fout = fopen(inFile.c_str(), "a+b");
fprintf(fout, "%s", outString.c_str());
fprintf(fout, "\n");
fclose(fout);
}
string fileRW::readFile(string q) {
fin.open(inFile);
error = false;
while (!fin.eof()) {
getline(fin, input);
if (!fin) break;
pos = input.find('\t');
if (string::npos == pos) {
break;
}
temp1 = input.substr(0, pos);
temp2 = input.substr(pos + 1, input.length() - pos);
if (q == temp1) {
fin.close()
return temp2;
}
}
if (error) {
cout << "File is corrupted..." << endl;
}
fin.close()
return "Question not found";
}
main.cpp
#include "getTime.h"
#include "fileRW.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef string KeyType;
typedef void(getTime::*DoFunc)(void);
typedef pair<const KeyType, DoFunc> Pair;
typedef map<KeyType, DoFunc> mapTimeCall;
int nReadLine();
bool findA(string, mapTimeCall);
int main() {
string input;
string q, a;
char endLoop = 'N';
int menu = 0;
fileRW frw;
mapTimeCall callTimeMap;
callTimeMap.insert(Pair("printCurrentTime()", &getTime::printCurrentTime));
callTimeMap.insert(Pair("printCurrentDate()", &getTime::printCurrentDate));
while (endLoop != 'Y') {
cout << "1. New Question" << endl;
cout << "2. Ask Question" << endl;
cout << "3. Exit" << endl;
cout << "Enter command: ";
menu = nReadLine();
cout << endl;
switch (menu) {
case 1:
getline(cin, q);
cout << "What is the question: ";
getline(cin, q);
cout << endl << "And the answer: ";
getline(cin, a);
cout << endl;
frw.writeFile(q, a);
break;
case 2:
getline(cin, q);
cout << "What is the question: ";
getline(cin, q);
cout << endl;
a = frw.readFile(q);
if (!(findA(a, callTimeMap))) {
cout << "The answer is: " << a << endl;
}
break;
case 3:
endLoop = 'Y';
break;
}
}
system("pause");
return 0;
}
int nReadLine() {
int input;
cin >> input;
return input;
}
bool findA(string a, mapTimeCall timeCall) {
getTime getTime;
mapTimeCall::iterator x;
x = timeCall.find(a);
if (x != timeCall.end()) {
DoFunc f = x->second;
(getTime.*f)();
return true;
}
return false;
}
To have an example of this working, follow these steps when program is loaded:
- Type: 1
- Press: Enter
- Type: What time is it?
- Press: Enter
- Type: printCurrentTime()
- Press: Enter
- Type: 1
- Press: Enter
- Type: What is the date?
- Press: Enter
- Type: printCurrentDate()
- Press: Enter
- Type: 1
- Press: Enter
- Type: What is your name?
- Press: Enter
- Type: (type some name)
- Press: Enter
- Type: 2
- Press: Enter
- Type: (type previous entered question)
- Press: Enter Output Should be an answer that you typed in.
OR, create a text file and fill in the format as question "TAB Answer Newline"