Thread: entering numbers to array in a row whithout length input

  1. #46
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in option 2
    when i enter a string it goes threw a process of cutting the spaces.

    but although it cuts the spaces from them,it adds a " char in the end of the string

    and sends it to the is_valid with " in the end
    i printed the string before and after and this " char comes from nowhere?
    Code:
    #include <stdio.h>
    
    #define N 9
    int is_valid(const char input[],int leng);
    void mainPlayGame(board);
    int playGame(char board[N][N],int size);
    void printBoard(char board[N][N],int size);
    int removeMine(char board[N][N],int size,int x,int y);
    void mainMenu();
    int unCover(char board[N][N],int size,int x,int y);
    
    int main(){
     char input[40];
     char input2[40];
    char board[N][N];
    mainPlayGame(board);
    printf("bye, please press enter to exit!\n");
    getchar();
    return 0;
    }
    
    void mainPlayGame(board){
        int opt,leng,size=-1;
      int index,kndex;
        char input[40];
        char ch_cords[40];
        char input2[40];
        int tr;
        int cords[8][2];
        int tndex;
    
        int i,k,j,ch,l;
    
        int  leng2;
        do
        {
        mainMenu();
    
        scanf("%d",&opt);
        l=getchar();
         if (opt==1){
             printf("enter board size [1..9] (no input control is needed):\n");
             scanf("%d",&size);
         }
    
         if (opt==2){
             if (size==-1){
                printf("you must choose board size first!\n");
             }
             else{
        printf("enter string\n");
     for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
        {
            input2[i] = ch;
        }
    
        input2[i] = '\0';
        leng=i;
        printf("%s\n", input2);
    k=0;
    for(index=0;index<i;index++){
      if(input2[index]!=' '){
          input[k]=input2[index];
          k++;
      }
     }
    
     if (is_valid(input,leng))
        {
            printf("%s is valid.\n", input);
        }
        else
        {
            printf("%s is invalid.\n", input);
        }
    
    
    
    
    //////////////////////////////////////////////input check
    
    leng2=leng/5;
    
      k=0;
      for(index=0;index<i;index++){
      if((input2[index]!='(')&&(input2[index]!=',')&&(input2[index]!=')')){
          ch_cords[k]=input2[index];
          k++;
      }
     }
    
                j=0;
        for(index=0;index<leng2;index++){
            for(kndex=0;kndex<2;kndex++){
    
    
                   tr=ch_cords[j]-'0';
                 cords[index][kndex]=tr;
                 j++;
            }
        }
    
    
             }
         }//end if opt 2
         if (opt==3){
         }
         if (opt==4){
         }
         if (opt==5){
         }
         if (opt==0){
         }
        }while(opt!=0);
    }
    
    int playGame(char board[N][N],int size){
    
    }
    
    void mainMenu() {
    
    
     printf("--------------------------\n");
    printf("welcome to the game\n");
    printf("1. choose board size\n");
    printf("2. place mines\n");
    printf("3. remove mines\n");
    printf("4. show mines\n");
    printf("5. start the game\n");
    printf("0. exit\n");
    printf("please enter your choice (input control is needed): \n");
    
    }
    
    
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
         printf("\n");
    
     }
      for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
    }
    
    int removeMine(char board[N][N],int size,int x,int y)
    {
        if (board[y-1][x-1]=='*'){
           board[y-1][x-1]=' ';
           printf("mine removed");
           return 1;
        }
        else
        {
        printf("wrong input or no mine");
        return 0;
        }
    
    
    }
    
    
    int unCover(char board[N][N],int size,int x,int y){
     int count=0;
      if (board[y-1][x-1]=='*'){
       printf("you lost\n");
       board[y-1][x-1]='X';
       printBoard(board, size);
       return 0;
      }
     else
     {
    
    
          if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
           count++;
          }
    
            if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
           count++;
          }
            if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
           count++;
          }
    
    
          if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
           count++;
          }
    
            if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
           count++;
          }
            if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
           count++;
          }
    
           board[y-1][x-1]='0' +count;
           printBoard(board, size);
       return 1;
    
     }
    }
    
    
    int is_valid(const char input[],int leng)
    {
    	int flag=3;
    	int i=1;
        /* Match "(<number>,<number>)" */
      int index;
         for(index=0;index<leng;index++){
      if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')')){
           if (((input[1])<'0')||((input[1])>'9')){
    		   return 0;
    	   }
      }
     }
         if (input[0] != '(')
          {
            flag=(int)NULL;
          }
    	   if (((input[1])<'0')||((input[1])>'9')){
    		   flag=(int)NULL;
    	   }
    	   i++;
        while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        } //it wiil get char '2'
    
       if (input[i] == ',') {   //'2' differs ','
      if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
      i++;
             while((input[i]<='9')&&(input[i]>='0')){
    
         input++; //increasing the  address by1
        }
            if (input[i]==')'){
             i++;
            }
            else
              flag=(int)NULL;
        }
       else{
          flag=(int)NULL;
       }
    	//////////////////////////////////////////////////////////////
        if (flag ==(int)NULL)
        {
            return 0;
        }
    
        /* Match zero or more " (<number>,<number>)" */
        while (input[i]!= '\0')
        {
    
    
             if (input[i] != '(')
          {
            flag=(int)NULL;
          }
    	   if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
    	   i++;
        while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        } //it wiil get char '2'
    
       if (input[i] == ',') {   //'2' differs ','
      if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
      i++;
             while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        }
            if (input[i]==')'){
             i++;
            }
            else
              flag=(int)NULL;
        }
       else{
          flag=(int)NULL;
       }
            if (flag ==(int)NULL)
            {
                return 0;
            }
        }
    
    ///////////////////////////////////////
        return 1;
        //////////////////////////////////////
    }
    Last edited by transgalactic2; 01-01-2009 at 04:44 PM.

  2. #47
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is how it looks on the output

    http://img115.imageshack.us/img115/2469/29270183ai6.gif

  3. #48
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you sure you copy the terminating \0 character into your input?

  4. #49
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    yes.
    in the end of the string entering

    Code:
    input2[i] = '\0';
    where did " comes from?

  5. #50
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    yes.
    in the end of the string entering

    Code:
    input2[i] = '\0';
    where did " comes from?
    But when you are removing spaces in the next few lines of code, you are NOT copying the zero, you get rubbish after it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #51
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am copying every cell which differs the space char
    and '\0' supposed to be copied because it differs space.

    am i correct?

  7. #52
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by transgalactic2 View Post
    i am copying every cell which differs the space char
    and '\0' supposed to be copied because it differs space.

    am i correct?
    No. You are copying every character except (1) characters that are ' ' and (2) the last character (because your for loop ends one too soon).

  8. #53
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    i am copying every cell which differs the space char
    and '\0' supposed to be copied because it differs space.

    am i correct?
    But you stop when index = i, which is just before you copy the terminating zero.

    And by the way, should you not use "k" to set the leng value?

    Of course, the other option, which would make all things much simpler would be to allow spaces inside yout is_valid() function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #54
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks it works

  10. #55
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    why it didnt do this problem when i built it separately.
    it was with the old index<i
    and it didnt add " to the end.
    but when i added it to the main program.
    its started to add "
    ??

    here when i built it alone??
    Code:
    #include <stdio.h>
    
    
    int is_valid(const char input[],int leng);
    
    
    int main()
    {
           int index,kndex;
        char input[40];
        char ch_cords[40];
        char input2[40];
        int tr;
        int cords[8][2];
        int tndex;
    
        int i,k,j;
    
        int leng,leng2;
    
    int ch;
    
    
     printf("enter string\n");
     for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
        {
            input2[i] = ch;
        }
    
        input2[i] = '\0';
    leng=i;
    k=0;
    for(index=0;index<i;index++){
      if(input2[index]!=' '){
          input[k]=input2[index];
          k++;
      }
     }
    leng2=leng/5;
    
      k=0;
      for(index=0;index<i;index++){
      if((input2[index]!='(')&&(input2[index]!=',')&&(input2[index]!=')')){
          ch_cords[k]=input2[index];
          k++;
      }
     }
    
                j=0;
        for(index=0;index<leng2;index++){
            for(kndex=0;kndex<2;kndex++){
    
    
                   tr=ch_cords[j]-'0';
                 cords[index][kndex]=tr;
                 j++;
            }
        }
    
        if (is_valid(input,leng))
        {
            printf("%s is valid.\n", input);
        }
        else
        {
            printf("%s is invalid.\n", input);
        }
    
    
    
    
        return 0;
    }
    
    
    int is_valid(const char input[],int leng)
    {
    	int flag=3;
    	int i=1;
        /* Match "(<number>,<number>)" */
      int index;
         for(index=0;index<leng;index++){
      if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')')){
           if (((input[1])<'0')||((input[1])>'9')){
    		   return 0;
    	   }
      }
     }
         if (input[0] != '(')
          {
            flag=(int)NULL;
          }
    	   if (((input[1])<'0')||((input[1])>'9')){
    		   flag=(int)NULL;
    	   }
    	   i++;
        while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        } //it wiil get char '2'
    
       if (input[i] == ',') {   //'2' differs ','
      if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
      i++;
             while((input[i]<='9')&&(input[i]>='0')){
    
         input++; //increasing the  address by1
        }
            if (input[i]==')'){
             i++;
            }
            else
              flag=(int)NULL;
        }
       else{
          flag=(int)NULL;
       }
    	//////////////////////////////////////////////////////////////
        if (flag ==(int)NULL)
        {
            return 0;
        }
    
        /* Match zero or more " (<number>,<number>)" */
        while (input[i]!= '\0')
        {
    
    
             if (input[i] != '(')
          {
            flag=(int)NULL;
          }
    	   if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
    	   i++;
        while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        } //it wiil get char '2'
    
       if (input[i] == ',') {   //'2' differs ','
      if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
      i++;
             while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        }
            if (input[i]==')'){
             i++;
            }
            else
              flag=(int)NULL;
        }
       else{
          flag=(int)NULL;
       }
            if (flag ==(int)NULL)
            {
                return 0;
            }
        }
    
        return 1;
    }

  11. #56
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Considering that what happens to be at the end of the string is completely dependant on where in memory that particular variable ends up, and what the content of that memory happens to be. Since this will depend on what your code actually looks like, what functions you call before it, etc, etc.

    The " is what happens to be in memory at that time - now you've changed where your string is located by changing the call-sequence to the is_valid, which also changes what is in the memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  3. array length
    By Wick in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 04:53 PM
  4. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM