Like Tree1Likes
  • 1 Post By c99tutorial

Help with inputs

This is a discussion on Help with inputs within the C Programming forums, part of the General Programming Boards category; Hey All I've just started learning the C programming language at uni and am in need of some help. If ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    7

    Help with inputs

    Hey All
    I've just started learning the C programming language at uni and am in need of some help.

    If I have a input such as "3H 6S KH 7D AC"

    Is there a way to group the second and first terms in the input e.g

    First terms= 3,6,K,7,A
    Second terms= H,S,H,D,C

    Also is there a way to give the letters numerical values within the source code such as

    J=11
    Q=12
    K=13

    Any help would be appreciate my knowledge of C is very basic
    Thanks
    Murock

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    333
    The first question is easy. Simply have two parallel arrays, one of letters and one of values. If the letters go A-Z with no gap, you don't even need an actual array of letters, just access with values[ch - 'A'];

    For the second question, you can write scratch code to handle this particular case. But really what you need to do is to define exactly what inputs are allowed (are they always pairs, always separated by a single space, are there always five pairs in a line?). Then you need to say what information you are getting, for example is a list of characters adequate, or do you need more structure?

    An example function would be

    Code:
    /*
      get first and second terms in list of values
      Params: input - input in the form 3H 6S KH 7D AC
                 frist - return pointer for first values in pair
                 second - return pointer for second values in pair.
      Returns: number of pairs parsed, -1 on error
    */
    int parselist(const char *input, char *first, char *second)
    {
      /* logic goes here */
    }
    
    /*
       Now to use it
    */
    char *line = getinput();
    char ch1[256];
    char ch2[256];
    int Nread;
    int i;
    
    Nread = parselist(line, ch1, ch2);
    if(Nread <= 0)
      /* error in input, handle here */
    else
    {
       for(i=0;i<Nread;i++)
         printf("Pair %d char one %c char two %c\n", i, ch1[i], ch2[i]);
    }
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    There are always 5 its for 5 cards so like A=Ace H=Hearts 2=2 etc so they are always in this format

    So for the arrays would I do something like

    char suit[3];
    char value[12];
    Last edited by murock; 12-30-2012 at 03:24 PM.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Also found this
    if (('2' <= CARD) && (CARD <= '9')) VALUE = CARD - '2';
    else if ('J' == CARD) VALUE = 11;
    else if ('Q' == CARD) VALUE = 12;
    else if ('K' == CARD) VALUE = 13;
    else if ('A' == CARD) VALUE = 1;

    could be helpful?

    If I could somehow apply this to work for all 5 card value inputs
    Last edited by murock; 12-30-2012 at 04:47 PM.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    If the 10 card is represented with a single value like T I would just make a string to search for the value, since all the values are single characters. The code you found is also useful as an alternative, but it doesn't handle the 10 card. It should be easy to fix though if you know what's happening.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by murock View Post
    So for the arrays would I do something like

    char suit[3];
    char value[12];
    There are usually 4 suits and 13 values in a deck.

    Code:
    char array[n]
    is an array for n chars with valid indeces 0 <= i < n (i.e. 0, 1, 2, ..., n-1)

    Bye, Andreas

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    so isn't char suit [3] ok since its makes (0,1,2,3) giving 4?
    My ten card has to be represented in the input as the number "0" which i am finding difficult to change its value to 10 for obvious reasons.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,062
    Quote Originally Posted by murock View Post
    so isn't char suit [3] ok since its makes (0,1,2,3) giving 4?
    No. Declaring a character array "char suit[3]" means that there are three elements, called suit[0], suit[1] and suit[2]. Each element stores a char.

    Quote Originally Posted by murock View Post
    My ten card has to be represented in the input as the number "0" which i am finding difficult to change its value to 10 for obvious reasons.
    What's wrong with using '0' to represent the 10 card? Choosing a character to represent each card is up to you. You could use '!' or '$' to represent 10 if you want. Each character, written inbetween single quotes, is actually a small integer corresponding to your character encoding, so the value 10 (without single quotes) is also a valid value.

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Anyone know a good place for tutorial videos or just plain tutorials not sure how to do a string search

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    There are a lot of tutorials on algorithms and data structures here: Algorithms and data structures in C/C++ - Cprogramming.com. There are also books written on this subject too. In particular I think the article on linear search will help you: Linear Search and Binary Search - Cprogramming.com. After you learn what linear search is, all you have to do is write one for your purpose. For example if I make the values one long string like "234567890JQKA" and as input I have 9, the search will return the location in the string, 7, which is the value of the card under this scheme.

  11. #11
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Right well I know how a linear search works now But I'm still lost on how to do this could you guys show me an example lets be simply say my stdin is "3H KS" and I want to split the variables up so that 3 is a stand alone variable from H and K is stand alone from S. I'm not sure if this makes sense. So my stdin is in the form "ab cd" and a=3, b=15, c=13 d=16.
    So eventually i want to end up with a program that can do the below.


    0=10 H=15
    J=11 S=16
    Q=12 C=17
    K=13 D=18
    A=14


    inputs are in the form
    ab cd ef gh ij

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,287
    Perhaps you could write a function to return a value based on the character passed to it. Then you can just read the input as a string, and run a loop that calls this function for each character in the string.

    Code:
    int getValue(char userInput)
    {
        int result = 0;
    
        switch(userInput)
        {
            case '2':
                result = 2;
                break;
            case '3':
                result = 3;
                break;
            // ...
        }
    
        return result;
    }
    This is not a full solution, but might help you get started with an easier implementation.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    inputs are in the form
    ab cd ef gh ij
    1. Read a.
    2. Read b.
    3. Pass argument a to search function to find value.
    4. Pass argument b to other function to verify suit

    Assuming these are successful, a and b compose a card. Now you just do that for all of the input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing Inputs
    By aaleclaire in forum C++ Programming
    Replies: 9
    Last Post: 06-18-2012, 09:37 PM
  2. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  3. Help with inputs
    By RustySpoon in forum C Programming
    Replies: 3
    Last Post: 03-14-2010, 09:38 PM
  4. getting inputs from other windows
    By ballackz in forum Windows Programming
    Replies: 3
    Last Post: 12-26-2004, 03:19 PM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21