Reading an array with floating point numbers (C programming language)

arrays, c, floating-point, numbers

Solution

The proper format specifier matters a lot here.For float it is `%f`.So change your `scanf()` to

scanf("%f", &input);

Problem

I am creating a function to read in inputs from the user and put them into a floating point array of numbers with predetermined fix size of 25. It also returns the total amount of items that the user enters. However, for some reason this code will not terminate when I enter 999. I know it is something to do with that it is an int and input is a float, but I don't really know how to fix this (only been learning C for five days). ``` int readArray(float a[]){ //accepts inputs and puts items in a predefined array of size 25 int index = 0; float input; printf("Enter 25 or less elements for array (999 to finish):\n"); scanf("%d", &input); //accept initial response; priming prompt printf("1st Prompt accepted"); while (input != 999 && index < 25) { printf("In while loop"); a[index] = input; index++; scanf("%d", &input); } return (index); } ```

Original source