2

My code asks a the 10 digit number, it reads it as a string, pass it to another function that checks if the user's input is a real number and has no characters or symbols with ASCII, and then if it's good, with atof it changes the string into a number for a variable.

I want the user to only introduce 10 digits/characters on the input console, I mean, if the user would put a 11 character for example, the console just don't grab it, or in the case this is impossible for C, make that if the user put more than 12 characters on the input, then the program launches an error message saying it exceeds the limit, the problem is, when i tried to use this method, for example if i put some big numbers like a 40 digit number, then the program goes crazy and send just incomprehensible results like "1.#J", or if I put a character in middle of the numbers, then it sends the corresponding error message i set for the user to not put characters, but it still grabs part of the number and accept it as it is nothing wrong, here's the main of code I tried:

int main() {

    char chain[10];
    float N;
    int valid=0;

    do{
        printf("introduce real numbers: ");
        fgets(chain, sizeof(chain), stdin);
        if( chain[strlen(chain)-1] == '\n')
            chain[strlen(chain)-1] = '\0';
        valid=validate_numbers(chain);  
    }while(valid==0);

    N=atof(chain);

    printf("float number is: %.2f", N);

    getche();

    return 0;
}

Here's the rest of the code for more extense check: Pastebin

And sorry if there's some novice errors or the question is plain simple, im quite new programing.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Squall32
  • 101
  • 1
  • 2
  • 10

2 Answers2

1

Change this:

char chain[10];

to this:

char chain[11]; // +1 for the NULL terminator

since C-strings should be NULL terminated, thus we need one cell reserved for the NULL-terminator in our array (which will store our string).


I mean, if the user would put a 11 character for example, the console just don't grab it

Not possible in C.

or in the case this is impossible for C, make that if the user put more than 12 characters on the input, then the program launches an error message saying it exceeds the limit.

Yes, let's do that! Read the string, and if the length of it is more than 10 characters, then print an error message.

Allow chain array to be of size 12 (10 for the maximum length of the valid input, 1 for an extra character (if any) and 1 for the NULL-terminator), so that we can store the extra character, if any.

Example:

#include <stdio.h>
#include <string.h>
int main(void)
{
  char chain[12];
  printf("introduce real numbers:\n");
  fgets(chain, sizeof(chain), stdin);
  chain[strcspn(chain, "\n")] = '\0'; 

  if(strlen(chain) > 10)
  {
    printf("Error: Maximum length of chain is 10! Exiting..\n");
    return 1;
  }

  return 0;
}

Note: You could use EXIT_SUCCESS and EXIT_FAILURE, instead of plain numbers (1 and 0 respectively): Should I return 0 or 1 for successful function?


Irrelevant to OP's question: In the full version of your code though, there is a plethora of problems, such as this top line of code int valid=validate_numbers(char number[]);, which wishes to declare the method. It should be just validate_numbers(char number[]);. The same holds true for the definition of the method too. Make sure you go through all your code again, and read the messages the compiler gifts to you. :)

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Was just about to mention the function name. Another serious problem is using ```long``` as a variable name! See this https://stackoverflow.com/questions/37986813/is-long-a-data-type-or-qualifier-in-c for what that keyword is reserved for in C. – gstukelj Sep 20 '19 at 20:42
  • 1
    `chain[strlen(chain)-1]` is a hacker exploit by having the first read character as a _null character_. `chain[strcspn(chain, "\n")] = '\0';` is a good alternative. – chux - Reinstate Monica Dec 19 '19 at 04:04
  • @chuxreinstatemonica I see your point, answer updated, thanks! – gsamaras Dec 19 '19 at 10:58
0

What about using scanf instead of fgets? This should read 9 characters and save them as a string:

scanf("%9s" , &chain)

I'd suggest reading https://en.m.wikipedia.org/wiki/Scanf_format_string and man pages as well.

gstukelj
  • 2,291
  • 1
  • 7
  • 20
  • `scanf("%9s" , &chain)` does well limit input and prevent buffer overflow, yet leaves the trailing `\n` in `stdin` for the next I/O function. `fgets()` remains the better alternative. – chux - Reinstate Monica Dec 19 '19 at 04:06