Thread: command line arguments

  1. #1
    Registered User
    Join Date
    Mar 2002

    command line arguments

    Heya fellow programmers
    I just ran into these two little variables that are used by convention for some time. argc and argv. How does one get to see the effects of this when running windows Me. For instance look at this program i pasted here below.

    #include <iostream.h>

    int main(int argc, char *argv[])
    if(argc != 2) {
    cout << "you forgot to type your name";
    return 1;

    cout << "Hello " << argv[1] << '\n';

    return 0;

    when i execute this tricky one all i get is the if statement output. Can someone just enlighten me a little please.
    Have a nice day.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Make sure you're entering the proper arguments:
    c:\cppTest Julienne Walker
    will bomb, but
    c:\cppTest Julienne
    works just fine.

    If you intend to have the program spit out both a first and last name then you want to test argc for 3 and print argv[1] and argv[2]. Spaces in between the arguments on the command line will cause them to be treated as completely different strings as opposed to one string with internal whitespace.

    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM