Results 1 to 5 of 5

Thread: initializing a char? {Resolved}

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  2. #2
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    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"

  3. #3

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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");

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    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.

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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
  •  



Click Here to Expand Forum to Full Width