Is there a possible way to stop an incrementing array
ex. A[9]1~10

normaly it goes like 1 2 3 4 5 6 7 8 9 10

but is there a way that if a press a key it will stop at that point??

ex. 1 2 3 4 5[key pressed] then in stops?

is that in a loop of some sort? If it is, then just break out of the loop.

How to detect a key press is a different matter. There is no standard C way to detect the press of a single key because standard c functions always require you to also press the <Enter> key. For MS-Windows some compilers support the non-standard functions in conio.h which contains the function _kbhit() which tells you if one or more keys are available in the keyboard buffer. So your program could do something like this:

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

int main()
{
   int x = 0;
   printf("Press X to stop this\n");
   while( 1 ) // infinite loop
   {
       ++x;
       printf("\r%d", x);
       if( kbhit() )
       {
          if( getch() == 'X' )
             break;

        }
    }
    return 0;

Thank You ill Try this out
and also The program im trying to make is
a Timer Program that usses arrays and pointers

#include <stdio.h>
#include <conio.h>
int main()
{
int x = 0;
printf("Press X to stop this\n");<-------to here
while( 1 ) // infinite loop                |
{                                          |
++x;                                       |
printf("\r%d", x);                         |
if( kbhit() )                              |
{                                          |
if( getch() == 'X' )<---------------------here
break;                                     |
}                                          |
}                                          |
return 0;                                  |
clrscr();                                  |
}                                          |
                                           |
question is it possible to return from ----|
#include <stdio.h>
#include <conio.h>
int main()
{
int x,i;
x=0;
i=0;
printf("Press X to stop this\n");
e:
while( i<=5 ) 5 loops
{
++x;
printf("\r%d", x);
i++;
if( kbhit() )
{
if( getch() == 'X' )
goto e;
}
}
return 0;

why wont this run?

Don't use goto -- use loops instead. you can have loops within loops

#include <stdio.h>
#include <conio.h>
int main()
{
int x,i;
x=0;
i=0;
for(;;) // an infinite loop
{
    printf("Press X to stop this\n");
    for(i = 0; i < 5; i++) //5 loops
    {
       ++x;
       printf("\r%d", x);
       if( kbhit() )
       {
          if( getch() == 'X' )
             break; // exit the for loop
       }
    }
}   
return 0;

why wont this run?

this run but the loop goes on forever i only need 5 loops if try to remove the 1st for statement no errors but wont run
and also im trying to set delay on the loop after its done for the 1st loop im trying to make it accept enter to enter the loop again but so far all my programs have encountered difficulties.
result i want.

Press X(or any letter or number) to stop.

Time1 : 1(counting till 10) but when i press x then ilt stop at that point and then when i press enter
Time2 : 2<--- this goes up an does the same thing as time 1

ive tried different methods only ussing simple arrays and pointers and loops because for this assignment my proffesor told me to use only those functions.
is this possible or not?

This program runs great
Just by making some changes

#include <stdio.h>
#include <conio.h>
int main()
{
int x,i;
x=0;
i=0;
for(;;) // an infinite loop
{
     printf("Press x to stop this\n");
     for(i = 0; i < 5; i++) //5 loops
     {
         ++x;
         printf("\r%d", x);
         if( kbhit() )
         {     getch();                //Use this to accept a character
             if( getch() == 'x' )               //It stops the loop if 'x' is pressed once 
                 break; // exit the for loop    //If you want to countinue the loop again just
          }                                     //press'x' one more time..It will contine form    
     }                                          //from whre it has stoped 
}
return 0;
}
This article has been dead for over six months. Start a new discussion instead.