So far I have a very basic application that can read simple arguments such as -t or -s. What I want to do is when the program picks up on the argument it will run a function. The two things I am not sure of though is how to read anything after the first argument and then what to store that second argument as to pass it to the function.
So, what I want to happen is the following pseudo code:
Terminal: app-name -t userEnteredText
Application then sees that "-t" was used and then passes "userEnteredText" to argumentT() and then argumentT() returns the results and then the application couts the info.
All I really need is to know how to read the text entered after -t.
Below is the code I have so far:
Code:#include <iostream> using namespace std; //This is where we are going to initialize our functions. //int x and int y are just space holders for now. int argumentT(int x, int y); int argumentS(int x, int y); int main(int argc, char *argv[]) { //Check to see if the user entered any arguments //if argc does not equal 2 then they did not. if(argc != 2) { printf("You did not tell me what to do!\n"); } else { //Assuming that argc does equal 2 then we see what //the user entered and then pass that info on to //the corresponding function. if(strcmp(argv[1],"-t")==0) { //This was just a test to see if the argument could be read. int x = 1; int y = 2; cout << argumentT(x, y) <<"\n"; } if(strcmp(argv[1],"-s")==0) { //This was just a test to see if the argument could be read. int x = 1; int y = 2; cout << argumentS(x, y) <<"\n"; } } } //This is the function for the -t argument. int argumentT(int x, int y) { return x + y ; } //This is the function for the -s argument. int argumentS(int x, int y) { return x * y; }



LinkBack URL
About LinkBacks



