Thread: I've Successfully Created a Program the Crashes! (help)

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Calgary, Alberta
    Posts
    9

    I've Successfully Created a Program the Crashes! (help)

    Hey folks, back again in search of more help.

    I've been programming a relatively simple game that builds a 6 X 6 grid, then takes six numbers submitted by the player and eliminates the row and column of numbers that the number is in - so that the player can't choose any of those numbers or the one they selected again.

    Now on to the problem; in the function that eliminates the number, row and column I have a little bit of programming that checks to make sure the number isn't under 0 or above 36 and that it isn't one of the numbers already eliminated. When the program loads up; it accepts any number that's been eliminated which is because I told the program to check that it wasn't a 0, which is what the eliminated numbers are turned into...but that isn't what the player is actually submitting so of course the number is accepted even if the number's already been eliminated - I'm not sure how to code it to keep track of the numbers that either are or are not valid. Any help on that would be appreciated.

    The other problem is if you submit a number over 36 the program will catch that and ask you to submit a new number, but then it crashes with the Microsoft window coming up and asking to submit the hairy details to them, yadda yadda yadda.

    Here's the function that is causing the problem, and I've attached the full program in a txt in case something needs to be changed there.

    Code:
    /* This function eliminates the rows and columns of numbers based on what the player has selected */	
    int elim( int gr[][6], size_t size, int subNum ){
    int row, col, r, c;
    
    /* This is making sure that the number submitted is a valid number */
    while(subNum <= 0 || subNum > 36){
    	printf( "That is not a valid number, please choose a number that is on the grid (other than zero)\n");
    	scanf( "&#37;2d", subNum );
    }
    
    for(r = 0; r < GRID_ROWS; r++)
    {
    
        for(c = 0; c < GRID_COLUMNS; c++)
        {
            
            if( gr[r][c] == subNum )
       	    {
            /* to adjust an entire row in a 2D array */
    
    			row = r;
    
    			for(col = 0; col < GRID_COLUMNS; col++)
    	   			gr[row][col] = 0;
    
    			col = c;
    
    			for(row = 0; row < GRID_ROWS; row++)
         			gr[row][col] = 0; 
        	}
    
        }
    }
    	
    						
    return subNum;
    }
    Last edited by LycanGalen; 03-07-2008 at 06:27 AM.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Location
    Calgary, Alberta
    Posts
    9
    Oops...Thanks for any help too.

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    /* This is making sure that the number submitted is a valid number */
    while(subNum <= 0 || subNum > 36){
    	printf( "That is not a valid number, please choose a number that is on the grid (other than zero)\n");
    	scanf( "&#37;2d", subNum );
    }
    You forgot to pass the address to the variable subNum to scanf... try &subNum.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf( "&#37;2d", subNum );
    scanf needs pointer to int
    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

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you compile with warnings you might get the following message:
    D:\a.c: In function `elim':

    D:\a.c:171: warning: format argument is not a pointer (arg 2)
    See something missing.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by anon View Post
    If you compile with warnings you might get the following message:


    See something missing.
    Depends on the compiler
    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

  7. #7
    Registered User
    Join Date
    Jan 2008
    Location
    Calgary, Alberta
    Posts
    9
    Wow. Well that wins for the "I've-been-staring-at-my-coding-too-long" award.

    Thanks a lot guys.

    And no, my compiler didn't give me any info on it. Starting to think I need to find a new program.

    any suggestions on examples of coding to check out for how to make sure they haven't submitted a number that's already been eliminated? Something specific to google even? I kinda feel like I hit a wall of stupidity on this.

    I know I could find a way to manually build an array of "banned" numbers by transferring the value of each eliminated array unit to another array, but I'm sure there's a cleaner way to do it.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    gcc is very good at detecting scanf/printf problems along these lines. if you are using gcc already, then you need to add "-Wall" to make it show you warnings.

    --
    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. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You might also fix the higher-level logic errors with this program. For example, elim returns the input that finally passes validation. However, you don't use the returned value, therefore accepting the original input anyway.

    I'm not sure, whether you want that or not, but it is a bit strange that you can "win" the game by entering values that aren't available (are 0 in the array - it would be quite easy to determine the array indices with the help of division and mod).

    In the main function, you probably should use an array instead of number1...number6 and a loop to get input.

    Otherwise, it's an interesting piece of work.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Jan 2008
    Location
    Calgary, Alberta
    Posts
    9
    Thanks for the feedback. I'll check out gcc when I get a chance.

    Also, anon you mentioned that the elim returns the submitted value which is not used - but I do use that; since that condition is trying to ensure that the person is submitting a valid number, then it's replacing the invalid number originally submitted in main with the valid number that finally passes evaluation in elim, so that the player is forced to only give numbers that work. Or at least that's what happens in the working fantasy version of this program that runs in my mind. I'll go back and make sure.

    Speaking of this - No, I don't want the player to be able to submit numbers that aren't available. I'm having trouble figuring out how to keep them from being able to do this. Do you have any examples, or something to look at that does something similar to what I'm trying to do? You mentioned array indices?

    As for the array and a loop; you're absolutely right. That's all blunt-force coding from when I was still unsure of arrays (heck, I still am to a degree) and hadn't gotten the hang of loops. I will definitely go back and change that. Thanks.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by LycanGalen View Post
    Thanks for the feedback. I'll check out gcc when I get a chance.

    Also, anon you mentioned that the elim returns the submitted value which is not used - but I do use that; since that condition is trying to ensure that the person is submitting a valid number, then it's replacing the invalid number originally submitted in main with the valid number that finally passes evaluation in elim, so that the player is forced to only give numbers that work. Or at least that's what happens in the working fantasy version of this program that runs in my mind. I'll go back and make sure.
    Well, you are using elim like that:
    Code:
    currentNumber = number1;
    
    elim( grid, 6, currentNumber);
    
    /* This is to make sure the correct number is stored in memory for later (ref elim loop) */
    	number1 = currentNumber;
    However, when you pass a variable to a function (by value) it will not be changed in the calling code. This should look something like this:

    Code:
    scanf( "%2d", &number1 );
    number1 = elim( grid, 6, number1);
    Now you pass the initial input for validation and assign the value that elim finally accepted back to number1.

    Actually it would probably be better to split elim into two functions: one that takes input and ensures it is valid, and another one that sets rows and columns to zero given a position.


    Speaking of this - No, I don't want the player to be able to submit numbers that aren't available. I'm having trouble figuring out how to keep them from being able to do this. Do you have any examples, or something to look at that does something similar to what I'm trying to do? You mentioned array indices?
    You should be able to calculate the row and column index from 1-36 easily.

    Firstly, this would work for zero-based indices, so you'd substract one from user input.
    Then dividing this by the length of a row would give you the row index, and doing a modulus with the row length should give you the column index.

    For validation you'd first make sure that input is in the correct range.
    If so, check if the value at that index is not 0. If so, prompt for new input and repeat from the first validation routine.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What you are trying to accomplish sounds liken to that of a "you sank my battleship!" in that the big difference is that any chose coordinate can not be used again by any other player. As mentioned to some degree, you will need an array to keep track of all coordinates submitted by players. I would use a double array for this and for each entry compare the coordinates to what is stored in the array[row][col]. For your size of the array there would not be any performance hit by this.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Location
    Calgary, Alberta
    Posts
    9
    I'm going to reiterate most of what you mentioned to make sure I understand what you've told me - I actually know I don't understand all of it, but there you have it.

    to transfer data back and forth between functions - specifically when returning a value, I have to use the same name in both functions, otherwise I'm passing something that the receiving function knows nothing about...right?

    Now the indexing of rows and columns... I'm not a math whiz. I'm going to try an example here. let's say the user submits 27. so as anon has suggested, we subtract 1 (26) and now divide it by the length of the row (6), which is 4.33. So how is this an index of the row? I understand the modulus though, that would be one which sure enough is the column that it's in... still not sure about the row though.

    But these indexes could in theory be stored in a 6 x 2 array, one row for storing eliminated rows, and one for storing eliminated columns, right?

    Sorry, I know I'm being pretty thick. I am trying though. Thank you for all the help. It's greatly appreciated.
    Last edited by LycanGalen; 03-09-2008 at 09:10 AM.

  14. #14
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by LycanGalen View Post
    to transfer data back and forth between functions - specifically when returning a value, I have to use the same name in both functions, otherwise I'm passing something that the receiving function knows nothing about...right?
    No, what a function actually returns has no name, and is the responsibility of the caller to store. The parameters of a function may have any name that is not a reserved word, and the scope of the name is local to the function.
    Quote Originally Posted by LycanGalen View Post
    Now the indexing of rows and columns... I'm not a math whiz. I'm going to try an example here. let's say the user submits 27. so as anon has suggested, we subtract 1 (26) and now divide it by the length of the row (6), which is 4.33. So how is this an index of the row? I understand the modulus though, that would be one which sure enough is the column that it's in... still not sure about the row though.

    But these indexes could in theory be stored in a 6 x 2 array, one row for storing eliminated rows, and one for storing eliminated columns, right?

    Sorry, I know I'm being pretty thick. I am trying though. Thank you for all the help. It's greatly appreciated.
    As for your example, if the division is done with integer precision then 26/6 is actually 4, the row, and 26 % 6 is 2, the column. You would not need an array to store the indexes, but if you do code it in such a way it will be a 36 x 2 array. And you would need to subtract 1 from the user input to index it.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Location
    Calgary, Alberta
    Posts
    9
    Okay, I'll go do more reading on functions and callers, apparently I'm missing something.

    As for the math, I "logic-ed" out the indexes, even made a working mini program for it. So I understand that now (brain fart on how integers work in the programming world, and also was confusing remainders with...stupidity on some level. However, I'm lost with xuftugulus' suggestion of a 36 x 2 array. If the only index components being submitted are 0 - 5 for row, and the same for column, the user is only submitting 6 numbers, and I am referencing the indexes submitted in the new array to make sure I don't add the same index twice (remember, once something in a row's been eliminated, that entire row is out of order from then on, same with columns), why do I need 36?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Program crashes and memory leaks
    By ulillillia in forum Tech Board
    Replies: 1
    Last Post: 05-15-2007, 10:54 PM
  3. two strcpy-ies crashes my program
    By Jakob in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2005, 07:17 AM
  4. Program crashes...help Needed!!
    By butterfly in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 11:22 AM
  5. counting program worked, but test file CRASHES IT.. WHY?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 02:29 AM