Thread: Problem with code

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    20

    Problem with code

    Hey guys, i'm trying to filter out the unnecessary characters from a text document that i scan. I want to output the text after filtering.

    Here's the text document:

    33333333333333333333
    33383498498908908098
    32233423433234342342
    10 13 FOX 12311209810281390289

    I'm trying to have it to where it looks like this

    33383498498908908098
    32233423433234342342
    12311209810281390289
    After the filtering, it skips the first line of the previous text document and takes out the the 10 13 Fox bit.


    Here's my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int input(int [][5]);
    
    void output(int [][5], int);
    
    int main(void) 
    {
       int scoreBoard[0][5];
       int countNum;
    
       countNum=input(scoreBoard);
       output(scoreBoard, countNum);
    
       return 0;
    }
    
    
    void output(int scoreBoard[][5], int countNum) 
    {
       int i,j;
    
       for (i=0; i<countNum;i++)
         for (j=0;j<5;j++)
          printf("scoreBoard[&#37;d][%d]=%d\n",i,j,scoreBoard[i][j]);
        
    }
    
    
    int input(int scoreBoard[][5]){
       int i,j;
       char character;
    
       for (i=0;scanf("%c",&character)>0 ;i++) {
          scoreBoard[i][0]= atoi (&character);
          for (j=1;  scanf("%c",&character), j<5; j++)
             scoreBoard[i][j]= atoi (&character);
       }
    
       return i;
    }
    Wat am i doing wrong? I can't seem to be able to output the new text after filtering.

    Help would be mad appreciated!
    Last edited by OxKing033; 02-15-2008 at 01:28 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    at least 2 things:

    int scoreBoard[0][5];
    is array of size 0 - it cannot contain any data

    atoi (&character); - is not working this way, atoi converts string, not a single character

    to convert one character like
    char c = '6';

    to int use
    int res = c - '0';
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    thanks man, i'll try that and some.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    hey guys, i got to show up. But the problem i'm having now is the way the text shows.

    I'm trying to have it where the new text document thats made is neater like this:



    4444444444
    333333*333
    222*222222
    1111*11111
    but it looks like this instead

    *******4444444444[][[][][][][][]********333333*333[][][][][[][]
    **********222*222222*******[][][][][][][][]
    ****1111*11111[][][][][][[]
    I'm trying to figure out why its not formatted correctly and why there are extra astericks.

    I've changed my code. my getInput method is what was changed all together.

    Code:
    
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    int Input(int row, int col);
    void output(int answer[][16],int count);
    
    
    int main(void) 
    {
       int answer[100][16];
       int count, row, col;
    
       count=Input(row, col);
       output(answer,count);
    
       return 0;
    }
    
    int Input(int row, int col)
    {
    
    char c;
    row = 0;
    col = 0;
        
         
      while (scanf("&#37;c", &c) != EOF)
      {
      
       if(c == '\n')
       {
        row++;
    
        col = 0;
        printf("\n");
       }
       
       
       if(row >=1)
       {
        if(col >=30 && col <=48)
        {
         
         if(c != ' ')
         {
          printf("%c",c);
         }
    
         }
         else if(col >= 49)
         {
          printf("\n");
         }
         else 
         { 
          printf("*");
         }
        }
        col++;
       }
        
         return 0;
      }  
    
    
    
    
    
    
    
    void output(int answer[][16],int count) 
    {
        int i,j;
    
        for (i=0;i<count;i++) 
        {
           for (j=0;j<16;j++)
    	 printf("%d",answer[i][j]);
           printf("\n\n");
        }
    }
    Any more help would still be appreciative!

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. Your input does not uses row,col parameters - so they should be made local vars for this function
    2. Your input does not fills the array - so it stays not initialized
    Instead it outputs something that is against its name
    3.Do not use magic numbers like 30 or 49 use corresponding constants like '9' to make your code readable
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    Hey man, i added to an array and set is as the parameters, and had the row-col localized in the input function. The program worked just about fine. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM