Thread: How to treat values on a command line as seperate

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    How to treat values on a command line as seperate

    Hi. I want to enter a number of chars on a command line. For example....... a b c 4 6

    How do I take these valuesin and treat them as seperate values?

    I want to use getchar instead of getch or scanf, thanks

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Have you looked at an example from the API reference? You should be able to modify that code easily to do what you want.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Quote Originally Posted by nadroj View Post
    Have you looked at an example from the API reference? You should be able to modify that code easily to do what you want.

    Hmmm, it's a bit limited to be honest. Perhaps this illustrates how restrictive getchar is

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I really don't see how it's limited. I think is capable of doing exactly what you've asked.

    If you know how many "char"s to ask for input, then just loop that many times and store each "getchar" value into some variable, say an array. If you dont know how many input values will be given, then there isn't really anything magical to do: you need a delimiter. In the example in the link I gave, the delimiter is a ".", which you can of course change.

    If you still think it is limited, then I think you're maybe missing something in your requirements that you haven't told us, in which case you should explain more what you need to do with examples.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Quote Originally Posted by nadroj View Post
    I really don't see how it's limited. I think is capable of doing exactly what you've asked.

    If you know how many "char"s to ask for input, then just loop that many times and store each "getchar" value into some variable, say an array. If you dont know how many input values will be given, then there isn't really anything magical to do: you need a delimiter. In the example in the link I gave, the delimiter is a ".", which you can of course change.

    If you still think it is limited, then I think you're maybe missing something in your requirements that you haven't told us, in which case you should explain more what you need to do with examples.
    That makes sense. I'm going to give that a try. Thanks. I'll get back if I'm struggling

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    I've written the following rough piece of code.......

    Code:
    for(i=0; i<2 && c!=' '; i++)
    {
            if(c != ' ')
    {
        c=getchar();
        putchar (c);
      }
    }
    What I'm hoping for here is that I want to take in 2 chars, but exclude spaces. Unfortunately I'm still seeing ' ' in putchar. Can anyone see where I'm going wrong?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You want to take a char in, before you make an if() statement involving it. Of course, c can only be ONE char, not two, so do you really want to skip every other char?

    The outer loop should be set to the strlen() of the line, OR be a while loop, that lasts until the end of the line (hopefully marked with a newline).

    IMO you should ditch getchar(), and use an index in a for or while loop, to get what you want. It's very easy that way.

    fgets() puts the char's into your char array, then you walk through each char, using an index:
    Code:
    for(i = 0; i < strlen(charArrayName); i++) {
      //your logic code goes in here - just an example:
      if(charArrayName[i] != ' ')
        putchar(charArrayName[i]);
    }

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Quote Originally Posted by Adak View Post
    You want to take a char in, before you make an if() statement involving it. Of course, c can only be ONE char, not two, so do you really want to skip every other char?

    The outer loop should be set to the strlen() of the line, OR be a while loop, that lasts until the end of the line (hopefully marked with a newline).

    IMO you should ditch getchar(), and use an index in a for or while loop, to get what you want. It's very easy that way.

    fgets() puts the char's into your char array, then you walk through each char, using an index:
    Code:
    for(i = 0; i < strlen(charArrayName); i++) {
      //your logic code goes in here - just an example:
      if(charArrayName[i] != ' ')
        putchar(charArrayName[i]);
    }


    I want to ignore white spaces

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf return values
    By pirog in forum C Programming
    Replies: 15
    Last Post: 09-13-2009, 03:58 AM
  2. Refreshing a seperate Dialog
    By LowlyIntern in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2009, 10:49 AM
  3. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  4. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 02:08 PM