简体   繁体   中英

How to exit C program using Enter key

I'm trying to figure out a way to exit my program by pressing the Enter key when the program asks for a number.

I tried this if statement within my main() but it does not seem to work.

int main()
{
  while(1){
    int val;
    printf("\nnumber to convert:\n ");
    scanf("%i", &val);

    ibits(val);
    if (val = '\n')
    {
      break;
    }
  }

  return 0;
}

You really shouldn't use scanf directly... especially if you're expecting multiple possible formats.

Consider using fread instead and then converting the input to the proper format.

ie:

int main() {
  while (1) {
    char buf[1024];
    printf("\nnumber to convert:\n ");
    unsigned long len = fread(buf, 1, 1023, stdin);
    buf[len] = 0;
    if (len == 0 || (len == 1 && buf[0] == '\n') ||
        (len == 2 && buf[0] == '\r' && buf[1] == '\n'))
      break;
    int val = atoi(buf);
    ibits(val);
  }
  return 0;
}

This will also allow you to validate input and test for overflow attacks:

int main() {
  while (1) {
    char buf[1024];
    printf("\nnumber to convert:\n ");
    unsigned long len = fread(buf, 1, 1023, stdin);
    buf[len] = 0;
    if (len > 11)
      goto under_attack;
    if (len == 0 || (len == 1 && buf[0] == '\n') ||
        (len == 2 && buf[0] == '\r' && buf[1] == '\n'))
      break;
    if (buf[0] != '-' && (buf[0] < '0' || buf[0] > '9'))
      goto under_attack;
    int val = atoi(buf);
    ibits(val);
  }
  return 0;

under_attack:
  fprintf(stderr, "Under attack?!\n");
  return -1;
}

In my experience it is really just a pain to try to do this. It would be nice to be able to do this, but it is difficult.

In my experience with scanf if you hit enter without anything before it the scanf command just waits until you type some characters that appear on the screen and then hit enter .

You could get around this by changing the properties of the terminal you are working from, but then you need to change the properties back again when the program has finished. - you can then set it up so that the program responds to individual key presses rather than scanf where you have to type something then hit return . See this question for further details

Generally, it is easier to have an exit condition where for example you read in a value of 0 or a negative number, for example, with scanf to exit. This is really the simplest solution.

Alternatively you could load a number with a string input using

scanf("%s",string);
val = atoi(string);

atoi converts ascii to integer.

So now you could test the first character of the string to see if it is Q or q for quit.....

scanf("%s",string);
if (string[0]=='q' || string[0]='Q') return 0; 
val = atoi(string);

note that you define string with

char string[50];

or something similar.

Note that we should also check the return value of scanf as pointed out in the comments....

check=scanf("%s",string);
if (check!=1) {printf("scanf error\n"); return 0;}
if (string[0]=='q' || string[0]='Q') return 0; 
val = atoi(string);

As pointed out in the comments you can also read in the string with gets(string); instead of scanf("%s",string); .

Another good point from the comments is given that string is declared as [50] long it would be better if using scanf to use scanf("%49s",string); which will read in a maximum of 49 characters to prevent issues with string overflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM