与时间一起工作
Working with time
我开发了一个c++应用程序在23:01:50打印Hello
我的代码
#include<iostream>
#include<string>
#include <time.h>
#include <windows.h>
using namespace std;
int main ()
{
time_t start = time (&start);
cout<<ctime(&start);
while(1)
{
time (&start);
if( ctime(&start) == "Fri Jan 18 23:01:50 2013n" )
cout << "Hello";
Sleep(500);
cout << ctime(&start);
}
}
但是输出是:
Fri Jan 18 23:01:49 2013
Fri Jan 18 23:01:49 2013
Fri Jan 18 23:01:50 2013
Fri Jan 18 23:01:50 2013
Fri Jan 18 23:01:51 2013
Fri Jan 18 23:01:51 2013
为什么不打印Hello
?
谢谢
在c++中,相等操作符"=="不能像您期望的那样用于char指针(这是您正在使用的)。它比较的是指针,这些指针实际上指向内存中的不同位置。要比较字符串(顺便说一句,这不是执行此检查的最佳方法),您需要使用字符串比较函数。
例如:if (strcmp(ctime(&start), "Fri Jan 18 23:01:50 2013n") == 0)
{
}
更多信息请参见:http://www.cplusplus.com/reference/cstring/strcmp/
将==
改为strcmp(ctime(...), "Fri Jan 18...") == 0
。你不能比较c-string和==
,因为它比较的是地址而不是值。
if( strcmp(ctime(&start), "Fri Jan 18 23:01:50 2013n") == 0 )
相关文章:
- 如何使我的 sizeof sum 结构与空参数包一起工作
- SFINAE:它如何与派生类一起工作?
- std::remove() 按预期处理文字,但不能与取消引用的迭代器一起工作
- 尝试让条形码扫描仪与Arduino一起工作
- 从书本中学习C++无法使该程序与类一起工作
- 让类与运算符一起工作更简单的方法
- 如何使lambda与std::nullopt一起工作
- 当我使用长整型时,我的代码不起作用,它与 int 一起工作得很好
- 返回对象如何与分配运算符一起工作
- GLM :: perspective()和glm :: lookat()一起工作
- 如何与朋友操作员一起在链表下面工作
- SFML OpenGL:如何同时与他们一起工作
- 使QTConcurrent ::映射与Lambdas一起工作
- 如何与C 字符串中的非ASCII字符一起工作
- 我如何适应擦除式习惯与矢量元组一起工作
- 需要帮助使 VOID 代码与总工资一起工作
- 我怎样才能让这个刽子手代码与 wxDev-C++ 一起工作
- Googletest 在 bazel test 中永远不会失败(在它应该失败的地方),但与 cmake & clion 一起工作
- 常量变量初始化仅与成员初始化列表一起工作
- feal_ptr与unique_ptr一起工作