Below is the sample code that I tried to solve. Calculation of grades of students using stl maps.
#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. Something like
$./calculator --student-name Rita --level 3
And my output should be something like marks*level. 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!