Results 1 to 4 of 4

Thread: Passing Parameters From Command Line

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    Scotland
    Posts
    21

    Question Passing Parameters From Command Line

    Hi, I'm wanting to pass a value such as 3 to my program when I run it from the command prompt. How can I do this? Everything I try just either doesn't work or gives me errors

    Basically I have a menu screen and when I type the number 3 for example:

    C:\>Myprogram.exe 3

    I want it to pass the value 3 in to my program and automatically select the 3rd element in the menu.

    At the momemt all its doing is running the program and displaying the menu.

    Any ideas?

    Here is what I have so far:

    void main(int argc, char *argv[])

    // Additional arguments to enable running of the program from the //DOS prompt

    {

    int menunumber=(int)argv[1];

    // etc etc...

    Cheers

    CaptainChainsaw

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    You should use the function atoi from stdlib.h, to read the menu number.
    int menunumber = atoi(argv[1]);

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    Scotland
    Posts
    21
    Hi there riis, I tried that code you posted - thank you. I got my program going from the command prompt and it passed the value as it should do, although to get it working I had to comment out a line which said cin >> menunumber; . When I tried to run the program in the development environment ( Visual C++ 6 ) the program just hangs. I assume this is because the program expects a value to be passed as a string from the command prompt and when it doesn't get a value it can't assign anything to the variable 'menunumber'.

    Is there a way of saying something like:

    IF no value has been passed via the command prompt then menunumber=0; and read the number from the keyboard the value the user inputs ??

    How also can I get the program to ignore the cin >> menunumber; statement when passing a value via the command prompt?

    This would enable my program to work both from the command prompt and the development environment would it?


    Cheers

    CaptainChainsaw

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The argument named argc is the number of arguments passed to the app. It is always at least 1 (the program name itself), so you can test if it is greater than 1 and if yes, get the item number. Else get input from the user.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width