小编sur*_*lyq的帖子

C++ switch语句,使用命令行参数

我试图使用switch语句来启动函数调用.需要在来自命令行参数的参数中传递给switch语句的值,这意味着它存储在argv []中.在我的例子中,已知这个参数存储在argv [5]中.我的问题是我不知道如何正确转换或存储argv [5]中找到的值,以便可以通过switch语句读取它.我尝试使用char*,char []和string作为变量'verbosity'的数据类型

我一直得到的错误是这样的:语句需要表达整数类型('char*'无效)开关(verbosity)^ ~~~~~~~~~

我的代码如下:

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;
}
Run Code Online (Sandbox Code Playgroud)

我应该使用某种格式将字符串从argv []传递到我的switch语句中吗?

c++ switch-statement command-line-arguments

2
推荐指数
1
解决办法
2051
查看次数