Thread: program compiles but dont work??

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    program compiles but dont work??

    i need some hints on whats up with my program. to give you some back ground info, its for my minefield game. specifically this is the part of the code that keeps track of the game stats. the three things to be kept up to date are the score, number of flags and number of mines.

    i wrote a simple driver that test the functions responsible for this task. and included only one of the updating function because all three are exactly the same.

    Code:
    #include "display.h"
    #include "stats.h"
    #include <stdio.h>
    
    int main()
    {
            int up_mines = 0;
            int up_flags = 0;
            int up_score = 0;
            char x;
    
            stats(up_mines, up_flags, up_score); 
            printf("Please enter number:");
            
            while((x = getchar()) != EOF)
            {
                    update_mines(x);
                    update_flags(x);
                    update_score(x);
            }
    
    }
    Code:
    void update_mines(int up_mines)
    {
            char i;
            int k = 0;
    
            while((i = getchar(up_mines)) && k < 2)
            {
                    numbers(i);
                    k++;
            }
    }
    Code:
    #include "stats.h"
    #define MAX_STAT_ROW 4
    #define MAX_STAT_COL 3
    
    
    /* tests and chooses the number to be printed in the stats board */
    void numbers(char x)
    {
            char num1[MAX_STAT_ROW][MAX_STAT_COL];
            int i;
            int k;
            
            /* initializes all arrays to blanks */
            for(i = 0; i < MAX_STAT_ROW; i++)
            {       for(k = 0; k < MAX_STAT_COL; k++)
                    {num1[i][k] = ' ';}
            }
    
            switch(x)
            {
                    case '0':
                            num1[0][1] = '-';
                            num1[1][0] = '|';
                            num1[1][2] = '|';
                            num1[2][0] = '|';
                            num1[2][2] = '|';
                            num1[3][1] = '-';
                            for(i = 0; i < MAX_STAT_ROW; i++)
                            {       for(k = 0; k < MAX_STAT_COL; k++)
                             {printf("&#37;c", num1[i][k]);}
                            }
                            break;
                    case '1':
                            num1[1][1] = '|';
                            num1[2][2] = '|';
                            for(i = 0; i < MAX_STAT_ROW; i++) 
                            {       for(k = 0; k < MAX_STAT_COL; k++)
                             {printf("%c", num1[i][k]);}
                            }
                            break;
                    case '2':
                            num1[0][1] = '-';
                            num1[1][2] = '|';
                            num1[1][1] = '_';
                            num1[2][0] = '|';
                            num1[3][1] = '-';
                            for(i = 0; i < MAX_STAT_ROW; i++)  
                            {       for(k = 0; k < MAX_STAT_COL; k++)
                             {printf("%c", num1[i][k]);}
                            }
                            break;
                    case '3':
                            num1[0][1] = '-';
                            num1[1][2] = '|';
                            num1[1][1] = '_';
                            num1[2][2] = '|';
                            num1[3][1] = '-';
                            for(i = 0; i < MAX_STAT_ROW; i++)  
                            {       for(k = 0; k < MAX_STAT_COL; k++)
                             {printf("%c", num1[i][k]);}
                            }
                            break;
                    case '4':
                            num1[0][0] = '|';
                            num1[1][0] = '|';
                            num1[1][1] = '_';
                            num1[1][2] = '|';
                            num1[0][2] = '|';
                            num1[2][2] = '|';
                            num1[3][2] = '|';
                            for(i = 0; i < MAX_STAT_ROW; i++)  
                            {       for(k = 0; k < MAX_STAT_COL; k++)
                             {printf("%c", num1[i][k]);}
                            }
                            break;
                    case '5':
                            num1[0][1] = '-';
                            num1[1][0] = '|';
                            num1[1][1] = '_';
                            num1[2][2] = '|';
                            num1[3][1] = '-';
                            for(i = 0; i < MAX_STAT_ROW; i++)  
                            {       for(k = 0; k < MAX_STAT_COL; k++)
                             {printf("%c", num1[i][k]);}
                            }
                            break;
                    case '6':
                            num1[0][1] = '-';
                            num1[1][0] = '|';
                            num1[2][0] = '|';
                            num1[3][1] = '-';
                            num1[2][2] = '|';
                            num1[1][1] = '_';
                            for(i = 0; i < MAX_STAT_ROW; i++)  
                            {       for(k = 0; k < MAX_STAT_COL; k++)
                             {printf("%c", num1[i][k]);}
                            }
                            break;
                    case '7':
                            num1[0][1] = '_';
                            num1[1][2] = '|';
                            num1[2][2] = '|';
                            num1[1][0] = '|';
                            for(i = 0; i < MAX_STAT_ROW; i++)  
                            {       for(k = 0; k < MAX_STAT_COL; k++)
                             {printf("%c", num1[i][k]);}
                            }
                            break;
                    case '8':
                            num1[0][1] = '-';
                            num1[1][0] = '|';
                            num1[1][2] = '|';
                            num1[2][0] = '|';
                            num1[2][2] = '|';
                            num1[3][1] = '-';
                            num1[1][1] = '_';
                            for(i = 0; i < MAX_STAT_ROW; i++)  
                            {       for(k = 0; k < MAX_STAT_COL; k++)
                             {printf("%c", num1[i][k]);}
                            }
                            break;
                    case '9':
                            num1[0][1] = '-';
                            num1[1][0] = '|';
                            num1[1][2] = '|';
                            num1[2][0] = '|';
                            num1[2][2] = '|';
                            num1[3][1] = '-';
                            num1[1][1] = '_';
                            for(i = 0; i < MAX_STAT_ROW; i++)  
                            {       for(k = 0; k < MAX_STAT_COL; k++)
                             {printf("%c", num1[i][k]);}
                            }
                            break;
    
            }
    }
    Last edited by youareafever; 12-07-2008 at 08:02 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think if you look at your post from the perspective of someone who is not you, you might be able to see why no one will ever reply to it.

    But I can't be bothered to explain that further, and it doesn't matter if you follow my meaning or not anyway.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    yea looking at it i wouldn't even respond to it. i only the wish the people in my grp would acctually help work on this project with me.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, suddenly I am motivated to explain myself. I might not be able to help you, but I suspect, looking at the nature of your code, that I can. However, you have not actually said "what doesn't work" about it, altho you did say it compiles. But probably I can't compile it, because I don't have the headers. So:

    • Am I supposed to just read thru this and find your problem? Why would I do that when you must have some idea of what it is? Because it's not worth it to you to explain yourself, but it will be worth it to me to figure you out?
    • Is there nothing you can do to narrow the scope of the problem down?


    What you have done is walked into an repair shop and said to the mechanics: "There's my car. Please fix it -- I don't know if it's the brakes, or the windshield wipers, or the transmission -- I just know it has a problem somewhere...I use this car to drive around town, usually, there's a trunk for groceries and a sunroof, actually the car is leased thru my business. But like I said, it has a technical problem, I can't even tell you why I think that, or for what reason I brought this car to you, but please, fix it."
    Last edited by MK27; 12-07-2008 at 10:04 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    Thumbs up

    the program compiles but when i execute nothing happens, so i suspect something is wrong with the first function called in the driver, stats().

    if i do take out stats() from driver.c something does happen. it returns an Segmentation Fault error. i looked it up and i guess its something to do with not enough physical memory being set aside for the program or something?

    maybe its something with the update functions?...idk

    Code:
    /* driver.c to test the functions stats(), update_mines(), update_flags(), update_score() */
    
    #include "display.h"
    #include "stats.h"
    #include <stdio.h>
    
    int main()
    {
            int up_mines = 0;
            int up_flags = 0;
            int up_score = 0;
            int x;
    
            /* values of 0 is passed into this function which should produce arrays all printing 0 */
      /*    stats(up_mines, up_flags, up_score); */ 
       
            /* from here we can further test the update functions by inputting more numbers */
            printf("Please enter number:");
            
            /* executes until EOF is reached */
            while((scanf("&#37;d", x)) != EOF)
            {
                    update_mines(x);
                    update_flags(x);
                    update_score(x);
            }
    
    }
    sorry for not taking the time to ask my question correctly the first time, just really stressed out to get at least half way done with this thing tonight.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your scanf needs more ampersands, and I don't see how your update_whatever functions could actually update anything (they do draw pretty pictures, though).

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I hate to continue seeming mean, but you might have to head straight back to "hello world" without passing go or collecting $200

    Make the first line of main:
    Code:
    puts("main(): here")
    and try executing it. If "here" prints out, keep moving it down one line until it no longer appears when you run it. That will be a clue. I presume the execution is not getting to where you ask for input. Does it segfault, suspend, or just end?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    thanks ill try that out, the updates are passed in int values through out the game. the values of which never goes above 2 digits (pretty unlikely in the minefield game to score more then 99 i assume) and every time that happens numbers() reprints the array representing the int, thus im hoping will update the score and watever else i need it to update.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    cant believe i forgot the ampersand in the scanf.

    MK27- it doesn't do anything until i press enter 5 times, then it prints "Please enter number:".

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void update_mines(int up_mines)
    {
            char i;
            int k = 0;
    
            while((i = getchar(up_mines)) && k < 2)
            {
                    numbers(i);
                    k++;
            }
    }
    Huh? You call getchar, with a number - I'm very surprised this even compiles. What is the intention of this code?

    --
    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.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Another strange thing about this function (update_mines) is that it is called from main with a user inputed int, then this number is used as the argument to getchar(), which does not require or even possibly use one. It looks like you have some ideas which have not been implimented yet?

    That's okay, but I'm tempted to say the sunroof is actually in the floor and the trunk is a sealed box. Anyway, the fact that you have to press return 5 times probably has to do with something in "stats.h" (eg, the stats() function) which was not part of any of your posts. So, when you find the keys and get the doors open, write back
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Add a default case to your switch, in it add a

    printf("default case - I didn't plan for this");

    and see if anything happens then.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. 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
  3. Generating Dates! Compiles but does not work...
    By ottomated in forum C Programming
    Replies: 11
    Last Post: 04-22-2008, 03:58 AM
  4. program wont work, why?
    By chasingxsuns in forum C Programming
    Replies: 11
    Last Post: 02-02-2006, 04:38 PM