-4

So my program needs to consist of it forcing the user to input a string between 5-10 characters, with validation, so ensuring that the length is correct, i have this so far but completely stuck, any advice on how to validate the data type to only strings allowed to be inputted?

char x[10];
int length, i;

for(i=0;i=10;i=i+1){
    printf("Please enter 5-10 Characters\n");
    scanf("%s", &x);

    length = strlen(x);
    if (length < 5){
        printf("Not Long Enough!\n");
    }
    if (length > 10){
        printf("Too Long!\n");
    } 
    while('x' == 'char'){
        if (scanf("%s", &x) == 1){
            return 0;
        }else{
            printf("Not a string, Try again");
            gets(x);
        }

    }
    printf("You inputted: %s\n", x);
}
Ben Coyne
  • 1
  • 2
  • 5
    Looking at the first line `char x[10];` the array is not large enough to hold a *string* of length `10`. Looking at the first loop `for(i=0;i=10;i=i+1)` this won't do anything sensible, if you read it carefully. The line `scanf("%s", &x);` does not prevent a buffer overflow, should be `x` and not `&x` and no check was made of the function's success. Further down `while('x' == 'char')` makes no sense at all. Then `gets` is not a C function (any more). You need to get back to your C primer. – Weather Vane Feb 15 '18 at 13:53
  • 3
    `for(i=0;i=10;i=i+1)` What does this do according to you? Please [see](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Aditi Rawat Feb 15 '18 at 13:54
  • basics, try and debug you code... there are so many mistakes – Omer Dagan Feb 15 '18 at 14:42
  • `while('x' == 'char')` is never true. – chux - Reinstate Monica Feb 15 '18 at 14:56
  • Code cannot force a user to enter only 5-10 characters. Code can restrict what it accepts. – chux - Reinstate Monica Feb 15 '18 at 14:58
  • 2
    1) Look at your own code for more than a second, 2) use ANY compiler on the market, they all have warnings for unintended assignment inside conditions. 3) Burn your source of learning C with fire. `gets` is an extremely obsolete function for over 20 years. You are learning from someone who has been living underneath a rock for at least that long. – Lundin Feb 15 '18 at 15:03
  • It also meaningless to ask "is this input a string". It *is* a string, period. – Lee Daniel Crocker Feb 15 '18 at 21:40
  • I swear this website is the cancer of the internet, cheers for helping my self esteem trying to learn C! – Ben Coyne Feb 16 '18 at 14:41

1 Answers1

2

Various problems in code, so will center on the title topic.

Only string between 5 and 10 characters inputted?
any advice on how to validate the data type to only strings allowed to be inputted?


Use fgets() to read a line of user input. I would use a helper function.

scanf() does not well recover from errant input. Do not use it.

Be sure to use a large enough buffer to hold the 10 characters read and the appended null character. @Weather Vane

// Return 1 on success
// Return -1 on EOF
// Return 0 on failure (too long or too short)
// `x` must be at least size `max + 1`

int read_line_min_max(char *x, size_t min, size_t max) {
  if (fgets(x, max + 1, stdin) == NULL) return -1;
  size_t len = strlen(x);
  if (len > 0 && x[len - 1] == '\n') {
    x[--len] = '\0'; // lop off potential \n
  } else {
    // more data to read, but saving not needed, simply consume it
    int ch;
    while ((ch = fgetc(stdin)) != '\n' && ch != EOF) {
      len++;
    }
  }
  return (len >= min) && (len <= max);
}

Example

#define READ_MIN 5
#define READ_MAX 10
char buffer[READ_MAX + 1];
int result;
do {
  printf("Please enter 5-10 Characters\n");
  result = read_line_min_max(buffer, READ_MIN, READ_MAX);
} while (result == 0);

while('x' == 'char') is unclear.

Perhaps should be while(strcmp(x, "char") != 0)

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Should it be `char buffer[READ_MAX + 2];` to include the newline as well as the terminating nul? Or was it OK on the basis that the newline, if the 11th character, won't be read? – Weather Vane Feb 15 '18 at 17:09
  • @WeatherVane I went for the `READ_MAX + 1` as that is all that is needed by the caller. The `'\n'` of `"0123456789\n"` input is read in the `fgetc()` part. When function returns, `buffer[]` never contains a `'\n'`, so +1 is sufficient for the _null character_. – chux - Reinstate Monica Feb 15 '18 at 18:53
  • That is true, but the newline might be left in the input buffer and fox someone's next input ;) – Weather Vane Feb 15 '18 at 18:56
  • @WeatherVane The newline is not left in `stdin` with `read_line_min_max()`. If it is not found by `fgets()`, it is consumed by `fgetc()`. – chux - Reinstate Monica Feb 15 '18 at 18:58
  • 1
    Oh yes. The answer was worth my +1 – Weather Vane Feb 15 '18 at 19:01