0

Right now I am using a dynamically allocated double array. How can I check if someone has entered a character?

double* scanarray(int length)
{
    double* arr;
    arr = (double*)malloc(sizeof(double)*length);
    printf("Enter %d real numbers: \n", length);
    for (int loop = 0; loop < length; loop++)
        scanf_s("%lf", &arr[loop]);
    return arr;
}
anti mage
  • 41
  • 1
  • 5
  • 1
    You don't enter a character. You use `scanf_s` to scan doubles... – StoryTeller - Unslander Monica Mar 01 '17 at 08:59
  • 2
    You could check `scanf` return value. – el.pescado - нет войне Mar 01 '17 at 09:00
  • 6
    Also you should decide if you are using C or C++ - in C you shouldn't cast the return of `malloc` while in C++ you shouldn't use `malloc` at all – UnholySheep Mar 01 '17 at 09:01
  • 1
    In C a pointer to void can be assigned (promoted) to pointer of any type without a cast. – Ajay Brahmakshatriya Mar 01 '17 at 09:06
  • 1
    The line `arr = (double*)malloc(sizeof(double)*length);` should be `arr = malloc(sizeof(double)*length);`. See [this answer](http://stackoverflow.com/a/605858/2878796) for a detailed explanation – UnholySheep Mar 01 '17 at 09:09
  • @UnholySheep Why should'nt I cast the return of malloc? – anti mage Mar 01 '17 at 09:12
  • 1
    I linked an answer that explains it in detail... – UnholySheep Mar 01 '17 at 09:12
  • @anti mage: What exactly do you mean by "entered a character"? If someone enters `123a`, does that qualify as "entered a character" or not? – AnT stands with Russia Mar 01 '17 at 09:19
  • 4
    @anti mage: You shouldn't cast the result of `malloc` because there's absolutely no reason for doing so. When you need to calculate `3 + 2` you just say `3 + 2`, not `(int) ((int) 3 + (int) 2)`, don't you? If so, then why are you doing this `(double*)` thing with `malloc`? In fact, it should be `arr = malloc(length * sizeof *arr)`. No mention of `double` at all. – AnT stands with Russia Mar 01 '17 at 09:21
  • @AnT Yes, it does qualify for "entered a character". The array should only have real numbers. – anti mage Mar 01 '17 at 09:24
  • 1
    @anti mage: `scanf` does not provide you with any credible means to reject such inputs as `123a`. From `scanf`'s point of view, this is a prefectly valid input. If you want to reject it, forget about `scanf`ing numbers directly. Read strings and then parse, validate and convert them to numbers manually (i.e. by separate library functions like `strtod`) – AnT stands with Russia Mar 01 '17 at 09:26

1 Answers1

2

You could check return value of scanf (and related functions such as scanf_s):

Return Value

Each of these functions returns the number of fields that are successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.

https://msdn.microsoft.com/en-us/library/t6z7bya3.aspx