5 Replies - 7893 Views - Last Post: 01 May 2009 - 02:30 PM Rate Topic: -----

#1 skywaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-May 09

c/c++ accept input from command line

Post icon  Posted 01 May 2009 - 02:52 AM

hi , new to c/c++ but i need to accept input as a parameter from command line
Is This A Good Question/Topic? 0
  • +

Replies To: c/c++ accept input from command line

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: c/c++ accept input from command line

Posted 01 May 2009 - 02:55 AM

Welcome to DIC!

Show us some code making an attempt to do this and we can help you correct it or improve it.
Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: c/c++ accept input from command line

Posted 01 May 2009 - 12:31 PM

This is one way to accept command line arguments.

argc stands for the number of arguments ( Always >= 1 because the program name counts as an argument )

argv is the contents of the passed in argument.

argv[0] is always the program name

argv[1] is the first arg

argv[2] is the second arg and so on.

int main(int argc, char* argv[])
{
  if(argc != 2)
  {
    cout << "Error: Incorrect number of args\n";
  }

  if(strcmp(argv[1], "My command line arg") == 0)
  {
  /* Do something */
  }

  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 theCcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-April 09

Re: c/c++ accept input from command line

Posted 01 May 2009 - 01:53 PM

I havn't done much work with command-line arguments, but don't use argc the way you're using it in the above; argc is a counter for the number of given arguments.

This post has been edited by theCcoder: 01 May 2009 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: c/c++ accept input from command line

Posted 01 May 2009 - 02:12 PM

It's much easier if you're asking a user for CMD line input if you just
int main()
{ 
int z;

cout << "please enter a number" << endl;
cin >> z;
cout << "the number you entered was" << z << endl;
getchar();
}


This post has been edited by IngeniousHax: 01 May 2009 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: c/c++ accept input from command line

Posted 01 May 2009 - 02:30 PM

View PosttheCcoder, on 1 May, 2009 - 12:53 PM, said:

I havn't done much work with command-line arguments, but don't use argc the way you're using it in the above; argc is a counter for the number of given arguments.


Some programs need a certain number of arguments to run. That is a way of using argc.

You can compare argc however you would like.
i.e. Greater than, Less than, equal too, not equal too.

I believe that skywaker was referring to passed in command line arguments. I may be wrong but, that is what I got out of his post.

This post has been edited by eker676: 01 May 2009 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1