Thread: Help me spot a (probably) simple error!

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68

    Help me spot a (probably) simple error!

    Hi everyone,
    Once again i call upon your collective or individual expertise!

    I'm writing a program (which i have working) and i wanted to put a bit of sanity checking or error checking in to it (using stderr) and its doing something a little funny!

    My code:

    Code:
    #include <stdio.h>
    
    int main()
    {
    int originalarray[9][9]; // 9x9 matrix, note that the elements are numbered from 0 and that (9,9) isn't used.
    int x, y, z; // Coordinate variables
    int error;
    FILE *array_output;
    FILE *formatted_input_file;
    // Build original matrix
    //***********************************************************************************************************************
    if ((formatted_input_file = fopen("formatted_input_file", "r")) == NULL)
               fprintf(stderr, "Cannot open %s\n", "formatted_input_file");
    
    for (x = 0; x <= 8; x++)
    {
    	char buf[10];
    	fscanf(formatted_input_file, "%s", buf);
    	for (y = 0; y <= 8; y++)
    	{
    	originalarray[x][y] = buf[y]-'0';
    	}
    }
    fclose(formatted_input_file);
    //Print out original array to output file and stdout
    //***********************************************************************************************************************
    if ((array_output = fopen("array_output", "w")) == NULL)
               fprintf(stderr, "Cannot open %s\n", "array_output");
    
    for (x = 0; x < 9; x++) //Goes through rows
    {
    	printf("\n");
    	fprintf(array_output, "\n");
    		for (y = 0; y < 9; y++) //Goes through columns
    		{
    		printf("%d ", originalarray[x][y]);
    		fprintf(array_output, "%d ", originalarray[x][y]); // prints each value of the original matrix in turn
    		}
    }
    
    fclose(array_output);
    //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    //Error checking section
    error = 0;
    for (x = 0; x < 9; x++)
    {
    	for (y = 0; y < 9; y++)
    	{
    		for (z = 0; z < 9; z++)
    		{
    			if(y != z)
    			{
    				if(originalarray[x][y] != 0)
    				{
    					if(originalarray[x][y] == originalarray[x][z])
    					{
    					error++;
    					}
    				}
    			}
    		}
    	}
    }
    
    if(error > 0)
    {
    fprintf(stderr, "\nInput Matrix is Wrong!\n");
    }
    
    }
    The output is:
    Code:
    0 2 0 5 0 0 6 4 2
    7 0 0 0 0 0 5 0 0
    0 3 0 2 0 8 0 0 7
    4 0 0 0 0 7 2 9 0
    2 8 0 0 0 0 0 6 3
    0 7 3 9 0 0 0 0 4
    6 0 0 8 0 1 0 7 0
    0 0 5 0 0 0 0 0 6
     
    Input Matrix is Wrong!
    0 9 7 0 0 2 0 8 0
    But it should be (or rather i want it to be):
    Code:
    0 2 0 5 0 0 6 4 2
    7 0 0 0 0 0 5 0 0
    0 3 0 2 0 8 0 0 7
    4 0 0 0 0 7 2 9 0
    2 8 0 0 0 0 0 6 3
    0 7 3 9 0 0 0 0 4
    6 0 0 8 0 1 0 7 0
    0 0 5 0 0 0 0 0 6
    0 9 7 0 0 2 0 8 0 
    Input Matrix is Wrong!
    I think i've probably done something really simple but i really can't spot it!

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fscanf(formatted_input_file, "&#37;s", buf);
    For this to work as you seem to want it to work, there should be no spaces in the line of your input matrix.

    > for (x = 0; x <= 8; x++)
    > for (x = 0; x < 9; x++)
    Adopt a consistent approach.
    The second one is more usual.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Thanks Salem, i fixed the inconsistencies but it still does the same output.

    The input file is ok:

    Code:
    020500642
    700000500
    030208007
    400007290
    280000063
    073900004
    600801070
    005000006
    097002080
    It reads the matrix in ok. Its just when there is an error (in this case deliberate to test it) that it displays the error message in a place i didn't expect

    (i thought it would display the error message after the matrix instead of inside it)

    Any thoughts?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Oh I see, you're using a wacky approach to printing newlines, and the last line you print has NO newline prior to the output of the error message.

    Maybe
    Code:
    for (x = 0; x < 9; x++) //Goes through rows
    {
    		for (y = 0; y < 9; y++) //Goes through columns
    		{
    		printf("&#37;d ", originalarray[x][y]);
    		fprintf(array_output, "%d ", originalarray[x][y]); // prints each value of the original matrix in turn
    		}
    	printf("\n");
    	fprintf(array_output, "\n");
    }
    The reason being the stdout remains unflushed, and the stderr pre-empts that prior to the streams being closed at the end of the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Ah ok!
    Well i'm kind of glad that it wasn't a silly mistake i was making somewhere. I would never have known about that. Thanks for sharing the knowledge!

    So how should i do the newlines. This is the only way i know how as i'm just learning. Is there a way to manually flush stdout?

    It works now if i stick in another printf("\n"); before the error checking section.

    Thanks a lot Salem

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    fflush(stdout);
    But then your error message would be appended to the end of the last line of your array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    ok, nice to know for future but i'll just stick with printing a newline.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM