c++ - 通过引用传递的 C 字符串的输入验证

标签 c++ cstring c-strings

<分区>

#include "cstack.h"
#include <iostream>
#include <cstring>
using namespace std;

bool isValidExpression (CStack&, char*);

int main (void)
{
    char expression[21];
    expression[0-21]=0;
    cout<< "Enter an expression: ";
    cin >>expression;
    CStack stack1;

    if (isValidExpression (stack1, expression)==true)
    {
        cout << "\nIt's a valid expression";
    }
    else
    {
        cout << "\nIt's NOT a valid expression";
    }
    return 0;
}

bool isValidExpression (CStack& stackA, char* strExp)
{
    for(int a=0;a<21 && strExp[a]!=0;a++)
    {
        if(strExp[a]="}"||"]"||")") //This is the issue right here
        {
            cout<<"Action A" <<endl;
        }
        else
        {
            stackA.push(strExp[a]);
        }
    }
    return true;
}

我遇到的问题是,无论我输入什么,操作 A 总是会发生。例如,如果我输入 [,我仍然会得到不是预期结果的操作 a。对于这样的事情,我一直使用字符串,但是我们需要在这个程序中使用 cstring。您将如何编辑它以使其发挥作用?

最佳答案

尝试更新:

if(strExp[a]="}"||"]"||")")

到:

if(strExp[a]=='}'|| strExp[a]==']'|| strExp[a]==')' )

关于c++ - 通过引用传递的 C 字符串的输入验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15400058/

相关文章:

c++ - 返回 CString 将调用析构函数?

string - 用C样式的字符串调用C函数

string - 如何将C字符串转换为D字符串?

c++ - 是否可以预测 srand(time(0)) 的随机数?

c++ - 为什么我不能在 linux 的代码块中使用 "auto"

c++ - OpenCV HOGDescriptor 返回值

c++ - cin 和 boolean 输入

swift - 在 Swift 中使用 C 字符串,或 : How to convert UnsafePointer<CChar> to CString

c++ - 对混合(空和非空)cstring 数组进行排序

有人可以向我解释使用指针与索引访问数组的性能差异吗?