How can I check input characters one by one as part of an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nik35
    New Member
    • Oct 2011
    • 2

    How can I check input characters one by one as part of an array

    A move in chess (http://en.wikipedia.or g/wiki/Algebraic_notat ion_(chess)) can be written entering three characters:
    like: Ka1
    The first letter can be K, Q, R, N, B, P
    The second letter can be a, b, c, d, e, f, g, h,
    The third letter can be 1, 2, 3, 4,, 5, 6, 7, 8
    User can input 3 characters using the above rules
    If something is wrong with input characters the program must show error message (for example when inputs ka1 - the first letter must be capital between K, Q, R, N, B, P)

    I wrote a program but I don't know what mistakes I've done or if this can be done in a different way

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char kinisi[4];
    char a1, a2;
    int a3;

    int main (void)
    {
    printf("Please give white move:");
    gets(kinisi);
    kinisi[0]=a1;
    kinisi[1]=a2;
    kinisi[3]=a3;

    while (((a1 =='K') || (a1 =='Q') || (a1 =='R') || (a1 =='N') || (a1 =='B') || (a1 =='P')) && ((a2 =='a') || (a2 =='b') || (a2 =='c') || (a2 =='d') || (a2 =='e') || (a2 =='f') || (a2 =='g') || (a2 =='h')) && ((a3 <9) && (a3 >0))) {
    printf("white move ok\n");
    }
    }

    I've got Pelles compiler and the mistake that founds is:
    The function 'gets' is marked as deprecated
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    gets is deprecated because it will happily write past the end of the kinisi array if the user types more than three characters. Consider instead fgets(kinisi,4, stdin).

    The three lines after gets will copy the uninitialized values of a1,a2,a3 into kinisi; thus throwing away whatever you obtained with gets. Reverse the assignment: put a1-a3 on the left side of the equals sign.

    Assigning kinisi[3] to int variable a3 will not convert the character obtained from gets into an integer. Make a3 a char, assign kinisi[2] to it, and compare it to characters '1' through '8'.

    If you call fgets as I suggest above then it will only return the first three characters the user types. Your program will be unable to detect when the user types too many characters. Do you wish to print an error message if the user does that?

    Comment

    • nik35
      New Member
      • Oct 2011
      • 2

      #3
      Thank you for your reply.
      I did the changes (except: assign kinisi[2] to it, and compare it to characters '1' through '8'-I don't know how)
      The program doesn't find error now, but still it doesn't show the message "white move ok" propably the while condition is not right.
      Yes I want an error message when user types more than 3 characters and when he doesn't type the permitted characters. Should I use strlen()?
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      char kinisi[4];
      char a1, a2, a3;
      int x;

      int main (void)
      {
      printf("Please give white move:");
      fgets(kinisi,4, stdin);
      a1=kinisi[0];
      a2=kinisi[1];
      a3=kinisi[2];
      x=strlen(kinisi );
      if (x >3) {
      printf("Wrong move\n You gave more than 3 characters\n Please give white move");
      }
      while (((a1 =='K') || (a1 =='Q') || (a1 =='R') || (a1 =='N') || (a1 =='B') || (a1 =='P')) && ((a2 =='a') || (a2 =='b') || (a2 =='c') || (a2 =='d') || (a2 =='e') || (a2 =='f') || (a2 =='g') || (a2 =='h')) && ((a3 <9) && (a3 >0))) {
      printf("white move ok\n");
      }
      return 0;
      }

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Please use CODE tags for your source code. Select the source code and then press the # button in the window.

        a3 contains a character. You cannot compare the character value like this: ((a3 < 9) && (a3 > 0)).

        What do you want the while loop to do? As it stands now, the loop will cause "white move ok" to be printed repeatedly forever.

        Comment

        Working...