I wish there was an option in the "What are you posting" for tutorials hehe. But I guess I will just have to right it here :). If anyone else has any pointers (pun intended) let me know ;).

Ok if your like me and were wondering how to accept command line arguements I am going to show you how (I couldn't find any detailed enough to explain to me anyways).

If you have ever seen some code where int main has a some parameters but weren't sure what they were?

int main(int argc, char *argv[]){
}

well here it is broken down

/*int argc is the number of arguements that your program will take
and char * argv[] is a (correct me if I'm wrong experts) pointer char array (an array of pointers?) and it stores what arguements are passed to the program.
IMPORTANT: always assume argv[0] is the program name otherwise your program won't work.

Now i will show you a few examples
Example one:

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[0])
{
  if(argc!=2){
      cout<<argv[0]<<" string"<<endl;
  }
  else{
  cout<<argv[1];
  }
}

ok so lets break it down :P

if(argc!=2){
      cout<<argv[0]<<" string"<<endl;
  }

that says if there isn't an arguement then print program's name and string
SUPER NOTE: remember that the last spot in any array is reserved

next:

else{
  cout<<argv[1];
  }

basically if there is an arguement print it out
and last example is the code that i wrote with my current knowledge of c++ and ive commented it so as too keep this sort of short

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc,char * argv[]){
    string input;                   //someplace to store our input :)
    int x,y,x1,x2;                  //x and y are for the loop and the main if check and x1 and x2 are for addition.
    if(argc!=2){                    // if there are no arguements cout usage
        cout<<"Usage: "<<argv[0]<<" name"<<endl; // argv[0] is the programs name even if you rename it.
    }
    else{
        ofstream file (argv[1]);    //the 1st arguement is our output files name
                                    //NOTE: that you can put anything where file is but you have to change all subsequent calls to it.
        loop:                       // our goto loop , you'll see later.
        if(y!=1){                   // Main if check
        cout<<"Hello World!,\nWelcome to Test "<<argv[1]<<endl;
        cout<<"Commands: add[2] "<<endl;
        cout<<"Enter a Command: ";
        cin>>input;
        for(x=0; x<1;){             // Main loop
            if(input=="add[2]"){    //if user typed add[2] then etc...
                cout<<"Enter a Number for X1: ";
                file<<"Enter a Number for X1: ";
                cin>>x1;
                file<<x1;          //VERY important the file part says to output the value of x1 to the file (in my case test.txt)
                cout<<"\nEnter a Number for X2: ";
                file<<"\nEnter a Number for X2: ";
                cin>>x2;
                file<<x2;
                cout<<"\nAnswer: "<<x1+x2<<endl;
                file<<"\nAnswer: "<<x1+x2<<endl;
                cout<<"Press Enter to Continue..."<<endl;
                cin.get();
                goto loop;            // goto loop. repeat as long as input is not exit. if input isnt either one the print add[2]
            }
            else if(input=="exit")
        {
            y=1;
            cout<<"exiting..";
            goto loop;
        }
            else {
                cout<<"Enter add[2]";
                 goto loop;     // input check
            }
        }
        }
    }
}
                                // and we are  done :)

Hope this helps some people out it took me awhile to figure this out :)

Note for Admins and moderator: I noticed that we only have one tutorial it would be great if we had more which is why i would like to recommend adding a tutorial spot in the drop bar of What are you posting :)

Recommended Answers

All 4 Replies

Sorry for double post but if there are any grammatical errors let me know or if you have any feedback :)

I wish there was an option in the "What are you posting" for tutorials hehe. But I guess I will just have to right it here :). If anyone else has any pointers (pun intended) let me know ;).

Ok if your like me and were wondering how to accept command line arguements I am going to show you how (I couldn't find any detailed enough to explain to me anyways).

How about this?

commented: But that's written by some dude named WaltP at that other site. You must have stolen his username (and avatar) for this site +2

Thanks WaltP that helped me alot :) but i still wanted to write the tutorial since we only have one here in the c++ forum :)

A tutorial has to be approved by either Dani or Davey, and for this to happen the tutorial has to meet their (very high) standards. That's the reason why we only have 1 tutorial in C++.

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.