|
-
Aug 23rd, 2005, 02:40 PM
#1
Thread Starter
Frenzied Member
initializing a char? {Resolved}
Ok, I feel really stupid for not know how to do this, and I can't even find it on the internet.
I want to initialize either a char or a string to have the value "x".
I've tried several ways:
char player = "x"; //doesn't work
string player("x"); //doesn't work
What am I doing wrong??
Last edited by System_Error; Aug 23rd, 2005 at 10:22 PM.
-
Aug 23rd, 2005, 03:12 PM
#2
Frenzied Member
Re: initializing a char?
char c[]="hi";
char c[3]="123";
char* dynamicC=new char[100];
delete dynamicC;
string s("Should work");
Have you
#include "cstring"
-
Aug 23rd, 2005, 04:02 PM
#3
Thread Starter
Frenzied Member
Re: initializing a char?
I'd like it to be a string i guess. I tried adding an include for the string:
#include <string>
that still didn't work. I do have this at the beginning of a class declaration like this:
class Test
{
string player("x");
Does that make any difference?
This is the beginning of my program. It's a simple console tic tac toe game(saw that in a magazine and thought it would be fun to try!).
Code:
/*
Import the files in the degree signs. The preprocessor
compiles and puts the files into the source code
*/
#include <cstdlib>
#include <iostream>
#include <string>
//usingnamespace std;
class TicTacToe
{
char board[3][3];
bool hasWon;
string player("x");
-
Aug 23rd, 2005, 07:34 PM
#4
Re: initializing a char?
It's because the variable is a member of a class that you're having difficulties.
you may need to initialize it like so:
Code:
#include <string>
class TicTacToe
{
private:
std::string player;
// ...
public:
// items following a colon after the constructor
// will be initialized with the specified value.
TicTacToe() : player("x")
{} // or simply { player = "x"; }
good luck!
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Aug 23rd, 2005, 10:21 PM
#5
Thread Starter
Frenzied Member
Re: initializing a char?
Thanks! I've got it working now. Thanks for the help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|