Safe get

This is a discussion on Safe get within the C Programming forums, part of the General Programming Boards category; Hey guys, I am trying to get a array input with mix with letters and numbers(but with pattern) etc.Qb2Ke4Rh4Xd4 I ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    Safe get

    Hey guys,
    I am trying to get a array input with mix with letters and numbers(but with pattern) etc.Qb2Ke4Rh4Xd4

    I know how to do the safe get for the string.
    Code:
    void safegets (char s[], int arraySize)
    {
        int i = 0, maxIndex = arraySize - 1;
        char c;
        while (i < maxIndex && (c = getchar()) != NEWLINE)
        {
            s[i] = c;
            i = i + 1;
        }
        s[i] = NULL;
    }
    How do i get the array input?
    or i can just use string and consider the number to be letter?
    Is there any function to do so?

    Thanks

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    I did not understand what you are saying. And I do not feel like doing so, please be more accurate next time.
    Take a look at atoi, in case this is what you are looking for.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can treat the numbers as just digits in a string, perhaps?

    Given this input: Qb2Ke4Rh4Xd4

    What do you want to put into an array - and what data type is this array? Can you show an example of what you want to put into this array?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Sorry about the confusion.
    The program is receive a input from user etc. Qb2Ke4Rh4Xd4,
    The letter Q or K represent the pieces on the Chess board. Q for queen, K for knight.
    The small letter b or e represents the horizontal coordinates on the chess board and the number represents the vertical coordinates.(all count from bottom left.)

    The program is to intake a input with a mix of letter and numbers with certain pattern.

    Then it will produce a 2 dimentional array which is a visual representation of the chess board with those picecs on it.
    etc.
    ........
    ........
    ........
    ........
    ...XK..R
    ........
    .Q......
    ........
    Qb2
    Ke4
    Rh4
    I have to list the pieces at bottom that is able to attack the x piece.

    My question is, when the user types the input in a form like this(Qb2Ke4Rh4Xd4 enter), how do I take them safely(i suppose i need to put them into an array first and process then later right?) Or My thinking is completely off the track?

    Thanks



  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,256
    So what you've done so far is to create a "safe" version of gets, except that you should initialize maxIndex = arraySize - 2; in order to leave room for the trailing null character ('\0'). (Note, microsoft already has a safe version of gets() called get_s(), but I assume that this was part of your class assignment).

    Assuming that the input is not messed up, then each piece / position information packet is exactly 3 characters, so there's not much point in making an array for that part.

    For the next part, how do you plan to represent the chess board in your program?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Thanks for your replay

    Quote Originally Posted by rcgldr View Post
    For the next part, how do you plan to represent the chess board in your program?
    I need to use a 2 dimensional array to printf it out.
    My thought is to fill the array with '.' first and put the letters(X Q K) in the 2 D array base on their coordinates.

    And you mentioned that i do not need to make a array for that part, However, The solution is not really obvious to me , Could you elaborate on that?
    When user input like (Qb2Ke4Rh4Xd4 enter), how do you get() it ?

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,256
    Quote Originally Posted by zhangz64 View Post
    I need to use a 2 dimensional array to printf.
    You can also use that array to help with the other parts of your program, not just for printing purposes.

    Quote Originally Posted by zhangz64 View Post
    You mentioned that i do not need to make a array for that part.
    What I meant was that you don't need to make separate arrays for the location of every piece since you'll have that information when you fill in the board. You'll just use 3 characters at a time from your input string to determine where each piece is supposed to go in that 2 dimensional array.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Quote Originally Posted by zhangz64 View Post
    Hey guys,
    I am trying to get a array input with mix with letters and numbers(but with pattern) etc.Qb2Ke4Rh4Xd4

    I know how to do the safe get for the string.
    Code:
     void safegets (char s[], int arraySize)
     {
         int i = 0, maxIndex = arraySize - 1;
         char c;
         while (i < maxIndex && (c = getchar()) != NEWLINE)
         {
             s[i] = c;
             i = i + 1;
         }
         s[i] = NULL;
     }
    How do i get the array input?
    or i can just use string and consider the number to be letter?
    Is there any function to do so?

    Thanks
    Hey there

    A few things I notice:

    NULL is not '\0' -> NULL is a pointer to address 0, change it to '\0'

    'maxIndex' is not needed, just use the arraySize variable (maybe change it to "emptyArraySpaces")

    You need to declare c as int, so that you can check for EOF - And then you need to check to see if getchar returns EOF.

    If there was too many character inputs, you may want to try and empty the input buffer (add a while(getchar() != '\n') on the event of i == emptyArraySpaces) - That way you won't get errors later on in your code

    I'd also change the function type from void to char* -> On the event of success, return s, otherwise return NULL.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this safe?
    By Shokwav in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2012, 02:36 PM
  2. Is this safe?
    By pheres in forum C++ Programming
    Replies: 22
    Last Post: 04-01-2008, 03:02 PM
  3. Is this safe?
    By caduardo21 in forum C Programming
    Replies: 55
    Last Post: 06-30-2005, 09:01 AM
  4. How Safe is your pc
    By GSLR in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-16-2003, 02:53 AM
  5. How safe is it?
    By hermit in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-08-2002, 09:33 PM

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