0

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!

1
  • To compare two C strings you should use the function strcmp. Commented Jun 14, 2020 at 18:06

1 Answer 1

1

For example in this if statement

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". 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>.

For example

#include <cstring>

//...

if( std::strcmp( argv[1], "--student-name" ) == 0 )
// ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.