0

I am trying to dynamically allocate memory for an array that will hold a set of names. The memory has a max of five elements in the array and the user will manually type in a first, middle and last name on one line. I am trying to print out the elements of the array to make sure they are getting stored properly but for whatever reason the first element is blank. I am very lost on this. I have been trying to solve this issue but not is working.

playerAmount is a separate variable in another function where the user will enter how many players they want (range 2 - 5) and depending on that int playerAmount will ask the user's names depending on what int they put.

I am having the issue in the getName function

void amountOfPlayers(int &playerAmount) {

    cout << "Enter the amount of players: ";

    cin >> playerAmount;

    while (cin.fail()) { // Input Validation - if user enter's letters

        cout << "ERROR: must be a number, try again: ";

        cin.clear();

        cin.ignore(1000, '\n');

        cin >> playerAmount;
    }

    while ((playerAmount < 2) or
           (playerAmount >
            5)) { // Input Validation - if user enters numbers out of range

        cout << "ERROR: must be a number between 2-5, try again: ";

        cin.clear();

        cin.ignore(1000, '\n');

        cin >> playerAmount;
    }
}

void getName(int &playerAmount, string *&playerNames) {

    int i = 0;

    for (; i < playerAmount; i++) {

        cout << "Player " << i + 1 << " enter your full name: ";

        getline(cin, playerNames[i]);

        cin.clear();

        cin.ignore(1000, '\n');

        cout << playerNames[0];
    }
}

int main() {

    int playerAmount;

    string *playerNames = NULL;

    playerNames = new string[playAmount];

    amountOfPlayers(playerAmount);

    getName(playerAmount, playerNames);
}
O'Neil
  • 3,790
  • 4
  • 16
  • 30
http
  • 27
  • 1
  • 7
  • 8
    _I am trying to dynamically allocate memory for an array that will hold a set of names_ you need to forget about that, and start using `std::vector` instead. I can't even begin to describe how much easier it will be if you do. – Tas Feb 06 '18 at 00:28
  • 3
    At least you should wait and allocate the array only *after* you have input the number of players. – Bo Persson Feb 06 '18 at 00:30
  • 4
    `Operator new` is radioactive. DO NOT TOUCH. Forget you ever heard of it. – Jive Dadson Feb 06 '18 at 00:43
  • 1
    @JiveDadson ^ ... unless you're a north corean rocket engineer ;-D! –  Feb 06 '18 at 00:44
  • Use return by value, not `int& player_amount`. – Jive Dadson Feb 06 '18 at 00:46
  • `string *&playerNames` That's weird. – Jive Dadson Feb 06 '18 at 00:50
  • 1
    "north corean" this would be Santa's elite team of engineer tunneling their way through the center of the earth to make present delivery more efficient? – user4581301 Feb 06 '18 at 00:50
  • Victor, what sort of restrictions do you have on this assignment? Are you allowed to use `std::vector`? Crom. I feel like I just [found my way into Airplane!](https://www.youtube.com/watch?v=fVq4_HhBK8Y) – user4581301 Feb 06 '18 at 00:52
  • 1
    @user4581301 Clearly the sequel to the movie *Core*, except it has The Rock trying to deliver presents. – tadman Feb 06 '18 at 00:54
  • @user4581301 I cannot use vectors! – http Feb 06 '18 at 02:24
  • I think my idea for the code is right BUT I think I am missing stuff on the syntax side. That is what is what I am looking for. – http Feb 06 '18 at 02:25
  • When you can't use `vector` my stock suggestion is that you write your own poor man's `vector`. Make a class that does the sizing (and maybe resizing) you need to do and overload the [] operator. Take [The Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) into account and you have a reasonably solid class you can use in assignments yet to come. And probably learn more than the rest of the class. [Write up of a vector and everything they got wrong so you can see what to do/avoid](https://codereview.stackexchange.com/questions/138389/my-own-stdvector). – user4581301 Feb 06 '18 at 02:48

2 Answers2

1

You need to initialize playerAmount before you use it.

You also need to spell it correctly when you use it.

Sid S
  • 6,037
  • 2
  • 18
  • 24
  • TBC: I'm not _stalking you_! –  Feb 06 '18 at 01:09
  • _"In C++ `or` is spelled `||` or `|` depending on what you want to do. It's not spelled `or`."_ Huh?? [**`or`**](http://en.cppreference.com/w/cpp/language/operator_alternative) is a valid keyword according the standards? –  Feb 06 '18 at 01:11
  • 1
    `or` is a valid C++ keyword equivalent to `||`. Also this is a pretty low effort answer as there are quite a few problems with OP's code that are not addressed at all. – patatahooligan Feb 06 '18 at 01:12
  • I guess the compiler I use is not standard compliant when it comes to that, then. I'll remove it from the answer, although I still think it is a bad idea to use such features when major compiler vendors don't support them. – Sid S Feb 06 '18 at 01:22
1

the problem seems to be with input buffer. I used cin.ignore() and cin.clear() in your amountOfPlayers() function after reading the playerAmount:

cin >> playerAmount;
cin.ignore(1000, '\n');
cin.clear(); 

I tested it and it seems to work.

Also, as it was mentioned in other answer by Sid S, playerAmount should be initialized before allocating string:

 amountOfPlayers(playerAmount);  //initialize playerAmount
 playerNames = new string[playerAmount]; //allocate memory for that size
  • So, I initialized playerAount to 5 and it works! It seems like when I cout playerAmount all the indexes work besides 0. So, that is where my issue lays – http Feb 06 '18 at 13:24
  • I thought my issue laid in the cin.ignore and cin.clear because you are telling the program to ignore and clear the first bit of text. – http Feb 06 '18 at 13:29