C++开关语句,使用命令行参数
C++ switch statements, using command line arguments
我正在尝试使用switch语句来启动函数调用。需要在来自命令行参数的参数中传递到switch语句中的值,这意味着它存储在argv[]中。在我的例子中,已知这个参数存储在argv[5]中。我的问题是,我不知道如何正确转换或存储argv[5]中的值,以便switch语句可以读取它。我尝试使用char*、char[]和string作为变量"verbose"的数据类型
我一直得到的错误是:语句需要整数类型的表达式('char*'无效)开关(冗长)^~~~~~~
我的代码如下:
char* verbosity = argv[5];
cout << endl << verbosity;
switch(verbosity)
{
case 'vlow':
{
vlow();//calls vlow function
break;
}
case 'vmed':
{
vmed();//calls vmed function
break;
}
case 'vhigh':
{
vhigh();//calls vhigh function
break;
}
default:
break;
}
我应该使用某种格式将argv[]中的字符串传递到switch语句中吗?
'vlow'
是一个多字符文字,而不是字符串文字。
要比较C样式字符串,请使用std::strcmp
,如下所示:
if (std::strcmp(verbosity, "vlow") == 0)
{
//process
}
请注意"vlow"
与双引号的使用。
相关文章:
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- 使用 C++ 将命令行参数拆分为参数/向量
- 如何在OMNET++中添加专门的命令行参数?
- 如何在不传递命令行参数的情况下在 c++ 中设置环境变量
- atoi() 在应用于大型命令行参数时会产生不正确的值
- 命令行参数,cant 或两个变量
- 在 Windows 中使用 boost::p rogram_options 从命令行参数读取 Unicode 字符
- 如何在 Android/NDK 上将命令行参数从 gradlew.bat 传递到 Clang
- 编写一个将 LLVM IR 文件作为命令行参数的程序
- 通过命令行参数获取llvm ir文件时面临问题
- 有没有办法根据命令行参数定义数组大小?运行时与编译时实例化?
- 如何在不使用文件扩展名的情况下使用命令行参数打开C++中的文本文件?
- 命令行参数在不到 3 个 LOC 中 int?
- 在命令行参数中使用引号
- 在VS2013中使用devenv (C++)传递命令行参数argv
- 二进制数据作为命令行参数
- 如何在 C++11 中创建具有命令行参数大小的动态数组?
- 需要有关如何设置getopt_long以正确传递命令行参数的建议
- Bash 脚本和命令行参数