C++ Beginner Question

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.

#include "stdafx.h"
#include <iostream>

using namespace std;

int * createArray(int);
void initializeArray(int[], int);
void inputNames(int[], int);
void outputNames(int[], int);


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;
}
You can't change a container from one type to another. Just make a new container.
I have to store the votes and the candidates names so I thought that maybe I could leave the int and somehow add char so that it will store both, but i know that doesn't sound right because it can only hold one type of data, correct?
Is there some way that I can use pointers so that i don't have to scrap this?
Of course you can.
Just change the type of your array to what suits your needs.
You say you want to store a name associated associated with a count. Why not use a simple structure like this:
1
2
3
4
5
struct CandidateInfo
{
    std::string Name;
    int NumberOfVotes;
};

That way the candidate's name and his votes will be associated in the same object, which makes things simpler.

Oh, and since you're programming in C++, you should use std::string instead of char*.
I've been changing the container that's why I'm still doing same thing. I'll just make another new container for this one. Thanks bro.
Thanks!
Topic archived. No new replies allowed.