Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I was wondering how i can take in a input of unknown ( i.e. it count be strin

ID: 3645199 • Letter: S

Question

So I was wondering how i can take in a input of unknown ( i.e. it count be string, float , int... ect) data type through void pointers to then store that data type in a array to then pass through a sort fuction.

typedef struct List
{
void *data;

} List;

List *arr;
arr= new List[numelements];

cout<<" Enter in the elements one by one"<<endl;
for (int i=0; i<numelements;i++)// loop to input array elements
{
cin>>arr[i].data;
}

it works for ints. It doesnt work for floats or strings. Can anyone explain how make this work for floats and strings?

Explanation / Answer

Did you mean generic data types? If so here is a mini example on how to implement this based on the code you gave. You can replace the type with string or a float etc,etc. #include using namespace std; template struct List { T data; List *link; }; int main() { List *head = new List; List *ptr = new List; head->data = NULL; head->link = ptr; char a; coutlink; } ptr = head; while(ptr != NULL) { cout link; } return 0; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote