Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

What comand line code on lesson 3 excersice 5 suppose to do?

Hi

I am new to thie C# world, but I have a question regarding the following exercise of the school (Lesson 3) Excersise 5 (Write a program that prints all the command line arguments).

The code reads as follows:

using System;

namespace CSharpSchool
{
// program that prints all the command line arguments
class CommandLineArgs
{
static void Main(string [] args)
{
for(int i=0; i<args.Length; i++)
{
Console.WriteLine
("The {0}th command line argument: {1}",
i, args[i]);
}
}
}
}

QUESTION:

What am I suppose to see on the command screen?
What do you mean by comand line argument? I cannot see at any point the display of "The {0}th command line argument: {1}". Instead I only can see the string "Press any key to continue".

Thanks !!

Comments

  • BaldusariusBaldusarius Member Posts: 162
    : Hi
    :
    : I am new to thie C# world, but I have a question regarding the following exercise of the school (Lesson 3) Excersise 5 (Write a program that prints all the command line arguments).
    :
    : The code reads as follows:
    :
    : using System;
    :
    : namespace CSharpSchool
    : {
    : // program that prints all the command line arguments
    : class CommandLineArgs
    : {
    : static void Main(string [] args)
    : {
    : for(int i=0; i<args.Length; i++)
    : {
    : Console.WriteLine
    : ("The {0}th command line argument: {1}",
    : i, args[i]);
    : }
    : }
    : }
    : }
    :
    : QUESTION:
    :
    : What am I suppose to see on the command screen?
    : What do you mean by comand line argument? I cannot see at any point the display of "The {0}th command line argument: {1}". Instead I only can see the string "Press any key to continue".
    :
    : Thanks !!
    :

    It will print out the arguments supplied on the command line. For example:[code]appName.exe HeresAnArg Arg2[/code]will output[code]The 0th command line argument: HeresAnArg
    The 1th command line argument: Arg2[/code]If no arguments are specified on the command line, the for loop will never execute and no ouput will be produced.

    As an aside, you can see from the sample output that the string supplied to Console.WriteLine doesn't produce grammatically correct output (1th) and the second argument should be i+1 instead of i.

  • frazrasheedfrazrasheed Member Posts: 18
    Hi,

    To use this program, you need to run the program on command prompt. To run the program, just compile it and run the generated .exe file with some arguments ahead of it. For example, if you have named the program myProgram and compiled it to the myProgram.exe file, then if I write on command prompt:

    C:>myProgram abc xyz

    Then it will print 0th argument as 'abc' and '1th' argument as 'xyz'. We purposefully and intentionally did not change the first item's position from 0 to 1 here as we liked to make it very clear that when talking about arguments (or in general arrays in C#) then the first item always has the index value zero (0), second item has one (1) and so on. But if you like the other way, you can print simply i+1th value to print 1th value with first item and 2th value with second item

    Regards,
    Faraz

    : Hi
    :
    : I am new to thie C# world, but I have a question regarding the following exercise of the school (Lesson 3) Excersise 5 (Write a program that prints all the command line arguments).
    :
    : The code reads as follows:
    :
    : using System;
    :
    : namespace CSharpSchool
    : {
    : // program that prints all the command line arguments
    : class CommandLineArgs
    : {
    : static void Main(string [] args)
    : {
    : for(int i=0; i<args.Length; i++)
    : {
    : Console.WriteLine
    : ("The {0}th command line argument: {1}",
    : i, args[i]);
    : }
    : }
    : }
    : }
    :
    : QUESTION:
    :
    : What am I suppose to see on the command screen?
    : What do you mean by comand line argument? I cannot see at any point the display of "The {0}th command line argument: {1}". Instead I only can see the string "Press any key to continue".
    :
    : Thanks !!
    :

Sign In or Register to comment.