| | |
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:
Solved Threads: 0
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:
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:
Can anyone help me figure out what to do? Thanks so much in advance!
In a different class (Path), I have a section of code that looks like this:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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!
>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:
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:
Now you can create Path objects in the main:
What's a problem?
Did you really not know what's a class constructor?
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)
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; }
C++ Syntax (Toggle Plain Text)
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; ... };
C++ Syntax (Toggle Plain Text)
Path* path = new Path(argv[3]); // or Path path; ... if (string(argv[2]) == "SC") { path.setOrg(argv[3]); path.scPrint(); }
Did you really not know what's a class constructor?
Last edited by ArkM; Jun 15th, 2009 at 10:20 am.
![]() |
Similar Threads
- Passing command line argument to function (C++)
- Trouble Using Command Line Argument (C)
- Command-line argument syntax (C++)
- What is the Command-Line Argument for??? (C++)
- command line arguments problem. (Shell Scripting)
Other Threads in the C++ Forum
- Previous Thread: How to open a directory using createfile() ?
- Next Thread: missing type specifier - C++ does not support default-int
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






