[英]Parsing command line arguments in C++
Below is the sample code that I tried to solve.下面是我试图解决的示例代码。 Calculation of grades of students using stl maps.使用 stl 地图计算学生的成绩。
#include <iostream>
#include <iterator>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
int main()
{
typedef map<string, int>mapType;
mapType calculator;
int level;
string name;
//Student and Marks:
calculator.insert(make_pair("Rita", 142));
calculator.insert(make_pair("Anna", 154));
calculator.insert(make_pair("Joseph", 73));
calculator.insert(make_pair("Markus", 50));
calculator.insert(make_pair("Mathias", 171));
calculator.insert(make_pair("Ruben", 192));
calculator.insert(make_pair("Lisa", 110));
calculator.insert(make_pair("Carla", 58));
mapType::iterator iter = --calculator.end();
calculator.erase(iter);
for (iter = calculator.begin(); iter != calculator.end(); ++iter) {
cout << iter->first << ": " << iter->second << "g\n";
}
cout << "Choose a student name :" << '\n';
getline(cin, name);
iter = calculator.find(name);
if (iter == calculator.end())
cout << "The entered name is not in the list" << '\n';
else
cout << "Enter the level :";
cin >> level;
cout << "The final grade is " << iter->marks * level << ".\n";
}
Now I want to assume that my program takes in 2 arguments like student name and level.现在我想假设我的程序包含 2 个 arguments ,例如学生姓名和级别。 Something like就像是
$./calculator --student-name Rita --level 3 $./calculator --student-name 丽塔 --level 3
And my output should be something like marks*level.而我的 output 应该类似于标记*级别。 I tried doing a small piece of code separately but I am not getting it right.我尝试单独编写一小段代码,但我没有做对。
using namespace std;
const char* studentName ="--student-name";
int main(int argc,char* argv[])
{
int counter;
if(argc==1)
printf("\nNo Extra Command Line Argument Passed Other Than Program Name");
if(argc>=2)
{
printf("%s\n",argv[1]);
if(std::argv[1] == "--student-name")
{
printf("print nothing");
}
else if(argv[1]=="--level")
{
printf("%s",argv[2]);
}
}
return 0;
}
Anyone can guide me on this.任何人都可以在这方面指导我。 Thanks!谢谢!
For example in this if statement例如在这个 if 语句中
if(std::argv[1] == "--student-name")
(where you are using incorrectly the qualified name std::argv[1]
of the local (block scope) variable argv
instead of just argv[1]
) there are compared two addresses: the first one is of the string pointed to by argv[1]
and the second one is the address of the first character of the string literal "--student-name"
. (您错误地使用了本地(块范围)变量argv
的限定名称std::argv[1]
而不仅仅是argv[1]
)比较了两个地址:第一个是argv[1]
指向的字符串argv[1]
第二个是字符串文字"--student-name"
的第一个字符的地址。 As these are two different objects then their addresses are different,由于这是两个不同的对象,因此它们的地址不同,
To compare C strings you need to use the standard C function strcmp
declared in the header <cstring>
. To compare C strings you need to use the standard C function strcmp
declared in the header <cstring>
.
For example例如
#include <cstring>
//...
if( std::strcmp( argv[1], "--student-name" ) == 0 )
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.