TheVB10Guy TheVB10Guy - 1 year ago 85
C Question

How to make program not wait for input

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download