943,578 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 822
  • C++ RSS
Jun 15th, 2009
0

Using command line argument as variable in another class

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. void Path::scPrint() const {
  2.  
  3. cout << "*** Starting in " + scArg + " ***" << endl;
  4. cout << "Print out of all flights starting in particular city" << endl;
  5.  
  6. }

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:
C++ Syntax (Toggle Plain Text)
  1. Path* path1 = new Path();
  2.  
  3. if (argv[2] = "SC") {
  4. //scArg to be given value of argv[3] here
  5. path1->scPrint();
  6. }

Can anyone help me figure out what to do? Thanks so much in advance!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
catums14 is offline Offline
3 posts
since Jun 2009
Jun 15th, 2009
0

Re: Using command line argument as variable in another class

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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 15th, 2009
0

Re: Using command line argument as variable in another class

>> 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)
Last edited by Ancient Dragon; Jun 15th, 2009 at 8:53 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jun 15th, 2009
0

Re: Using command line argument as variable in another class

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
catums14 is offline Offline
3 posts
since Jun 2009
Jun 15th, 2009
0

Re: Using command line argument as variable in another class

>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:
C++ Syntax (Toggle Plain Text)
  1. void Path::scPrint() const
  2. {
  3. cout << "*** Starting in " + scArg + " ***" << endl;
  4. cout << "Print out of all flights starting in particular city" << endl;
  5. }
  6. // That's right:
  7. cout << "*** Starting in " << scArg << " ***\n";
  8. << "Print out of all flights starting in particular city"
  9. << endl;
  10. }
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:
C++ Syntax (Toggle Plain Text)
  1. class Path
  2. {
  3. public:
  4. Path():scArg("Unknown") {}
  5. Path(const string& sc): scArg(sc) {}
  6. ...
  7. void setOrg(const string& sc) { scArg = sc; }
  8. string& getOrg() const { return scArg; }
  9. ...
  10. private:
  11. string scArg;
  12. ...
  13. };
Now you can create Path objects in the main:
C++ Syntax (Toggle Plain Text)
  1. Path* path = new Path(argv[3]);
  2. // or
  3. Path path;
  4. ...
  5. if (string(argv[2]) == "SC") {
  6. path.setOrg(argv[3]);
  7. path.scPrint();
  8. }
What's a problem?
Did you really not know what's a class constructor?
Last edited by ArkM; Jun 15th, 2009 at 10:20 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 15th, 2009
0

Re: Using command line argument as variable in another class

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
catums14 is offline Offline
3 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to open a directory using createfile() ?
Next Thread in C++ Forum Timeline: missing type specifier - C++ does not support default-int





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC