0

I have this function from my final project (a game) and it's supposed to call another function if the condition is true, sometimes my program will call capturePlayer() and some times it won't. I don't know why does this happens and I know the condition it's true because of the printf's I put in there. BTW I'm using graphics.h and pages for the game and when the program don't call the function the it won't even change the page and tell you that you won (which happens even before the condition). Thanks for your help.

void youWin(int score)
{
char name[15], Score[5];

int ret;

points records[RP];

setactivepage(0);

setcolor(GREEN);
settextstyle(GOTHIC_FONT, HORIZ_DIR, 10);
setbkcolor(BLACK);
cleardevice();
outtextxy(640-textwidth("YOU WIN!")/2, 360-textheight("YOU WIN!")/2, "YOU WIN!");
delay(1000);

sprintf(Score, "%d", score);

readRecords(records);

ret = strcmp(Score, records[4].score);

if(ret > 0)
{
    strcpy(records[4].score, Score);
    printf("About to capture.");
    cleardevice();
    setactivepage(0);
    capturePlayer(name, Score, records);
}
}
  • Definitely `capturePlayer()` is getting called when `ret>0`. How did you verify that it is not getting called? – MayurK Dec 03 '16 at 06:23
  • 1
    What does your debugger tell you when you put breakpoints and check the conditions? – Sami Kuhmonen Dec 03 '16 at 06:25
  • @MayurK because at the beginning of `capturePlayer()` I've put a `printf()` and it doesn't print nothing, in console the last thing it shows is `About to capture`. – Hernán Casillas Dec 03 '16 at 06:29
  • 1
    @HernánCasillas: Thread might have hung in `cleardevice();` OR `setactivepage(0);`. So Put prints before and after these two functions and make sure they are fine. – MayurK Dec 03 '16 at 06:32
  • Try to put fflush(stdin); before problematic bit that wont trigger. – someRandomSerbianGuy Dec 03 '16 at 06:36
  • 1
    @MayurK I noticed that after deleting the `cleardevice();` it does call the `capturePlayer()` but now the problem is that the function `youWin()` didn't show anything on screen, like it didn't clear the screen and showed the `outtextxy` thing. I'm frustrated :-( – Hernán Casillas Dec 03 '16 at 06:38
  • @Slay29 Actually with that advice that you gave me, `youWin()` does a better job than before, but it keeps not doing what it's supposed to do with the graphics thing and not triggering `capturePlayer()` – Hernán Casillas Dec 03 '16 at 06:44
  • Try adding fflush(stdin); before every function call and see what difference it makes. Also, 'capturePlayer()' not triggering may be result of your condition. Put a debugging point at your condition before calling function and see if it is passing through true part. – someRandomSerbianGuy Dec 03 '16 at 06:51
  • @HernánCasillas: So the issue is in `clearDevice()`? Not calling `clearDevice()` might not be the solution. Have you written code for that function. If yes, you need to show it for us to help you debugging. – MayurK Dec 03 '16 at 06:51
  • 1
    @MayurK clearDevice is written in `graphics.h` library and what it does is that it clears the screen and puts the background color, I use it to delete everything from the game and after that tell the player that he won and then if the condition is true (which it is) it'll ask the player's name, I did `//capturePlayer()` and now `youWin()` works perfectly and when I remove the slashes it just doesn't work again (sometimes) like I said before. – Hernán Casillas Dec 03 '16 at 07:29
  • 1
    @Slay29 Many consider `fflush(stdin);` very unportable code See http://stackoverflow.com/questions/2979209/using-fflushstdin – chux - Reinstate Monica Dec 03 '16 at 08:01
  • 3
    If `score > 9999 || score < -999`, then `Score[5]` is to small. – chux - Reinstate Monica Dec 03 '16 at 08:04
  • 1
    Also name is not initialised. Make 'char name[15] = "", Score[5] = ""; – MayurK Dec 03 '16 at 08:12
  • @HernánCasillas: I am not very clear yet. Make this change. `printf("About to capture.\n"); cleardevice(); printf("After clearDevioce\n"); setactivepage(0); printf("After setactivepage [%s] [%s]\n", name, Score); capturePlayer(name, Score, records); printf("After capturePlayer\n");` DO NOT COMMENT ANY CODE. Run this and tell us which prints you get. – MayurK Dec 03 '16 at 08:20
  • for ease of readability and understanding: 1) consistently indent the code. Indent after every opening brace '{'. unindent before every closing brace '};'. 2) separate code blocks (for, if, else, while, do...while, switch, case, default) via a single blank line – user3629249 Dec 03 '16 at 09:53
  • Note: the graphics.h and associated library is very obsolete. Unless your coding under `Turbo C` or similar, I would strongly suggest using a modern graphic library. – user3629249 Dec 03 '16 at 09:55
  • the posted code contains several 'magic numbers. 'magic' numbers are numbers with no basis. I.E. 360, 640, 1000. Strongly suggest using an `enum` statement or `#define` statements to give those 'magic' numbers meaningful names then using those meaningful names throughout the code. – user3629249 Dec 03 '16 at 09:58
  • 1
    is your code calling `rectangle()` or `setviewport()` to establish an area for the call to `cleardevice()` to operate upon? – user3629249 Dec 03 '16 at 10:03
  • 1
    @MayurK the output was this: About to capture. After clearDevioce After setactivepage[][3000] – Hernán Casillas Dec 04 '16 at 16:11
  • 1
    @HernánCasillas Is it ok to pass NULL name? – MayurK Dec 04 '16 at 17:53

0 Answers0