Thread: How to use int argc, char* argv[] ?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Unhappy How to use int argc, char* argv[] ?

    Hi,

    I am not sure how to use int argc, char* argv[]. Anyone could tell me when should I use it?

    Also, I cannot compile the following code. The error shows initialization of 'i' is skipped by 'default' label. What does it mean?

    #include <iostream>

    using namespace std;

    int main(int argc, char* argv[])
    {
    switch(argc-1)
    {
    case 2:
    case 3:
    case 4:
    for(int i = 1; i < argc; i++)
    cout << "Argument " << i << " is " << argv[i] << endl;
    break;
    default:
    cout << "You entered the incorrect number of arguments. " << endl
    << "Please enter 2, 3 or 4 arguments only. " << endl;
    }
    return 0;
    }

    Thanks in advance.

    gogo

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I am not sure how to use int argc, char* argv[]. Anyone could tell me when should I use it?
    Whenever you need to access parameters on the command line

    Your program looks OK as it stands.

    > The error shows initialization of 'i' is skipped by 'default' label.
    It means you have an older C++ compiler

    Originally, the declaration within the for loop meant this
    Code:
    int i;
    for( i = 1; i < argc; i++)
        cout << "Argument " << i << " is " << argv[i] << endl;
    The scope of i was from just before the for loop, to the end of the enclosing } (in this case, your switch statement).
    Basically, you have a variable declared (i), which is not initialised in all cases.

    A newer compiler declares things like so (in effect)
    Code:
    { int i;
    for( i = 1; i < argc; i++)
        cout << "Argument " << i << " is " << argv[i] << endl;
    }
    Now the scope of i is much less - certainly not the whole case statment.

    The GNU compiler has a switch to turn this on and off
    > gxx -fno-for-scope cpr-test.cpp
    cpr-test.cpp: In function `int main(int, char **)':
    cpr-test.cpp:17: jump to case label
    cpr-test.cpp:14: crosses initialization of `int i'

    > gxx cpr-test.cpp

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Thanks for your kind help. May I ask you how should I compile it through VC++? I do not have any MS-Dos mode compiler.

    Thanks

    gogo

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > May I ask you how should I compile it through VC++?
    Like you already are, but without that warning?

    Dunno - declare 'i' before you enter the switch statement, not when you declare the for loop.

    Or perhaps
    Code:
    case 4: 
    { // spoof an extra scope 
         for(int i = 1; i < argc; i++)
         cout << "Argument " << i << " is " << argv[i] << endl;
    }
    break;
    Compiler's broken - the rest is hackery

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM