خرید بک لینک

Vote count: 0

I have been trying to create a function in C that reads in doubles that are then stored into an array. I want to be able to retu the size of the array so that I can use it in main. So the purpose of this function is to ask the user to input values into an array and type in ^d (ctrl+d) or EOF to end the loop.

#include <stdio.h>

//Prototype Declaration
int getdata(double[], int); 

int main(int argc, char* argv[]) { 
 double array[20]; 
 int count = 0, max = 20;

 //Calls the getdata function
 count = getdata(array, max); 
 printf("%s%lf%s","Array1: " ,array[1], "n");  
 printf("%s%d%s", "Count is: ", count, "n");
 retu 0;
}

----- (I'm linking modules so these are in different files) -------

#include <stdio.h>

//Define getdata.c
int getdata(double values[], int limit){
 printf("%s","Please enter your values into the array.n");
 int count = 0;
 double n;
 while ((count < limit) && (scanf("%lf",&n) != EOF)) {
  values[count] = n; 
  count++;
 } 
 retu count; 
}

What happens, though, is that if I break the loop early, the array values pass by fine but the count would just print out 20. I am assuming that the program automatically fills in the rest of the empty indexes with something and continues to increment the count value after I type in EOF. What can I do so that the program can correctly get the amount of values inputted? I do not code much in C. I mostly do my work in C++. I am familiar with passing by reference but using C.

asked 36 secs ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 2 فروردين 1395 ساعت: 5:01

صفحه بندی