c - How I can fix this array -


good morning, trying fill dynamic vector when print input data this:

input: 1,5,3,4,2 output: 0,1,5,3,4 

could me? can't fix it. i've been trying lot , can't it. i'll apreciatte (sorry english)

#include <stdio.h> #include <string.h> #include <stdlib.h>  #define max_str 10  int vecto();  char seguir[max_strlen]; int var;  float* vector;  char* bv;  int vecto(){ int cont=0,ch; char v[max_str]; printf ("¿number of elements order?: "); scanf("%d",&var); vector = (float*)malloc(var*sizeof(float)); while((ch = fgetc(stdin)) != eof && ch != '\n' ){};  printf("input number press f finish \n");     do{         fgets(v,sizeof(v),stdin);         if((strcmp(v,"f")!=0)){             cont++;             vector[cont]=strtod(v,&bv);         }     } while(!((cont==var) || (strcmp(v,"f")==0))); printf("\n"); return 0; } 

you increment cont before set value.

just swap order

if((strcmp(v,"f")!=0)){             vector[cont]=strtod(v,&bv);             cont++;         } 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -