通过main传递参数-检查输入是否有效
Passing arguements through main - Checking to see if a valid input
我试图通过main传递参数,它工作得很好,然后我检查传入的参数是否包含正确的格式/值。然而,即使我传递正确的格式,它仍然显示有错误,下面是代码:
int main(int argc, char* argv[]) {
/* Check if arguments are being passed through */
if(argc == 1){
cout << endl << "--- ERROR ---" << endl;
exit(0);
}
/* Check if the first argument contains the correct data */
string file_name = argv[1];
/* Handle operation */
string operation = argv[2];
if(operation != "-t" || operation != "-r")
{
cout << "Something is not right";
}
}
如果我执行:cout << operation;
,那么当我运行应用程序时,通过-t传递结果将是:-t
。
谁能建议我哪里可能出错?
更新:我将传入这些参数:
./main something.wav -t
我正在等待if语句:
if(operation != "-t" || operation != "-r")
{
cout << "Something is not right";
}
返回负数,因为我输入的值是-t
if(operation != "-t" || operation != "-r")
{
cout << "Something is not right";
}
无论操作是什么,它必须要么不等于"-t"要么不等于"-r",因此将总是打印"Something is not right"。
我期待if语句:
,所以返回负值
因为我输入的值是-t
OR的后半部分为真。如果前半部分或后半部分为真,则OR为真。你想要((operation != "-t") && (operation != "-r"))
。这样,if
只会在输入不是-t 和(也不是-r)时触发。
相关文章:
- 检查输入是否不是整数或数字
- 检查输入 std::array 指针数据是否等于某个常量数组
- 如何检查输入是否有效?
- 为什么复制构造函数不需要检查输入对象是否指向自身?
- 我已经创建了一个模板函数,但是在检查输入是奇数还是偶数时收到错误 C++.
- 如果是数字或字符,请检查输入
- 尝试检查输入的文本与我自己的自定义答案
- 如何检查输入是否为字符并显示它是"invalid input",否则它将按原样运行
- 检查输入是否为字母数字时的逻辑错误
- 如何检查输入缓冲区是否为空
- 检查输入通信是否正确
- 如何检查输入文件是否已更新
- C++:检查输入数据
- 功能调用,该函数从文本文件-C 检查输入正确性
- 如何检查输入是整数,字符串还是浮动在C 中
- 检查输入是否为数字,否则将其视为字符
- 如何在牡丹加密中检查输入的密码是否正确
- 检查输入的文件是否有任何错误
- 检查输入值是整数类型
- 如何检查输入是否为整数/字符串