I have a program that is using command line arguments to tell the program what to do. So to run the program, you type in: main airports.txt SC PER, where SC is a different thing the program can do and PER is the code for a city. So PER is argv[3].

In a different class (Path), I have a section of code that looks like this:

void Path::scPrint() const {
     
     cout << "*** Starting in " + scArg + " ***" << endl;
     cout << "Print out of all flights starting in particular city" << endl;
     
}

I want the variable scArg to be the string stored in argv[3] -- in this case, PER. I can't work out how to do this. Currently in Main, I have this:

Path* path1 = new Path();
    
    if (argv[2] = "SC") {
        //scArg to be given value of argv[3] here
        path1->scPrint();
    }

Can anyone help me figure out what to do? Thanks so much in advance!

What a wonderful teleportation of argv[3] to every class Path object do you want? If you have a "default" starting point of the Path object (probably it's the scArg member), provide this class for a correspondent constructor or default starting point setting member function - that's all.

>> if (argv[2] = "SC")
You can't compare character arrays using the = operator like you do VB. Use strcmp() function prototyped in string.h header file -- or <cstring> if you prefer. if( strcmp(argv[2], "SC") == 0)

Thanks for your help, Ancient Dragon, I was wondering why that wasn't working properly.

ArkM, I'm not quite sure what exactly you mean. I'm really new to C++. Can you explain a bit more clearly?

>I'm not quite sure what exactly you mean. I'm really new to C++. Can you explain a bit more clearly?
OK, let's come back to the class Path design. The class has a member function without parameters: scPrint. It prints (in a very strange manner) a message:

void Path::scPrint() const 
{
    cout << "*** Starting in " + scArg + " ***" << endl;
    cout << "Print out of all flights starting in particular city" << endl;
}
// That's right:
    cout << "*** Starting in " << scArg << " ***\n";
         << "Print out of all flights starting in particular city" 
         << endl;
}

Evidently a class object has a default starting point. In other words, a starting point name is an attribute of Path object. If so, we must define this attribute in Path constructor or later but before scPrint using - for example:

class Path
{
public:
    Path():scArg("Unknown") {}
    Path(const string& sc): scArg(sc) {}
    ...
    void setOrg(const string& sc) { scArg = sc; }
    string& getOrg() const { return scArg; }
...
private:
    string scArg;
    ...
};

Now you can create Path objects in the main:

Path* path = new Path(argv[3]);
// or
Path path;
...
if (string(argv[2]) == "SC") {
    path.setOrg(argv[3]);
    path.scPrint();
}

What's a problem?
Did you really not know what's a class constructor? ;)

Nah, it was the default starting point setting member function that had me stumped! A friend finally worked out that you meant an initialisation helper function and I was about to post that I'd fixed the problem when I saw your post.

Thanks for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.