将字符串传递给布尔函数
pass string to bool function
如何通过将字符串传递给布尔函数来实现tis?我需要让用户输入一系列字符串,在每次输入后,程序应该根据字符串是否符合给定的标准给出反馈。字符串应包含"1101"的子字符串,不包含任何字母。感谢您的帮助
#include <iostream>
#include <cstring> // for strstr
#include <string>
#include <cctype>
using namespace std;
bool stringCompare(char*y);
string str2;
int main ()
{
string str1, str2;
str1= "1101";
do
{
cout << "Please enter your string: " << endl;
cin >> str2;
while((stringCompare(str2)) == true)
{
if(strstr(str2.c_str(),str1.c_str())) // Primary string search function
{
cout << "ACCEPTED " << endl;
}
else
cout << "NOT ACCEPTED " << endl;
}
} while (2 > 1);
return 0;
}
bool stringCompare(char*y)
{
for(int a = 0; a < strlen(str2); a++)
{
if (!isdigit(str2[a]))
return false;
}
return true;
}
stringCompare
接受类型为char*
的参数,但您试图传递一个std::string
。那行不通。
您可以使用std::string
的c_str
方法来获得指向std::string
的内部char
数组的const char*
。这意味着您必须将参数设置为const char*
。
或者,更好的是,您可以将stringCompare
替换为引用std::string
:
bool stringCompare(string& y)
并且将CCD_ 12改变为CCD_。(或者更好的是,将整个循环简单地替换为:
for(char& ch : str2) // range-based for-loop loops over the entire str2
{
if (!isdigit(ch))
return false;
}
)
此外,您不需要比较返回值== true
。只需:
while(stringCompare(str2))
相关文章:
- 如何编写一个使用n倍三元条件语句的C++布尔函数
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 类中的虚拟布尔函数参数不起作用
- 我无法在 c++ 中调用布尔函数(已关闭)
- 如何使用布尔函数按降序对数组进行排序?
- 在布尔函数 c++ 中使用 elses
- 布尔函数在一段时间内不起作用
- 我正在尝试创建一个布尔函数,该函数计算字符并在字符为"()*-+"时返回 true
- 如何编写正确的"布尔"函数?
- 无法在 c++ 中" xor() "命名布尔函数
- 递归布尔函数的问题
- 布尔函数始终返回真正的 C++
- 具有单个参数的布尔函数映射
- 返回类型布尔函数中的声明语法错误
- 为什么我的布尔函数返回 true 会导致读取访问冲突?
- 单调布尔函数
- 布尔函数无法返回真或假
- 在C 中调用布尔函数
- 模板布尔函数没有<>,这是什么意思?