Command-line Arguments

Write a program that prints one complete line of output and nothing more. The output the program prints is "IN ORDER" if its command-line arguments are in alphabetical ascending order; it prints "NOT IN ORDER" if these arguments are out of order.

Can someone help me with this?
Start with this:
1
2
3
4
int main(int argc, char **argv)
{
    return 0;
}

argc contains the number of command line arguments. Note that the program name is always the first argument so argc is at least 1. argv points to the arguments themselves, so argv[0] is the name of the command, argv[1] is the first argument, argv[2] is the second etc.

Hope this helps.
No, I'm confused...would it be like this:
1
2
3
4
5
6
7
8
int main(int argc, char **argv)
{
argv[0]
argv[1]
argv[2]
{
    return 0;
}
Yes
1
2
3
4
5
6
7
8
9
int main(int argc, char **argv)
{
    argv[0] //this is the first command - it will always be the name of the program 
               //("program" or "program.exe" on windows, and "./program" on Linux)
    argv[1] //this is the first command entered by the user AFTER the name of the program
    argv[2] //the second command, and etc

    return 0;
}


For example, say I'm using windows and I launch my program called "MyProgram"


Command Prompt:
C:\Users\Me> MyProgram.exe 100 Lines Of Code

Now Windows invokes the program, feeding "MyProgram.exe" as "argv[0]", "100" as "argv[1]", "Lines" as "argv[2]", "Of" as "argv[3]", and "Code" as argv[4].

You don't have to worry about going out of bounds because "argc" will ALWAYS tell you how many arguments are being provided when the program is invoked.

I have a program called "Maze.exe" that traverses a maze in a text file.

Here is how I invoke in from the command line:
--------------------------------------------------------------------------------------------------------------

C:\Users\********\Documents\Visual Studio 2013\Projects\Maze\Debug>dir
Volume in drive C is OS
Volume Serial Number is ****-****

Directory of C:\Users\*********\Documents\Visual Studio 2013\Projects\Maze\De
bug

11/20/2014 11:06 PM <DIR> .
11/20/2014 11:06 PM <DIR> ..
11/20/2014 10:48 PM 56 input.txt
11/20/2014 11:06 PM 134,656 Maze.exe
11/20/2014 11:06 PM 783,920 Maze.ilk
11/20/2014 11:06 PM 1,708,032 Maze.pdb
4 File(s) 2,626,664 bytes
2 Dir(s) 288,124,624,896 bytes free

C:\Users\Kurt Slagle\Documents\Visual Studio 2013\Projects\Maze\Debug>Maze.exe i
nput.txt -dfs


#I'm not including the output from my program - it's functionality is not important here


I called the program with "Maze.exe", which becomes "argv[0]", I passed "input.txt" as the next argument, which becomes "argv[1]" and I passed "-dfs" as the last argument, which becomes "argv[1]".

The lab doesn't like that answer:

CTest.cpp:7: error: expected unqualified-id before '{' token

More Hints:
⇒ You almost certainly should be using: "
⇒ You almost certainly should be using: #
⇒ You almost certainly should be using: ++
Can you post the code you submitted?
1
2
3
4
5
6
7
8
 int main(int argc, char **argv)
{
argv[0]
argv[1]
argv[2]
{
    return 0;
}
There's an extra { on line 6. And you're missing semicolons on lines 3 through 5.

But you need to go beyond that - you don't know that there are exactly 3 arguments. And you need to check what those arguments contain so you can decide if they're in alphabetical order.

Write a program that prints one complete line of output and nothing more. The output the program prints is "IN ORDER" if its command-line arguments are in alphabetical ascending order; it prints "NOT IN ORDER" if these arguments are out of order.
Last edited on
Topic archived. No new replies allowed.