c++ - 如何测试字符串字符是否等于字符
问题描述
我允许用户插入一个time
包含时间的字符串。格式的一个示例是 08:10:45AM。我正在测试第 8 个字符是“A”还是“P”。
这是我的代码:
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string time;
cin >> time;
if (time[8] == "A") {
// do stuff
}
return 0;
}
但是,我收到此错误:“指针和整数之间的比较('int' 和 'const char *”
有谁知道如何解决这一问题?
任何帮助将不胜感激 :)
解决方案
如果要比较字符,请比较字符:
if (time[8] == 'A')
请记住,在 C++'A'
中是一个字符并且"A"
是一个 C 字符串。
推荐阅读
- c# - 在 PowerShell ISE 中作为 PowerShell 脚本的一部分运行的 c# 代码中使用 ReadLine 从用户那里获取输入
- android - 如何在数据绑定 xml 文件中使用 getLifecycleOwner() 方法
- uwp - 在 uwp 中的 zip 文件名前显示 / 符号
- java - 我们可以在一个 Android 项目中同时使用 OpenTok 和 Jitsi 吗?
- c# - 如何在 google DialogFlow Webhook 中添加 localhost URL?
- python - Flask - RuntimeError:未找到应用程序。在视图函数中工作或推送应用程序上下文
- ios - 为什么 Swift Firebase auth() 响应较晚?
- docker - Docker 容器网络 --format
- powerbi - 如何在按钮单击时从 mvc 应用程序将 power bi 报告导出为 pdf
- java - 我想使用 NCDC(国家气候数据中心)气候数据 API 根据邮政编码获取美国过去的最高和最低温度