Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,376 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,156 people online right now. Registration is fast and FREE... Join Now!




Command line argument

 
Reply to this topicStart new topic

Command line argument

eudos
post 2 Sep, 2006 - 10:59 AM
Post #1


D.I.C Head

**
Joined: 23 Apr, 2006
Posts: 58


My Contributions


Hi there.

I am tryin to use a program using comand line argument of my own... i mean like when i enter "./list -r" it should print in reverse order.. but i have proplem in the code below..
Can anyone plz help me with this.. smile.gif

CODE

// Print in reverse - initially false
    int reverse = 0;

        if((argc == 2 ) && (argv[1]  == -r)) {
             reverse = 1;
        }else {
             printf("Pls use : %s [-r]", argv[0]);
        }


    // Print list
    printf("The list contains:\n");
    if (reverse) {
       list = reverseList (list);
       printRecursive(list);
    } else {
       printRecursive(list);
    }

User is offlineProfile CardPM

Go to the top of the page

Nova Dragoon
post 2 Sep, 2006 - 11:04 AM
Post #2


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,128



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


you need to do a string compare to find -r because, - and r are two letters which make a string
User is online!Profile CardPM

Go to the top of the page

Jayman
post 2 Sep, 2006 - 11:14 AM
Post #3


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


The problem is the argv[1] is a character array, you cannot make a literal comparision using the ==. So this statement (argv[1] == -r) will never work. You need to use the strcmp function to compare the the command argument with the expected value.

Function strcmp returns 0 if it matches and 1 if it does not. Here is a link that shows API for strcmp.

Should look like this:
CODE

if((argc == 2 ) && (strcmp(argv[1],"-r") == 0))
User is offlineProfile CardPM

Go to the top of the page

eudos
post 2 Sep, 2006 - 11:20 AM
Post #4


D.I.C Head

**
Joined: 23 Apr, 2006
Posts: 58


My Contributions


Thanks.. code is now perfect. biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

skyhawk133
post 2 Sep, 2006 - 11:33 AM
Post #5


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,844



Thanked 45 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


jayman and Nova, thanks for helping out eudos!

Eudos, I'm glad you got your code working, don't hesitate to come back to dream.in.code in the future if you have other questions.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 06:07AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month