I have been working on this code for a dynamic array and we were inputting number, but now I need it to hold String. The program asks the user how many names you would like to display and then it displays them. My problem is how to change it from int to string. I have tried a few times, but keep bombing out. This should be easy but i'm just not seeing it. Amy help would be appreciated! Thanks!
// Author: Jessie
//Date: September 20, 2012
//Program: Election
//Description: This program will ask the user for the last names of 5 candidates and place them into an array.
//Then the user will vote and the votes will be tallied and the winner displayed.
int main()
{
int wait;
int arraysize;
int *names; // pointer will hold the starting address of the dynamic array //
cout << "How many Candidate Names do you wish to enter: ";
cin >> arraysize;
if (arraysize > 0)
{names = createArray(arraysize);
initializeArray(names, arraysize);
inputNames(names, arraysize);
outputNames(names, arraysize);
}
cin >> wait;
return 0;
}
int * createArray(int arrsize)
{
int *array;
if (arrsize <= 0) //the value given to the pointer is NULL to indicate no memory locations are available
return NULL;
array = new int[arrsize]; //create the dynamic array when the program is executed //
return array; // uses a pointer to return the starting address of the array //
}
void initializeArray(int array[], int arrsize)
{
int i;
for (i=0; i < arrsize; i++)
array[i] = 0;
return;
}
void inputNames(int array[], int arrsize)
{
int i;
//char name[31];
//cin>> name;
for (i=0; i < arrsize; i++)
{
cout<< "Enter name # "<< i+1 <<": ";
cin>> array[i];
}
return;
}
void outputNames(int array[], int arrsize)
{
int i;
for (i=0; i < arrsize; i++)
{
cout<< "\nName # "<< i+1 <<"is ";
cout<< array[i];
}
return;
}
int main()
{
int wait;
int arraysize; char **names; // pointer will hold the starting address of the dynamic array //
cout << "How many Candidate Names do you wish to enter: ";
cin >> arraysize;
if (arraysize > 0)
{names = createArray(arraysize);
initializeArray(names, arraysize);
inputNames(names, arraysize);
outputNames(names, arraysize);
}
cin >> wait;
return 0;
} char** createArray(int arrsize)
{
char** array;
if (arrsize <= 0) //the value given to the pointer is NULL to indicate no memory locations are available
return NULL;
array = new (char*)[arrsize]; //create the dynamic array when the program is executed //
return array; // uses a pointer to return the starting address of the array //
}
void initializeArray(char** array, int arrsize){
int i;
for (i=0; i < arrsize; i++)
array[i] = NULL;
return;
}
void inputNames(char** array, int arrsize)
{
int i;
for(i=0;i<arrsize;i++)
{
array[i] = new char[31];// or scan the size and specify that instead of 31
cout<< "Enter name # "<< i+1 <<": ";
cin>> array[i];
}
return;
}
void outputNames(char** array, int arrsize){
int i;
for (i=0; i < arrsize; i++)
{
cout<< "\nName # "<< i+1 <<"is ";
cout<< array[i];
}
return;
}
Hope this helps.Also freeing the dynamically allocated memory is must.
@Jackson Marie - STL i.e. std::string, std::vector etc... are much safer that managing the memory yourself, they are written for efficiency and have a lot of algorithms/functions you can use. If you need to store strings as above, use a vector but if you need to be able to search the container for specific items then use a map... HTH
Here we have a program that will rely on the user typing something in. That will take ENORMOUS amounts of time compared to everything else going on here. To choose manual memory management for reasons of speed in such a situation is insane.