I want to write code using a while loop, but I want to break the loop when I press a key, and if I did not press a key then the loop continues. For example, in this code, I want the program to print natural numbers until I press a key.
How can I do that?
int main()
{
    int i= 1;
    while(true)
    {   
       cout<<i<<" ";
       i++;
    }
}
 
    