简体   繁体   中英

How Terminate the Program on Pressing Enter key in C?

i am making a small project in which I have to convert different values to different bases like 10,8,16.But the problem is that I want to run the program till the user press 6 but if user hit Enter key then too it is waiting for the input rather than simply terminating. I'm using C11 version of C on online compiler. and here's my code.

#include "ConvertInBackgnd"

#include<stdio.h>

int main() {
  int choice;
  printf("1 for Decimal to Binary\n2 for Binary to Decimal\n3 for Decimal to Octal\n4 for Octal to Decimal\n5 for Decimal to Hexadecimal \n6 for reconverting values \n");
  l1: printf("Input your choice : ");
  scanf("%d", &choice);
  switch (choice) {
  case 1:
    dec_bin();
    break;
  case 2:
    bin_dec();
    break;
  case 3:
    dec_octal();
    break;
  case 4:
    octal_dec();
    break;
  case 5:
    dec_hex();
    break;
  case 6:
    goto l1;
  default:
    printf("Invalid choice.");
    break;
  }
  printf("Input 6 for reconverting the values.");
  scanf("%d", &choice);
  if (choice == 6) {
    goto l1;
  } else
    return 0;
  return 0;
}

I have made a separate file in which I have made functions and I thought it isnot necessary to put that code here too.

Consider using fgets to take input into a character array.
If needed, the input can be parsed with sscanf , strtol or others.

#include<stdio.h>

int main() {
    char line[100] = "";
    do {
        printf("1 for Decimal to Binary\n2 for Binary to Decimal\n3 for Decimal to Octal\n4 for Octal to Decimal\n5 for Decimal to Hexadecimal \n6 for reconverting values \n");
        printf("Input your choice : ");
        fgets ( line, sizeof line, stdin);
        switch ( line[0]) {
            case '1':
                printf ( "dec_bin()\n");
                break;
            case '2':
                printf ( "bin_dec()\n");
                break;
            case '3':
                printf ( "dec_octal()\n");
                break;
            case '4':
                printf ( "octal_dec()\n");
                break;
            case '5':
                printf ( "dec_hex()\n");
                break;
            case '6':
            case '\n':
                break;
            default:
                printf("Invalid choice.");
                break;
        }
        if ( line[0] != '\n') {
            printf("Input 6 for reconverting the values.");
            fgets ( line, sizeof line, stdin);
        }
    } while ( line[0] == '6');
    return 0;
}

To solve the enter problem:

scanf("%d", &choice)

Right now you are taking int value, try with char value and match the enter key with it. Then you'll be able to do what you are trying to do.

Ok, So I wanted to terminate the program if Enter key is pressed. So I was first a newbie then I realized that itsnot a big deal and to do so I just need to take a char as Input and then check that if that char input is enter key or not. Its a sample code to terminate the program after pressing enter key.

 char line;

 scanf("%c",&line);

    if(line=='\n')
       return 0;
    else
    // your other code

But if your program doesnot take input then you should clear the buffer before the above provided code by adding just this single line before the above code.

 while(getchar()!='\n');

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