strstr函数与2015年相比不起作用
strstr function does not work vs 2015
因此,我必须引入一些名称(无论如何),并计算其中有多少个名称我可以找到子字符串"ana"。我不允许使用字符作为名称,我需要使用字符串。我写的代码非常简单,但strstr函数不起作用。
#include <iostream>
#include <string>
#include<stdio.h>
using namespace std;
void main()
{
string name;
string ana;
int ok = 0;
int nr = 0;
ana = "ana";
cout << "if you want to stop entering names introduce 1 after the last name, else introduce 0.";
while (ok == 0)
{
cout << "Name here: " << flush;
getline(cin, name);
if (strstr(name, ana) != 0)
nr++;
cin >> ok;
}
cout << "The number of names that contain 'ana' is: " << nr;
}
你能帮我一下吗?错误为:"严重性代码描述项目文件行禁止显示状态错误C2665"strstr":2个重载都无法转换所有参数类型T1E1 e:\uni\an ii\lf\T1E1\T1E1\e1.cpp 20"
strstr
适用于C字符串,而不是std::string
。
请改用std::string::find
。
代替
if (strstr(name, ana) != 0)
nr++;
使用
if ( name.find(ana) != std::string::npos )
nr++;
否则,您可以包含头文件cstring
,其中包含strstr
函数。在.cpp
文件中添加以下代码。
#include <cstring>
更多详细信息,请访问cppreference.com.
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- Opencv在2013年第5季度不起作用.应用程序总是在窗口中崩溃
- 迁移到 Visual Studio 2015 时 ATL 不起作用
- Visual Studio 2015 中的其他包含目录不起作用
- strstr函数与2015年相比不起作用
- OpenCV和Visual Studio 2015:为什么包含静态库不起作用
- UNICODE 不起作用 -> "????" (Visual C++ 2015)