-2

Fixed

system("read -r -p \"Press any key to continue...\" key")

I am now writing a command line tool and using C language, but I am wondering how to pause the program? (Not abort it, but can be re-continued) I was trying to make it like "Press Enter To Continue".

I am on Linux so I don't have or what, I have , and I tried "pause()", but this function cannot realize "Press Enter To Continue", it cannot be re-continued.

Wonder if there is a function or what can help me to do that? If yes, please tell me, thanks!

PS: These are my codes above, as you may see, I am using a function named "conti" to set a pause

PLUS: Ignore those "delay" functions, just doing tests.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void delay500ms(void){
    unsigned char i,j,k;
    for(i=15;i>0;i--)
        for(j=202;j>0;j--)
            for(k=81;k>0;k--);
}

void delay1s(void){
    unsigned char h,i,j,k;
    for(h=5;h>0;h--)
        for(i=4;i>0;i--)
            for(j=116;j>0;j--)
                for(k=214;k>0;k--);
}

int pause(void);

int ldown (int ln)
{
    int k;
    for (k=0;k<ln;k++)
    {
        printf("\n");
    }
}

void conti ()
{
    printf("\n\nPress Enter To Continue...\n\n");
}

void cmd ()
{
    ldown(100);
    system("toilet -f mono12 -F metal S-MODE");
    ldown(4);
    printf("[1] Emergency Reboot\n");
    printf("[2] Clean All Progress\n");
    printf("[3] Change Into Pure Net\n");
    printf("[4] Change Into Normal Net\n");
    printf("[5] Edit This Script\n");
    printf("[6] Rebuild This Script\n");
    printf("[7] Delet This Script\n");
    printf("[8] Exit S-MODE\n\n");
    printf("Now Please Type Your Command [1~8]:\n");
    int cmdn;
    cmdn = 8;
    scanf("%d",&cmdn);
    ldown(4);
    switch (cmdn)
    {
        case 1:
            printf("Checking Root Permision...\n\n");
            system("reboot");
            printf("\n\nShutting Down The System...\n\n");
            conti();
            break;
        case 2:
            printf("Cleaning All Progress...\n\n");
            system("kill -9 -1");
            conti();
            break;
        case 3:
            system("pure");
            conti();
            break;
        case 4:
            system("unpure");
            conti();
            break;
        case 5:
            printf("Checking Root Permision...\n\n");
            system("sudo vim /bin/engage.c");
            break;
        case 6:
            printf("Checking Root Permision...\n\n");
            system("sudo gcc /bin/engage.c -o /bin/engage");
            printf("\n\nScript Rebuilt! Please Restart The Script\n");
            conti();
            break;
        case 7:
            printf("Checking Root Permision...\n\n");
            system("sudo rm /bin/engage");
            exit(0);
            break;
        case 8:
            printf("Exiting...");
            ldown(10);
            exit(0);
            break;
        default:
            printf("Unknow Command :(\n\n");
            break;
    }
}

void main()
{
    do
    {
        cmd();
    } while (1==1);
}   
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Coder BTS
  • 13
  • 1
  • 6

3 Answers3

0

You can do it like that:

#include <stdio.h>
int main(){
  puts("Press any key to continue ... ");
  getchar();
}

If you really need to make it enter-accepted:

#include <stdio.h>
int main(){
  puts("Press enter to continue ... ");
  while(getchar()!=27);
}

27 is enter keycode.

Another C solution would be:

system("read -n1 -r -p \"Press any key to continue...\" key")

In c++ it would look like that:

#include <iostream>
int main(){
  std::cout << "Press enter to continue ... " << std::endl;
  std::cin.ignore(INT_MAX);
  cin.get();
}
Kamila Szewczyk
  • 1,874
  • 1
  • 16
  • 33
0

There are several methods to do this, what you can do is to add one of these into your void conti() function.

getchar

  • returns the character read as an unsigned char cast to an int or EOF on end of file or error, getchar();

In POSIX systems, you can use :

#include <unistd.h>

pause ();

In Windows,you can use kbhit().

kbhit() function

  • Is used to determine if a key has been pressed or not. To use kbhit() in C or C++ programs you have to include the header file "conio.h".

Sample usage of kbhit():

while (!kbhit())
    printf("Howdyy you haven't pressed a key.\n");
Kamila Szewczyk
  • 1,874
  • 1
  • 16
  • 33
danglingpointer
  • 4,708
  • 3
  • 24
  • 42
-5
#include <conio.h>
#include <stdio.h>
int main(){

  while(1){
    puts("Press command ... ");

  }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks for taking the trouble of answering, but please read http://stackoverflow.com/help/formatting, it seems that it would help you improving your answer. Also, I doubt the usefulness of an endless loop, which does output repeatedly, but ingores input. It does not really match the stated problem. I did not downvote, but what I described might have been on the mind of whoever did. Consider getting the badge for taking the tour http://stackoverflow.com/tour – Yunnosch Apr 18 '17 at 15:24
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/16222767) – Uyghur Lives Matter May 24 '17 at 18:02
  • @cpburnz I don't think that an answer being _wrong_ is a valid reason for deletion. After all, it _does_ "pause" the program like the OP requested (the fact it also makes the program useless is a different issue). This is exactly why downvotes exist... – Dev-iL May 25 '17 at 09:26