Program from a book not working?

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Monado_III, Aug 14, 2015.

  1. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    So I'm gradually learning C from this: http://www.amazon.com/Beginning-From-Novice-Professional/dp/1590597354 but there's an example that fails to work (on Linux) even when I straight copy it from the book. The problem occurs as soon as the random input is shown, so I as soon as I press enter nothing appear and thus it is an impossible game as it never showed me the string of digits I'm supposed to repeat. I'd try and figure it out but for all I know, what this book says is outdated.


    Warning: Spoilers inside!
     
    Last edited by Monado_III, Aug 14, 2015


  2. zoogie

    zoogie simple pimp tool

    Member
    6,146
    7,721
    Nov 30, 2014
    United States
    works fine for me

    gcc -o stuff.exe stuff.c -std=c99
    was how i compiled it
     
  3. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    are you on linux? If so how are you running it? Terminal won't display the digits (compiled the same way) and skips the 'press y to play again part' altogether.
     
  4. zoogie

    zoogie simple pimp tool

    Member
    6,146
    7,721
    Nov 30, 2014
    United States
    nope windows, hence the .exe output.

    — Posts automatically merged - Please don't double post! —

    rename the attached file and try it.
    i have a theory why it didn't work.
     

    Attached Files:

  5. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    nope, same error/bug, what did you change as I see it is smaller?
     
  6. zoogie

    zoogie simple pimp tool

    Member
    6,146
    7,721
    Nov 30, 2014
    United States
    converted to unix line endings. guess that wasn't the issue. :/
     
  7. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    what? pls explain, How did that save like 100 bytes? From what I can tell based off of google results that would've only affected a few bytes.
     
  8. zoogie

    zoogie simple pimp tool

    Member
    6,146
    7,721
    Nov 30, 2014
    United States
    There's about 100 lines of 2 chars in the win version, so you go to one char per line ending in a unix text file and save 100 bytes. Sounds right 2 me. :P

    Anyway. Put a getch(); after the
    printf("%s\n", correct? "Correct!" : "Wrong!");

    it's hacky but may fix the issue.
     
    Monado_III likes this.
  9. cdoty

    cdoty GBAtemp Fan

    Member
    327
    96
    Sep 14, 2009
    United States
    Replace this:

    Code:
    /* Now overwrite the digit sequence */
    printf("\r"); /* go to beginning of the line */
    for(int i = 1; i <= sequence_length; i++)
    printf(" "); /* Output two spaces */
    With this:
    Code:
    /* Now overwrite the digit sequence */
    #if 0
    printf("\r"); /* go to beginning of the line */
    for(int i = 1; i <= sequence_length; i++)
    printf(" "); /* Output two spaces */
    #endif
    And see if it shows the result.

    It's designed to hide the sequence after 1 second.

    The correct way to write this for loop:
    Code:
    for( ;clock() - now < CLOCKS_PER_SEC; );
    is:

    Code:
    for ( ;clock() - now < CLOCKS_PER_SEC; )
    {
    }
    Many compilers will generate a warning on the original version, because it it a common programming error to put a semicolon at the end of for or while loops.

    This should probably be a while loop also.
    Code:
    while (clock() - now < CLOCKS_PER_SEC)
    {
    }
     
    Monado_III likes this.
  10. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    What do the hashtags do for #if and #endif
     
  11. cdoty

    cdoty GBAtemp Fan

    Member
    327
    96
    Sep 14, 2009
    United States
    They are compiler directives.

    In this case, it removes the code between #if 0 and #endif from the program. It's a way to conditionally compile sections of code.

    You'll usually see it used in something like this

    #if defined Win32
    ... code for Windows
    #elif defined OSX
    ... code for Macs
    #elif defined LINUX
    ... code for Linux
    #endif

    or:
    #if defined DEBUG
    .,. code only included in Debug builds
    #endif
     
    Monado_III likes this.
  12. Cyan

    Cyan GBATemp's lurking knight

    Global Moderator
    17,632
    8,211
    Oct 27, 2002
    France
    Engine room, learning
    unrelated to the issue, I have a question:
    Code:
    sequence_length += counter++%3 == 0;
    Shouldn't the right side comparison returns TRUE instead of 1 ?
    sequence_length += true; is strange.

    1 is true, but true is not 1.
    By default a comparison returns 1 instead of true?
    that's why sometime there are dual reverse to get true instead of 1?
    !!( counter++%3 == 0) always returns true or false

    i would have write
    Code:
    if(counter++%3 == 0)
        sequence_length++;
    but if it returns 1 then I suppose inline is better, it uses less cycles.
     
  13. tj_cool

    tj_cool Site dev

    Supervisor
    9,987
    -1
    Jan 7, 2009
    Belgium
    This planet
    Booleans don't really exist in C. "FALSE" is represented by the integer 0 and "TRUE" by any other integer (standardized as 1).

    The C open standard actually mentions it:
     
    Cyan likes this.
  14. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    It displays the digits now (and it looks like the rest of the program works), but then how would I go about getting the digits to disappear after 1 second?
     
  15. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    Anyone? Since it seems like the way it made the digits disappear after a second was a bad way to do it, how would I fix it?
     
  16. cdoty

    cdoty GBAtemp Fan

    Member
    327
    96
    Sep 14, 2009
    United States
    I am surprised the original code doesn't work. You may try replacing it with:

    for ( ;clock() - now < CLOCKS_PER_SEC * 3; )
    {
    }

    to give it a three second delay. and see if that works. My guess is clock() may not work correctly on new systems.
     
    Last edited by cdoty, Aug 15, 2015
  17. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    Tried it and it still didn't work, this is what the program should look like right?
     

    Attached Files:

  18. zoogie

    zoogie simple pimp tool

    Member
    6,146
    7,721
    Nov 30, 2014
    United States
    Honestly, you should move on with your book.
    A single demo program not working is insignificant compared with the rest of your learning.
    Allocate your time wisely.

    Besides, once you have a grasp of the language, you will be better equiped to debug issues like this rather than get others to figure it out.
     
  19. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    I would, but that's the end of the chapter and sone of the exercises requires me to modify the program and do some other stuff with time.
    Although I might do that if no one comes up with solution by the end of of today.
     
  20. sarkwalvein

    sarkwalvein Professional asshole at GBATemp

    Member
    GBAtemp Patron
    sarkwalvein is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    4,664
    4,646
    Jun 29, 2007
    Germany
    Niedersachsen
    Don't be dirty!
    Flush the output!

    Warning: Spoilers inside!