Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > assinging strings to two dimensional array

Reply
Thread Tools

assinging strings to two dimensional array

 
 
Reginald Carlier
Guest
Posts: n/a
 
      06-10-2004
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


 
Reply With Quote
 
 
 
 
Pete C.
Guest
Posts: n/a
 
      06-10-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM
Re: Two dimensional pointers and Two dimensional arrays Icosahedron C++ 8 08-21-2003 05:15 AM
Re: Two dimensional pointers and Two dimensional arrays John Harrison C++ 4 08-19-2003 04:00 PM
Re: Two dimensional pointers and Two dimensional arrays Alf P. Steinbach C++ 0 08-18-2003 08:25 AM



Advertisments