c++ - Adding and listing an array of structs -
i've been having few problems using array of structs , listing them , adding them. not sure what's happening, i've had search around , asked few friends , they've suggested memory allocation , has said memory allocation not needed , there problem in code.
i have been unable locate problem , wondering if able point me in direction of going wrong.
i apologies if code doesn't right - not sure on how implement on site.
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct data{ int testone; int testtwo; }data; struct data _datastore[25]; int datacount = 0; int adddata(struct data __datastore[datacount]){ printf("\n\t\t\tpacket source - "); scanf("%i", &_datastore[datacount].testone); printf("\t\t\tpacket destination - "); scanf("%i", &_datastore[datacount].testtwo); system("cls"); return 0; } void listdata(struct data _datastore[25]){ int = 0; (i = 0; < datacount; i++){ printf("data stored - %i",datacount); printf("%i___%i \n", _datastore[datacount].testone,_datastore[datacount].testtwo); } } int main(){ char choice; do{ printf("\t\t\t counter - %i", datacount+1); printf("\n\t\t\t1 - add data. \n"); printf("\t\t\t2 - list data. \n"); printf("\n\t\t\tenter choice - "); fflush(stdin); choice = getchar(); getchar(); switch(choice){ case '1': adddata(&_datastore[datacount]); datacount++; system("cls"); break; case '2': system("cls"); listdata(&_datastore[datacount]); break; default: printf("invalid input \n"); break; } }while (choice != '5'); return 0; }
there quite few apparent bugs, example:
for (i = 0; < datacount; i++){ printf("data stored - %i",datacount); printf("%i___%i \n", _datastore[datacount].testone,_datastore[datacount].testtwo); } }
i suspect wanted _datastore[i]
instead. also, suggest using more sensible function prototype:
int adddata(struct data* __datastore, int arraylen)
Comments
Post a Comment