-1

I want my user to enter the input which I have set only for each and every declaration, but when I tried running the program it still proceeds, for example, name the ic_size the limit character is only 12 character input, but if I put more than 12 it just accepts it as nothing wrong. Here's the coding I tried:

void login_register()
{
    const name_size = 80;
    char name[name_size];
    const ic_size = 12;
    char ic[ic_size];
    const no_size = 12;
    char no[no_size];
    const nationality_size = 20;
    char nationality[nationality_size];
    const email_size = 50;
    char email[email_size];
    int select;
    int Day =0;
    int bed_tax =0;
    double RM;
    double room_price=0;
    double service_tax = 0;
    double total = 0;
    char term,check;


    printf("\n Please enter your name : ");
    while(gets(name))/* same as scanf ("%s",name) %s mean print the corresponding argument in string*/
    {
        if(!isalpha && sizeof(name) > name_size)           // restrict user input can only be alphabeth and must not exceed array size
        {
            printf("\n Please enter a valid input");
        }
        else
        {
            break;
        }
    }

    printf(" Please enter your IC number : ");
    while(gets(ic))
    {
        if(isdigit && sizeof(ic) < ic_size)               // restrict user input can only be numerical and must not excedd array size
        {
            printf("\n Please enter a valid input");
        }
        else
        {
            break;

        }
    }

    printf(" Please enter your phone number : ");
    while(gets(no))
    {
        if(isdigit && sizeof(no) < no_size)
        {
            printf("\n Please enter a valid input");
        }
        else
        {
            break;

        }
    }

    printf(" Please enter your nationality : ");
    while(gets(nationality))/* same as scanf ("%s",nationality) %s mean print the corresponding argument in string*/
    {
        if(!isalpha && sizeof(nationality) > nationality_size)           // restrict user input can only be alphabeth and must not exceed array size
        {
            printf("\n Please enter a valid input");
        }
        else
        {
            break;
        }
    }

    printf(" Please enter your email : ");
    while(gets(email))/* same as scanf ("%s",email) %s mean print the corresponding argument in string*/
    {
        if(!isalpha && sizeof(email) > email_size)           // restrict user input can only be alphabeth and must not exceed array size
        {
            printf("\n Please enter a valid input");
        }
        else
        {
            break;
        }
    }  
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Bronywyn
  • 1
  • 2

1 Answers1

1

Rather than gets(), which has no limit on user input, consider fgets().

Shift design goal to:

  • Allow unlimited input per line, but only save up to N characters of input.
  • Let user know if too much entered. Try again if needed.

I recommend a helper function that prompts, reads input, and handles long lines.

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

// 'size' is the size of the array 'destination' points to.
int get_user_input(const char *prompt, size_t size, char *destination) {
  char buf[size + 2];  // size + \n + 1 extra
  do {
    fputs(prompt, stdout);
    if (fgets(buf, sizeof buf, stdin) == NULL) {
      *destination = '\0';
      return 0; // End of file
    }
    
    char *end_of_line = strchr(buf, '\n');
    if (end_of_line) {
      *end_of_line = '\0'; // Lop off \n
    } else {
      // get rest of line
      scanf("%*[^\n]");  // Read everything up to a \n and toss
      scanf("%*1[\n]");  // Read a \n and toss
    }
  } while (strlen(buf) >= size);
  
  strcpy(destination, buf);
  return 1;
}

Now read as needed. Recall that name_size = 12 means up to 11 characters are saved as 1 more is needed to save the null character. Adjust name_size as needed.

int main(void) {
  const int name_size = 12;
  char name[name_size];
  const int nationality_size = 20;
  char nationality[nationality_size];

  get_user_input("Please enter your name: ", sizeof name, name);
  printf("Name <%s>\n", name);
  get_user_input("Please enter your nationality: ", sizeof nationality, nationality);
  printf("Nationality <%s>\n", nationality);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256