-1

I'm trying to have the user hit the ENTER key to exit my program. I am using the

(void) getchar();

getchar function to wait for the user to press the ENTER key. This worked for a simple Hello world program that did not request any user input, but in this program with the user inputting a double, the program skips the getchar command. If I double-click on the "Lab 1B.exe" executable code, after the user inputs the double side A of the triangle, the executable window with the running program disappears (the program skips the getchar command). How do I get the program to wait for the user to type the ENTER key?

How do I get the program to wait for the user to type any key???

#include <stdio.h>

int main(void)
{
    double a = 0, b = 0, c = 0;
    //int x = 0;
    double x = 0;

    printf("Lab 1B - Triangles:\n\n");
    printf("Enter the length of your triangle's side A: ");
    scanf_s("%lf", &a);
    printf("The length of the triangle's side A is: ");
    printf("%lf", a);
    printf("\n");


    printf("\nPress the <ENTER> key to exit.\n");
    (void) getchar();
    
    //printf("\nEnter any value to exit: ");
    //scanf_s("%i", &x);
    //scanf_s("%lf", &x);
    return 0;
}
  • Assuming `scanf_s` is like `scanf`, you might just try adding a space to the end of the format string: `scanf_s("%lf ")` – William Pursell Feb 10 '21 at 02:57
  • @WilliamPursell A poor idea (for `scanf` anyway, IDK about scanf_s) , as that means it will never stop reading until a non-whitespace character is input – M.M Feb 10 '21 at 04:05
  • All of the answers posted here are wrong; please see the question we've linked that has good answers (or at least, a good accepted answer) – M.M Feb 10 '21 at 04:07
  • @M.M A better characterization is "it will skip whitespace". That is usually the desired behavior, and seems to be what the OP wants. – William Pursell Feb 10 '21 at 13:35
  • @WilliamPursell OP wants the code to continue with the next statement once the person types their response and presses Enter (which will not happen with your suggestion) – M.M Feb 11 '21 at 08:05

2 Answers2

0

scanf() leaves a trailing newline character that is later consumed by your getchar() call. If you are always getting the input via the user and not redirection or another FILE stream, you could have another getchar() call after your scanf() call. You should check the result of scanf() more on this here.

#include <stdio.h>

int main(void)
{
  double a = 0, b = 0, c = 0;
  double x = 0;

  printf("Lab 1B - Triangles:\n\n");
  printf("Enter the length of your triangle's side A: ");

  if (scanf("%lf", &a) != 1) /* scanf_s behaves similar in terms of the return value */
  {
    fputs("error msg",stderr);
    return -1;
  }
  getchar(); /* consuming the leftover newline */

  printf("The length of the triangle's side A is: %lf\n",a);

  printf("\nPress the <ENTER> key to exit.\n");

  while (getchar() != '\n') /* waiting for newline input */
    ;

  return 0;
}

Alternatively and far better, you could use fgets()to get the input as a string from the user and later use sscanf() to parse it.

#include <stdio.h>

#define MAX_SIZE 500

int main(void)
{
  char buff[MAX_SIZE] = {0};
  double a = 0, b = 0, c = 0;
  double x = 0;

  printf("Lab 1B - Triangles:\n\n");
  printf("Enter the length of your triangle's side A: ");

  fgets(buff,MAX_SIZE,stdin);

  if (sscanf(buff,"%lf",&a) != 1)
  {
    fputs("error msg",stderr);
    return -1;
  }

  printf("The length of the triangle's side A is: %lf\n",a);

  printf("\nPress the <ENTER> key to exit.\n");

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

  return 0;
}
alex01011
  • 1,670
  • 2
  • 5
  • 17
  • The first solution doesn't handle the user typing extra characters after the number before pressing enter ; the second one doesn't handle input larger than MAX_SIZE , and both of them go into an infinite loop if the input is closed . See linked duplicate question for some code that addresses all of these issues – M.M Feb 10 '21 at 04:11
-1

The issue is because scanf function only reads until non-space or non-newline character. After executing scanf(), \n from the first input is still in the buffer. When you call getchar() again, it accepts the newline character and the program terminates.

One easy way to fix it is to call getchar() again right after scanf to consume the newline character.

Another way is to put space or newline character in scanf. Reference: https://www.geeksforgeeks.org/problem-with-scanf-when-there-is-fgetsgetsscanf-after-it/

scanf_s("%lf ", &a);
// or
scanf_s("%lf\n", &a);
thuyein
  • 1,684
  • 13
  • 29
  • When I enter the blank space or the '\n' character into the format string of the scanf_s command, the user needs to enter TWO values (two double values followed with the key each) to get past the Side A input, and then the program AGAIN skips the getchar() command! – Paolo Barone Feb 10 '21 at 03:12
  • A trailing whitespace in the format string is an awful idea because it means to never stop reading until a non-whitespace character is entered – M.M Feb 10 '21 at 04:08