Using command line argument as variable in another class

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 3
Reputation: catums14 is an unknown quantity at this point 
Solved Threads: 0
catums14 catums14 is offline Offline
Newbie Poster

Using command line argument as variable in another class

 
0
  #1
Jun 15th, 2009
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:
  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:
  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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Using command line argument as variable in another class

 
0
  #2
Jun 15th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Using command line argument as variable in another class

 
0
  #3
Jun 15th, 2009
>> 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: catums14 is an unknown quantity at this point 
Solved Threads: 0
catums14 catums14 is offline Offline
Newbie Poster

Re: Using command line argument as variable in another class

 
0
  #4
Jun 15th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Using command line argument as variable in another class

 
0
  #5
Jun 15th, 2009
>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:
  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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: catums14 is an unknown quantity at this point 
Solved Threads: 0
catums14 catums14 is offline Offline
Newbie Poster

Re: Using command line argument as variable in another class

 
0
  #6
Jun 15th, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC