assinging strings to two dimensional array

R

Reginald Carlier

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
 
P

Pete C.

Reginald said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top