0

EDIT 1:

I would like to have a program that checks for enter key pressed and then skip some code after it (kind of like some games that have the skippable screens when you launch them). But I don't want it to wait for the input.

if(getchar()=='\n') 
{
goto skip;
}
ClearScreen();
printf("%s                                         Welcome to Guy's game!\n\n");
Sleep(500);
ClearScreen();
printf("%s                                         Welcome to Guy's game!\n\n");
Sleep(500);
ClearScreen();
printf("%s                                         Welcome to Guy's game!\n\n");
Sleep(500);
ClearScreen();
printf("%s                                         Welcome to Guy's game!\n\n");
Sleep(500);
ClearScreen();
//this is the where it should skip
skip:
printf("%s                                         Welcome to Guy's game!\n\n");
printf("Please enter your name: ");
gets(name);
Sleep(250);

I want it to check for enter while it prints "Welcome to Guy's game!" so that no matter when I press enter (as long as it's printing "Welcome to Guy's game!") I can skip to the last part of the code. I can't figure out how to get this to work.

UPDATE:

I have one more question that I forgot to ask.. How can I fix "warning: implicit declaration of function 'Sleep', when I use:

ClearScreen();
printf("%s                                         Welcome to Guy's game!\n\n");
Sleep(500);

inside a for loop.

Guy
  • 41
  • 10
  • google for `while loop waits for enter` maybe point you for your target solution. – meAbab Aug 22 '16 at 21:43
  • If you're on Windows, use `kbhit` after every `printf` to see if the user pushed a key. – user3386109 Aug 22 '16 at 21:52
  • @user3386109 isn't that in a package though? – Guy Aug 22 '16 at 21:57
  • This will help you: http://stackoverflow.com/questions/6731317/c-exit-from-infinite-loop-on-keypress and http://cc.byexamples.com/2007/04/08/non-blocking-user-input-in-loop-without-ncurses/ – Abhijay Ghildyal Aug 22 '16 at 22:02
  • @TheVB10Guy It would help if you told us what operating system and what compiler you're using. – user3386109 Aug 22 '16 at 22:06
  • 1
    @user3386109 I am using Windows 10 and my compiler is gcc – Guy Aug 22 '16 at 23:40
  • If you answered your own question, post it in the big Answer box below. Don't change your question as it no longer makes sense. You can accept your own answer after a day or so. – Dour High Arch Aug 24 '16 at 01:08
  • @DourHighArch I know I wanted to do that but it says "We are no longer accepting answers from this account", or something. Also can you check http://stackoverflow.com/questions/21198953/pemdas-visual-basic out, because I asked a question poorly and many people downvoted it, and some closed it. Also I never answered a single question on stackoverflow, so I can't edit one to make it more clear. Thanks! – Guy Aug 24 '16 at 01:15

3 Answers3

2

You need to alter your terminal to not do canonical key processing.

All terminals hold information for just a little while to allow for things like the user hitting backspace (and the program not getting the erased character). This buffering of keystrokes is not sent to the program until a special "buffer flushing" key is pressed, like Enter or Return.

In a game this is undesirable, you want the keys as soon as you can get them. This means your program will requires the termios data structure, and then turn off (communicating to the terminal) the Canonical key processing. Note that this means you will have to handle backspace erasing in your program (should you desire it in other portions of your program, like entering in names to go with the high score).

// define a terminal configuration data structure
struct termios term;

// copy the stdin terminal configuration into term
tcgetattr( fileno(stdin), &term );

// turn off Canonical processing in term
term.c_lflag &= ~ICANON;

// set the terminal configuration for stdin according to term, now
tcsetattr( fileno(stdin), TCSANOW, &term);

Note that this only improves the response; but, really you still have small delays as the terminal communicates to the kernel, and the kernel then communicates to your program.

For example, the kernel actually captures the key down / key up events, and evaluates them within a short timer period to determine if a key needs to "autorepeat".

My recommendation is that you don't venture into writing this code yourself if you are interested in getting your game out quickly. Instead use a game programming library that interacts and configures your environment appropriately, like allegro.

However, if you are interested in how this works under the covers, by all means write your code to handle it (as it is quite a fascinating topic and you really will understand the terminal / program / kernel communications much better!).

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • I have tried using programming libraries, but I can't seem to find where to get them or how to make them work correctly within my code. I tried '#include ' and '#include "EXAMPLENAME.h'" (in which I downloaded the package and put it in the program's folder) for outside programming packages, but I can't seem to get it to work. Can you recommend any websites for downloading packages? – Guy Aug 22 '16 at 21:55
  • Libraries like allegro and other game libraries have code which is windows / linux sensitive and will do the right thing based on the installed platform. It won't permit you to display while waiting for input the way you envision, but it will permit you to "detect key down" and "detect key up" like you might do for a game where the length of the key press is important, and you then do a "get input", "update data", "draw output" loop to handle drawing to the screen. – Edwin Buck Aug 22 '16 at 22:07
0

Start a thread that shows your message. Meanwhile, read the keyboard waiting for a hit. When the user hits enter, terminate the thread that shows the text and resume the normal flow jumping to the new screen.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Valmir Cinquini
  • 371
  • 7
  • 17
  • How do I create a thread? Sorry, I don't know I tried looking it up, but I don't get it. – Guy Aug 22 '16 at 23:51
  • 1
    Well you have threading libraries that run under Linux, but not on windoze, and those that run under windoze, but not on Linux. So if you are on Windows 10 with gcc, you have a Hobson's choice to make. – David C. Rankin Aug 23 '16 at 02:39
0

So.. I finally solved it! I created a new program to try to find how to fix the one I'm currently working on.

(Code goes in [...] of

#include <stdio.h>
#include <conio.h>
#include <windows.h>

void ClearScreen(void)
{
  system("cmd /c cls");
}

int main(void)
{
  [...] //Everything is in here for next examples
}

for next examples)

For Any Letter:

  int i=0;
  char name[20];

  while(1)
  {
    for(i=1; i>0&&i<5; i++)
    {
      //ClearScreen();
      printf("%i", i);
      printf("                                         Welcome to Guy's game!\n\n");
      Sleep(500);
      while (kbhit())
      { 
        getch();
        goto INPUT;
        i=0;
      }   
    }
  goto INPUT;
  }
  INPUT:
  ClearScreen();
  //this is the where it should skip
  printf("                                         Welcome to Guy's game!\n\n");
  printf("Please enter your name: ");
  gets(name);
  Sleep(250);
  printf("%s", name);
  return 0;

For Only One Letter:

  int i=0;
  char name[20];

  while(1)
  {
    for(i=1; i>0&&i<5; i++)
    {
       //ClearScreen();
       printf("%i", i);
       printf("                                         Welcome to Guy's game!\n\n");
       Sleep(500);
       while (kbhit()&&getch()== 'p')
       {
         goto INPUT;
         i=0;
       }   
    }
    goto INPUT;
  }
  INPUT:
  ClearScreen();
  //this is the where it should skip
  printf("                                         Welcome to Guy's game!\n\n");
  printf("Please enter your name: ");
  gets(name);
  Sleep(250);
  printf("%s", name);
  return 0;

You may use this in your code if you like! Good luck, and thank you for all of your answers!

*BTW I made it show numbers in the loop so that it would prove that the program works, but you can take that part out because it is not needed.

Guy
  • 41
  • 10