如何打印用户输入初始化的时间?



我是编程新手,尤其是c++。我试图做一些像谷歌助手,但基于文本,我目前正在打印出的时间,当用户键入的东西像"timepls"。对于代码,我使用了一种基于数组的库,这样我就可以将预配置的字符串存储在数组中,并将用户输入变量与每个数组中的每个对象进行比较。如果字符串与函数中的一个数组匹配,它就会被执行。我的问题是,我已经尝试了ctime,并使用了与我知道的另一个函数相同的初始化代码(它是唯一的其他函数lol)。有人能帮我打印出用户输入的当前时间吗?下面是我的代码:

#include <iostream>
#include <ctime>
using namespace std;
void howareyouq();
void whattimeisit();


int main(){
howareyouq();
whattimeisit();
}



void howareyouq(){
string howruq[4] = {"how are you?", "hru?", "how r u?", "hru"}; //array for the valid thingies
string user_input; //creating userinput variable
cin >> user_input; //writing user input to user_input 
for (int i=0; i!=4; i++) { 
if (user_input == howruq[i]) {  //if the array contains something equal to user_input, this gets executed
cout << "Thanks, Im fine!" << endl; //the answer
}
}
}
void whattimeisit()
{
string thetimepls[] = {"timepls", "what time is it?", "how late is it?", "whats the time?", "time?"};
string user_input;
cin >> user_input;
for (int i=0; i++;) {
if (user_input == thetimepls[i]) {
time_t now = time(0);
char *date = ctime(& now);
cout << "The local date and time : " << date << endl;
}
}
}

获得正确的边界并在一次读取:

#include <iostream>
#include <ctime>
using namespace std;
void howareyouq(string const& user_input);
void whattimeisit(string const& user_input);
int main()
{
string user_input; //creating userinput variable
cin >> user_input; //writing user input to user_input 
howareyouq(user_input);
whattimeisit(user_input);
}          
void howareyouq(string const& user_input)
{
string howruq[4] = {"how are you?", "hru?", "how r u?", "hru"}; //array for the valid thingies
for (int i=0; i!=4; i++)
{ 
if (user_input == howruq[i]) {  //if the array contains something equal to user_input, this gets executed
cout << "Thanks, Im fine!" << endl; //the answer
}
}
}
void whattimeisit(string const& user_input)
{
string thetimepls[] = {"timepls", "what time is it?", "how late is it?", "whats the time?", "time?"};
for (int i=0; i!=5; i++) { 
if (user_input == thetimepls[i]) {
time_t now = time(0);
char *date = ctime(& now);
cout << "The local date and time : " << date << endl;
}
}
}

最新更新