Thread: Command Line Arqument

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    59

    Command Line Arqument

    I am stuck here , i need some help here , I am trying to read 10 command line arquments ignoring digits and special characters....I have tried

    Code:
    isalpha( argv[x] )
    but i found out that it only takes an int....

    this is what I have done so far ....

    Code:
    
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NB_OF_ARGS 
    
    int main(int argc, char **argv) {
        if (argc != NB_OF_ARGS + 1) {
            printf("10 command line arguments please.\n");
            return EXIT_FAILURE;
        }
        
        for (int i = 1; i <= NB_OF_ARGS; ++i)
    
            printf("  %s", argv[i]);
    
        putchar('\n');
        return EXIT_SUCCESS;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by code_lover
    but i found out that it only takes an int....
    Therefore, check all the characters of the string using isalpha, say by writing a function with a loop.

    By the way, it looks like you did not actually set NB_OF_ARGS to an integer value, and then since you are using NB_OF_ARGS, your error message should incorporate that instead of hard coding 10.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    59

    Therefore, check all the characters of the string using isalpha, say by writing a function with a loop.
    What do you mean ..please explain to me ,i didn,t understand .

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose I provide you with a microscope and ten say, cell samples to examine. The microscope can only be used to examine one cell sample at a time. Will you tell me that the task of examining the ten cell samples is impossible with this microscope, or will you simply examine each cell sample in turn?

    isalpha can only check if a single char is alphabetic. How can you possibly use it to check if an entire string is alphabetic?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by code_lover View Post
    but i found out that it only takes an int....
    this is what I have done so far ....

    Code:
    #define NB_OF_ARGS
    And now many args do you need? (It needs a number!)

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    How Can I read char by char of the argument ....let me say we have two arquments " James John"
    how can i read char by char of j a m e s ......

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How would you access the first character?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by code_lover View Post
    How Can I read char by char of the argument ....let me say we have two arquments " James John"
    how can i read char by char of j a m e s ......
    Run a loop across each string, copying out the desired characters as you go...

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    I can access the first character by argv[1][0]

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by code_lover
    I can access the first character by argv[1][0]
    Right. Therefore, you are not far off from writing a loop to loop over the characters of the string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What would you do to change to actually counting instead of having a static number there? Go read the loops and arrays tutorials until you can answer that.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    What would you do to change to actually counting instead of having a static number there? Go read the loops and arrays tutorials until you can answer that.
    Quzah.
    Interestingly enough... he knows how to print all 10 of them with a loop...

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Interestingly enough... he knows how to print all 10 of them with a loop...
    /facepalm


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    If using loop to find each character ....How to i know the size of each arguments
    Code:
    for (int i = 1; i <= NB_OF_ARGS; ++i)
          for(int j = 0 ; ??? ; ++j)

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If only there were some way to tell when a string ended...


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line how to handle optional command.
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2010, 11:41 PM
  2. Run from command line
    By JStrobel in forum C++ Programming
    Replies: 6
    Last Post: 01-26-2009, 02:40 AM
  3. Command-Line MFC
    By Pegarus in forum Windows Programming
    Replies: 3
    Last Post: 09-22-2008, 05:21 PM
  4. command line
    By mariacolette in forum C Programming
    Replies: 4
    Last Post: 01-14-2008, 10:16 AM
  5. C in command line
    By ZakkWylde969 in forum Linux Programming
    Replies: 2
    Last Post: 12-30-2003, 09:30 AM