Thread: Just a little help please... :) ?

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    8

    Unhappy Just a little help please... :) ?

    Hi

    Here is a part of my code where I want to get input from file, now I did it and I'm getting the input from my file but I want to get rid of the user inputing his umm, input....(from keyboard...)

    Thanks a lot !!!!

    Code:
    /*====================================================================*/
    /* Function:           Get_generation_0
     * Purpose:            Read in first generation
     * Input args:         number_of_rows, number_of_cols
     * Input/output args:  world_p
     * Input:              X's and blanks representing first generation
     */
    void Get_generation_0(FILE* pSrc,world_t world, int number_of_rows,
                          int number_of_cols) {
        int i, j;
        char temp;
    
        printf("Enter the first generation\n");
    
        for (i = 0; i < number_of_rows; i++) {
            j = 0;
            fscanf(pSrc,"%c", &temp);
            while ((temp != '\n') && (j < number_of_cols)) {
                if (temp == PRINT_OCCUPIED)
                    world[i][j] = OCCUPIED;
                j++;
                fscanf(pSrc,"%c", &temp);
            } /* while */
    
            /* If the current character in temp isn't a newline, then
             * there's garbage we need to skip over
             */
            if (temp != '\n')
                Skip_rest_of_line(pSrc);
        } /* for */
    
    }  /* Get_generation_0 */

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Your post is not specific enough to be answered. What works, and what doesn't? What happens that you don't want to happen, exactly?

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by deflamol
    Hi

    Here is a part of my code where I want to get input from file, now I did it and I'm getting the input from my file but I want to get rid of the user inputing his umm, input....(from keyboard...)

    Thanks a lot !!!!

    Code:
    /*====================================================================*/
    /* Function:           Get_generation_0
     * Purpose:            Read in first generation
     * Input args:         number_of_rows, number_of_cols
     * Input/output args:  world_p
     * Input:              X's and blanks representing first generation
     */
    void Get_generation_0(FILE* pSrc,world_t world, int number_of_rows,
                          int number_of_cols) {
        int i, j;
        char temp;
    
        printf("Enter the first generation\n");
    
        for (i = 0; i < number_of_rows; i++) {
            j = 0;
            fscanf(pSrc,"%c", &temp);
            while ((temp != '\n') && (j < number_of_cols)) {
                if (temp == PRINT_OCCUPIED)
                    world[i][j] = OCCUPIED;
                j++;
                fscanf(pSrc,"%c", &temp);
            } /* while */
    
            /* If the current character in temp isn't a newline, then
             * there's garbage we need to skip over
             */
            if (temp != '\n')
                Skip_rest_of_line(pSrc);
        } /* for */
    
    }  /* Get_generation_0 */
    I don't understand what you want. The only code in the function you posted that suggests user input is the prompt at the beginning and the removal of garbage. In the first case,
    Code:
    printf("Enter the first generation\n");
    If pSrc will always be a file then there's no need for such unnecessary output and you can remove the line. However, if the function can be called with stdin as the first argument then you should take that into account with something like this.
    Code:
    if (pSrc == stdin) {
      printf("Enter the first generation\n");
    }
    Not to mention that the prompt should be more informative as to what input is expected. In the second case,
    Code:
    if (temp != '\n')
      Skip_rest_of_line(pSrc);
    If pSrc will always be a file then once again there's no need for this as the file should be formatted properly. This conditional should fail every time if pSrc is a file, but if pSrc could be stdin then it couldn't hurt to leave it in the function as a hedge.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    8
    sorry I couldn't explain correctly, so here I go again :
    This part of the program asks me to input the first generation (a matrix), but I want it only to use the file and not ask for input from the keyboard so what should I erase ??

    Now it prompts me to enter the first generation, so I have to enter a matrix OR press the enter key 24 times (max number of columns...) before it starts using the file I specified....

    hope I made it clearer... Thanks !

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    8
    Thank you both for trying to help but can someone please tell me how to get rid of the stdnin scanf ?? (I want the program to read the information from the file and that's it... I don't want it to be asking me to type in the matrix...)

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by deflamol
    Thank you both for trying to help but can someone please tell me how to get rid of the stdnin scanf ?? (I want the program to read the information from the file and that's it... I don't want it to be asking me to type in the matrix...)
    You mean something like fscanf perhaps?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    8
    well I'm sorry to post be reposting but I can't figure it out... when I erase the fscanf function the loops a couple of times then it crashes... logical I suppose since it's not getting any values... so here is my full program, what I want to do is let the program scan the values of the first matrix (generation 0) from the world.txt file.

    thanks a lot and sorrz for reposting....

    Code:
    /*Programming homework : The Game of Life
    				by Rami Moussawi */
    
    #include <stdio.h>     /* for printf, scanf, etc. */
    #include <stdlib.h>    /* for exit                */
    
    #define MAX_ROWS    24
    #define MAX_COLS    24
    #define MAX_GENS 1000000000
    
    #define OCCUPIED        1
    #define PRINT_OCCUPIED  'X'
    #define VACANT       0
    #define PRINT_VACANT ' '
    
    
    typedef int world_t[MAX_ROWS][MAX_COLS];
    
    void Print_instructions(void);
    
    void Initialize_world(world_t world, int *number_of_rows_p,
                          int *number_of_cols_p);
    
    void Get_user_input(world_t world, int* number_of_rows_p,
                        int* number_of_cols_p, int *max_gens_p);
    
    
    int Get_int_in_range(int min, int max);
    
    
    void Get_generation_0(FILE* pSrc,world_t world, int number_of_rows,
                          int number_of_cols);
    
    
    void Find_next_generation(world_t world, int number_of_rows,
                              int number_of_cols);
    
    
    void Print_world(world_t world, int number_of_rows,
                     int number_of_cols);
    
    void Skip_rest_of_line(FILE* pSrc);
    
    
    int Count_neighbors(int number_of_rows, int number_of_cols,
                         world_t world, int row, int col);
    
    
    int  Cell_count(world_t world, int number_of_rows, int number_of_cols,
                    int i, int j);
    
    int Somethings_alive(world_t world, int number_of_rows,
                         int number_of_cols);
    
    
    /*===================================================================*/
    int main(void) {
        world_t world;
        int number_of_rows;
        int number_of_cols;
        int max_gens;
        int curr_gen;
    	FILE* pFile;
    
        Print_instructions();
        Initialize_world(world, &number_of_rows, &number_of_cols);
        printf("Reading First Generation from file ""world.txt""\n");
    
    	pFile=fopen("world.txt","rt");
    	if(!pFile)
    	{
    		printf("Unable to open file world.txt");
    		return;
    	}
    
    
        Get_user_input(world, &number_of_rows, &number_of_cols, &max_gens);
    	fscanf(pFile,"%dX%d,%d\n", &number_of_rows, &number_of_cols, &max_gens);
        Get_generation_0(pFile,world, number_of_rows, number_of_cols);
    
    
        curr_gen = 0;
        while ( (Somethings_alive(world, number_of_rows, number_of_cols))
                && (curr_gen < max_gens) ) {
            Find_next_generation(world, number_of_rows, number_of_cols);
            Print_world(world, number_of_rows, number_of_cols);
            curr_gen++;
    	printf("Press any key to get next generation");
    	getchar();         /*stops the program until user inputs a value*/
        }
    
        printf("Enjoyed life ?\n");
    
        return 0;
    }  /* main */
    
    
    
    void Print_instructions(void) {
        printf("Welcome to the game of Life!\n\n");
        printf("Life is an artificial world on a rectangular grid: each\n");
        printf("square or cell is either alive or vacant.  The \n");
        printf("configuration of living and vacant cells changes \n");
        printf("according to the following rules:\n");
        printf("1.   Any cell with 0 or 1 neighbor will die from\n");
        printf("     loneliness.\n");
        printf("2.   Any living cell with 2 or 3 neighbors will\n");
        printf("     surive into the next generation.\n");
        printf("3.   Any cell with 4 or more neighbors will die from\n");
        printf("     overcrowding.\n");
        printf("4.   Any vacant cell with exactly 3 neighbors will come\n");
        printf("     to life in the next generation.\n");
        printf("Updates happen all at once.  The next generation of\n");
        printf("cells is computed solely on the basis of the current\n");
        printf("generation.  After the next generation is computed, the\n");
        printf("current generation is replaced by the newly computed\n");
        printf("generation.\n\n");
        printf("Note: Make sure that your first generation matrix is\n");
        printf("      in the same directory as this program as :\n");
        printf("      ""world.txt"".\n\n");
    }  /* Print_instructions */
    
    
    void Initialize_world(world_t world, int* number_of_rows_p,
                          int* number_of_cols_p) {
        int row, col;
    
        for (row = 0; row < MAX_ROWS; row++)
            for (col = 0; col < MAX_COLS; col++)
                world[row][col] = VACANT;
    
        *number_of_rows_p = 0;
        *number_of_cols_p = 0;
    
    } /* Initialize_world */
    
    
    void Get_user_input(world_t world, int *number_of_rows_p,
                        int *number_of_cols_p, int *max_gens_p) {
    
    
        *number_of_rows_p = 24;
        *number_of_cols_p = 24;
        printf("Please enter the maximum number of generations\n");
        *max_gens_p = Get_int_in_range(1, MAX_GENS);
    
        /* Skip the rest of the current line before starting to
         * read generation 0 -- there's definitely a newline that hasn't
         * been read.
         */
        Skip_rest_of_line(stdin);
    
        Get_generation_0(stdin,world, *number_of_rows_p, *number_of_cols_p);
    }  /* Get_user_input */
    
    
    void Get_generation_0(FILE* pSrc,world_t world, int number_of_rows,
                          int number_of_cols) {
        int i, j;
        char temp;
    
        printf("Enter the first generation\n");
    
        for (i = 0; i < number_of_rows; i++) {
            j = 0;
            fscanf(pSrc,"%c", &temp);
            while ((temp != '\n') && (j < number_of_cols)) {
                if (temp == PRINT_OCCUPIED)
                    world[i][j] = OCCUPIED;
                j++;
                fscanf(pSrc,"%c", &temp);
            } /* while */
    
            /* If the current character in temp isn't a newline, then
             * there's garbage we need to skip over
             */
            if (temp != '\n')
                Skip_rest_of_line(pSrc);
        } /* for */
    
    }  /* Get_generation_0 */
    
    
    int Get_int_in_range(int min, int max) {
        int temp;
    
        scanf("%d", &temp);
        while ( (temp < min) && (temp > max) ) {
            printf("Please enter a number >= %d and <= %d\n", min, max);
            scanf("%d", &temp);
        }
        return temp;
    }  /* Get_int_in_range */
    
    
    
    void Skip_rest_of_line(FILE* pSrc) {
        char temp;
    
        fscanf(pSrc,"%c", &temp);
        while (temp != '\n')
            scanf("%c", &temp);
    
    }  /* Skip_rest_of_line */
    
    
    
    void Find_next_generation(world_t world, int number_of_rows,
                              int number_of_cols) {
        int row, col;
        world_t temp_world;
        int neighbor_count;
    
        for (row = 0; row < number_of_rows; row++)
            for (col = 0; col < number_of_cols; col++) {
                neighbor_count = Count_neighbors(number_of_rows,
                                           number_of_cols, world, row, col);
    
                switch (neighbor_count) {
                    case 0:
                    case 1:
                        temp_world[row][col] = VACANT;
                        break;
                    case 2:
                        temp_world[row][col] = world[row][col];
                        break;
                    case 3:
                        temp_world[row][col] = OCCUPIED;
                        break;
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                        temp_world[row][col] = VACANT;
                        break;
                    default:
                         printf("Error!  neighbor_count for world");
                         printf("[%d][%d] = %d\n", row, col,
                                 neighbor_count);
                         exit(-1);
                }
            }  /* for col */
    
        for (row = 0; row < number_of_rows; row++)
            for (col = 0; col < number_of_cols; col++)
                world[row][col] = temp_world[row][col];
    }  /* Find_next_generation */
    
    
    
    int Count_neighbors(int number_of_rows, int number_of_cols,
                        world_t world, int row, int col) {
        int i, j;
        int count = 0;
    
        for (i = row-1; i <= row+1; i++)
            for (j = col-1; j <= col+1; j++)
                count = count + Cell_count(world, number_of_rows,
                                     number_of_cols, i, j);
        count = count - world[row][col];
    
        return count;
    }  /* Count_neighbors */
    
    
    int  Cell_count(world_t world, int number_of_rows, int number_of_cols,
                    int i, int j) {
    
        if ((i < 0) || (i >= number_of_rows) ||
            (j < 0) || (j >= number_of_cols))
            return 0;
        else
            return world[i][j];
    }  /* Cell_count */
    
    
    void Print_world(world_t world, int number_of_rows,
                     int number_of_cols) {
        int row, col;
        int j;
    
        /* Print a line */
        for (j = 0; j < MAX_COLS; j++)
            printf("-");
        printf("\n");
    
        for (row = 0; row < number_of_rows; row++) {
            for (col = 0; col < number_of_cols; col++)
                if (world[row][col] == OCCUPIED)
                    printf("%c", PRINT_OCCUPIED);
                else
                    printf("%c", PRINT_VACANT);
            printf("\n");
        }
    }  /* Print_world */
    
    
    
    int Somethings_alive(world_t world, int number_of_rows,
                         int number_of_cols) {
        int i, j;
    
        for (i = 0; i < number_of_rows; i++)
            for (j = 0; j < number_of_cols; j++)
                if (world[i][j] == OCCUPIED) return 1;
        return 0;
    }  /* Somethings_alive */

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, since I don't know what your input file looks like, I'll make it up. Here's are some modifications. They may work. Since I don't know what you're actually trying to read...
    Code:
    void Get_generation_0(FILE* pSrc,world_t world, int number_of_rows,
                          int number_of_cols) {
        int i, j, temp;
    
        printf("Entering the first generation\n");
    
    
        for (i = 0; i < number_of_rows; i++) {
            for( j = 0; j < number_of_cols; j++ ) {
                temp = fgetc( pSrc );
                if( temp == PRINT_OCCUPIED )
                    world[i][j] = OCCUPIED;
                else
                if( temp == '\n' )
                    break; /* stop looping, this line is done */
                else
                if( temp == EOF )
                    return; /* end of file, you need to anticipate this and deal with it some how */
                else
                    { /* not OCCUPIED, so what is it? You tell me. */ }
            }
        }
    }
    It's hard to say exactly what you were doing here, so this is a guess. However, you'll notice that I use fgetc, since you're just reading one character at a time. You use an int to store the value when you read it, because it may read EOF.

    Anyway, even if that doesn't do what you want, consider using fgetc. Shrug.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Intmainer
    Join Date
    May 2004
    Posts
    7
    Yeh, the reason it did not work is because you did not use fscanf....lol, but good code none the less...

Popular pages Recent additions subscribe to a feed