The following code contains commentary regarding why changes were made
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
// prototypes
int vecto();
int var;
double* vector;
char* bv;
int vecto()
{
int cont=0;
int ch;
char v[10];
// at this point, there is no allocated memory to free
//free(vector);
printf ("¿Number of elements to order?: ");
// need to chec the returned value from scanf
if( 1 != scanf("%d",&var) )
{ // then scanf failed
perror( "scanf for number of elements to order failed" );
exit( 1 );
}
// implied else, scanf successful
// need to check the returned value from malloc
// in C, do not cast the returned value from malloc
if( NULL == (vector = malloc(var*sizeof(double)) ) )
{ // then malloc failed
perror( "malloc for vector array failed" );
exit( 1 );
}
// implied else, malloc successful
printf("Input numbers press (f) for finish \n");
// any time inputting, need to chack for terminator
while( ((ch = fgetc(stdin)) != EOF) && (ch != '\n') && (ch != 'f') ){};
if( 'f' == ch )
{ // then exiting early
printf( "terminator entered before reading any lines\n" );
free( vector ); // cleanup
}
else
{
do{
if( NULL == fgets(v,sizeof(v),stdin) )
{ // then fgets failed
perror( "fgets failed" );
free(vector); // cleanup
exit( 1 );
}
// implied else, fgets successful
//if((strcmp(v,"f"))!=0)
// keep code simple
if( 'f' != v[0] )
{ // then terminator NOT entered by user
// add 'v', as double, to end of vector
// need to check that range of conversion was successful
double tempDouble = strtod(v,&bv);
if( ERANGE == errno )
{ // then conversion failed
printf( "conversion of %s to double failed", v );
}
else
{// else, conversion successful
vector[cont++]=tempDouble;
}
}
else
{ // else, terminator entered by user
cont = var;
}
// terminator already checked
} while( cont < var );
printf("\n");
} // endif
return 0;
} // end function: vecto