Start New Discussion within our Software Development Community

I have to write a program that creates a dynamic array of five strings, store them and be able to access them.

This is the code I have written so far. The console receives the information that is inputted, but I believe does not store them. Also I commented out the cout on line 33 that was suppose to test the entry. Mainly because it makes the program go into an infinite loop. I've got a big suspicion that the whole thing is wrong, but I am not a hundred percent sure. Any help you can offer would be greatly appreciated.

#include<iostream>
#include <string>


using namespace std;
typedef char* CharArray;
const int NAMES = 5;
void getNames(string Name, CharArray nameArray);//getNames function declaration

int main()
{
     CharArray nameArray;
     nameArray = new char[NAMES];
     string Name;//Variable name for this string

     getNames(Name, nameArray);//Function call for getNames

return 0;
}

void getNames(string Name, CharArray nameArray)//Function definition
{     
     for ( int i = 0; i < NAMES; i++ )//Used to iterate the strings that are being created.
     {
          cout << "\nPlease enter name " << i + 1 << ".";//Output o instrct user to enter names.
          cin  >> Name;//Recieves info from user
     }
     // cout << Name:
}

I'm not sure I understand what you are trying to accomplish. You're reading in the names as std::strings into Name, but then you don't do anything else with it. You've got a colon after Name on line 29, so that's probably why it's getting stuck (colons can indicate a label in your program), but I'm not confident of how it compiled.

I think your typedef just makes everything more confusing, but that's a matter of opinion and style.

This article has been dead for over six months. Start a new discussion instead.