error input ke dalam array (anggota struct) :: C

typedef struct{
 char **Word;
}Data;

void clearBuffer();
void inputData(Data *inputData,int Index,int arraySize,char *strTemp);
void showData(Data *printData,int Index);
void Menu(Data *Instance);

int main(void){
 Data *data = calloc(1,sizeof(Data));
 Menu(data);
 free(data);
}

void inputData(Data *inputData,int Index,int arraySize,char *strTemp){
 inputData->Word = realloc(inputData->Word,arraySize*sizeof(Data)); /* inisialisasi index */
 inputData->Word[Index] = calloc(255,sizeof(char)); /* inisialisasi index ke-n */
 strcpy(inputData->Word[Index],strTemp);
}

void showData(Data *printData, int Index){
 for(int Idx = 0;Idx<Index;Idx++){
  printf("[%d]: %s\n",Idx+1,printData->Word[Idx]);
 }
}

void Menu(Data *Instance){
 int Select;
 int Index=0,arraySize=1;
 while(true){
  printf(
         "1.input word\n"
         "2.delete all data\n"
         "3.show data\n"
         "4.exit\n"
         "input: ");
  scanf("%d",&Select);
  clearBuffer();
  if(Select==1){
   char *strTemp = (char*) calloc(10000,sizeof(char));
   printf("input word: ");
   scanf("%s",strTemp);
   clearBuffer();
   inputData(Instance,Index,arraySize,strTemp);
   ++arraySize;
   ++Index;
   free(strTemp);
  }
  else if(Select==2){
   printf("\ndata has been destroyed\n");
   free(Instance->Word);
   Index=0;
   arraySize=1;
  }
  else if(Select==3){
   showData(Instance,Index);
  }
  else if(Select==4){
   printf("\nexit\n");
   exit(EXIT_SUCCESS);
  }
  else{
   printf("\ninvalid input\n");
  }
 }
}

void clearBuffer(){
 while(getchar() != '\n' );
}

misi gan. ane lagi bikin program input data ke 2d array tiap index bertipe array of char (string). misal input: "nama saya acil" hasi print: "nama". artinya tersimpan ke dalam char bukan array of char (string). mohon bantuan perbaikanya, jawaban apapun sangat ane apresiasi :D #janganbully

compiler : llvm clang 6.0.0-1 amd64

avatar MaySirus
@MaySirus

2 Kontribusi 0 Poin

Dipost 6 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban