assinging strings to two dimensional array

Discussion in 'C++' started by Reginald Carlier, Jun 10, 2004.

  1. Hi,

    I'm programming a game and one of the variables is a two dimensional
    array in wich I store the names of the players. So far so good; with
    cin.get I ask the names of the players and everything works fine.
    Now I want the player also to be able to play against the computer and
    I don't want him to fill in the name "computer" interactively; I want to
    set the second element of the two-dimensional array to computer.
    When i do
    player[2][30];
    player[1]="computer";
    I get an error that the compiler can not convert from a char[9] type to
    a char[30] type wich I can understand.
    but when i do
    player[1][0]={'c'}; the comiler complains of missing ;

    Does anyone know how to solve this problem?
    The goal is to get a two dimensional array containing the name of the
    first player in the first array. (this is done with cin.get(player[0],max);)
    In the second I want to store the name computer.

    Reginald
    Reginald Carlier, Jun 10, 2004
    #1
    1. Advertising

  2. Reginald Carlier

    Pete C. Guest

    Reginald Carlier wrote:
    > Hi,
    >
    > I'm programming a game and one of the variables is a two dimensional
    > array in wich I store the names of the players. So far so good; with
    > cin.get I ask the names of the players and everything works fine.
    > Now I want the player also to be able to play against the computer
    > and
    > I don't want him to fill in the name "computer" interactively; I want
    > to set the second element of the two-dimensional array to computer.
    > When i do
    > player[2][30];
    > player[1]="computer";
    > I get an error that the compiler can not convert from a char[9] type
    > to
    > a char[30] type wich I can understand.
    > but when i do
    > player[1][0]={'c'}; the comiler complains of missing ;
    >
    > Does anyone know how to solve this problem?
    > The goal is to get a two dimensional array containing the name of the
    > first player in the first array. (this is done with
    > cin.get(player[0],max);) In the second I want to store the name
    > computer.
    >
    > Reginald


    strcpy(player[1], "computer");

    But, I would suggest using vectors and strings instead:

    #include <string>
    #include <vector>
    #include <iostream>

    std::vector<std::string> player(2);
    std::getline(std::cin, player[0]);
    player[1] = "computer";

    All memory management is automatic, you can resize the array
    (player.resize(length);), and there is no risk of the strings being to long
    (as long as they can fit in your memory).

    - Pete
    Pete C., Jun 10, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg
    Replies:
    0
    Views:
    365
  2. Alf P. Steinbach
    Replies:
    0
    Views:
    423
    Alf P. Steinbach
    Aug 18, 2003
  3. John Harrison
    Replies:
    4
    Views:
    6,914
    Default User
    Aug 19, 2003
  4. Icosahedron
    Replies:
    8
    Views:
    641
    Vivek
    Aug 21, 2003
  5. Venkat
    Replies:
    4
    Views:
    963
    Venkat
    Dec 5, 2003
Loading...

Share This Page