Home > Enterprise >  the gets_s function in C language
the gets_s function in C language

Time:12-19

I wrote a test program, in this progtam there is a menu function, which allows user to choose a thing he wants to do. And there is a thing, if he chooses a task, where function gets_s is located and when, for example, user wants a task 2, he writes 2 and then presses "enter" and then the function gets_s thinks that this "enter" is for it and then my program finishes.

#include <stdio.h> 
 
void test() 
{ 
    char str[25]; 
    gets_s(str, 24); 
    printf("%s\n", str); 
} 
int menu() 
{ 
    int choice; 
    printf("Choose a task\n1 - task 1\n else...\n"); 
    scanf_s("%d", &choice); 
    return choice; 
} 
int main(void) 
{ 
    int x; 
    x = menu(); 
    if (x == 1) 
    test(); 
}

You can try this program and see that after your input 1, the program will finish. I don't understand why this happens, how can i do it without finishing the program?

CodePudding user response:

If by "without finishing," you mean you want your program to repeat accepting user input and taking appropriate action, you need a loop around your main logic:

int x = 0;
while (x >= 0) {
    x = menu(); 
    if (x == 1) 
        test(); 
    else if (x ==2) 
        test2();
    // etc.
}

CodePudding user response:

User input is significantly more difficult than people generally to think it is (or ought to be).

I personally find that a little helper function works, such as one to get an integer from the user. Here is one such way:

int ask_integer( const char * prompt )
{
  // Prompt the user for input
  if (prompt) 
    printf( "%s", prompt );

  // Get user’s answer
  char s[100];
  fgets( s, sizeof(s), stdin );
  char * p = strchr( s, '\n' );  // (find the newline/ENTER)
  if (!p) complain_and_quit();   // (not found == line too long == error)
  *p = '\0';                     // (strip the newline)

  // Convert user’s input to an integer, if possible
  int n = strtol( s, &p, 10 );
  while (isspace( *p ))   p;     // (skip over any trailing whitespace)
  if (*p) complain_and_quit();   // (fail if input not JUST an integer)

  return n;
}

Notice that there are a lot of things to check just to get an integer. Yes, it is obnoxious.

The method you use to handle errors is up to you. For most schoolwork assignments, any variation of a simple “complain and quit” is sufficient.

Notice the pattern:

  1. Obtain an entire line of input
  2. Attempt to validate/convert/process that input

The using code is now much simpler:

int menu(void)
{
  return ask_integer(
    "Menu:\n"
    "  1 - Quux\n"
    "  2 - Frob\n"
    "  ...\n"
    "? " );
}

int main(void)
{
  switch (menu())
  {
    case 1: test(); break;
    ...
  }
  return 0;
}

Or, using if:

int main(void)
{
  int choice = menu();
  if (choice == 1)
    test();
  else if (...)
    ...;
  else
    ...;
  return 0;
}
  • Related