char* pointing to char help

Started by
10 comments, last by JasonBlochowiak 17 years, 9 months ago
In my code, I'm trying to get char* Humanturn to point to the return value of the AskYesorNo function. I return the address of some global const chars, but get a compile error on these return addresses. A little help please. :)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

//function prototypes
void DisplayInstructions();
char GetHumanTurn();
char* AskYesorNo(string question);

//constant characters
const char EMPTY = ' ';
const char X = 'X';
const char O = 'O';

int main()
{
    DisplayInstructions();
    vector<char> board(8, EMPTY);
    char Human = (GetHumanTurn());
    //testing purposes
    cout << endl << Human;
    
    system("PAUSE");
    return 0;
}

void DisplayInstructions()
{
     cout << "Welcome to Tic Tac Toe! \nWhere you will test your might against man's greatest creation, the computer!\nBe prepared to go one on one with the processor as you battle for supremacy,\nand the entire human race!" << endl;
     cout << "The rules are simple, pick a number that corresponds with the position on the \nboard.\n\nExample board:\n\n";
     cout << "| 0 | - - | 1 | - - | 2 |\n\n| 3 | - - | 4 | - - | 5 |\n\n| 6 | - - | 7 | - - | 8 |" << endl;
}

char GetHumanTurn()
{
      char* HumanTurn = AskYesorNo("\nDo you want to go first? (y or n)");
      if(*HumanTurn == X)
      {
                    return X;
                    }
      else
      {
          return O;
          }
}

char* AskYesorNo(string question)
{
      char answer;
      cout << endl;
      do
      {
      cout << question;
      cin >> answer;
      }
      while(answer != 'y' && answer != 'n');
      if(answer == 'y')
      {
                return &X;
                }
                if(answer == 'n')
                {
                          return &O;
                          }
}     


 E:\Documents and Settings\Kev\Desktop\Book Exercizes\tictactoewithpntrs.cpp In function `char* AskYesorNo(std::string)': 
61 E:\Documents and Settings\Kev\Desktop\Book Exercizes\tictactoewithpntrs.cpp invalid conversion from `const char*' to `char*'
65 E:\Documents and Settings\Kev\Desktop\Book Exercizes\tictactoewithpntrs.cpp invalid conversion from `const char*' to `char*' 

Advertisement
So why not have AskYesorNo return a "const char*". Also why return a pointer? I don't really see the need, you could just make it "char". It's not like copying a single char is going to kill performance.
Yes, but it's for practice :) And I don't really understand what your saying. . .return a pointer to a const character?
If you want to do this the way you have you need to cast away the const part of the global chars. In askyesorno there is a logic error
while(answer != 'y' && answer != 'n');

Well you can implicitly cast const onto things, but not off of things. You would either have to return a const char* or change the EMPTY, X and O to char*.

Dave
Quote:Original post by Dave
Well you can implicitly cast const onto things, but not off of things. You would either have to return a const char* or change the EMPTY, X and O to char*.

Dave


const_cast
You created the vars as const up top. If you want to return the address of them, you have to return it in a const pointer (to ensure const-ness).

Check out my new game Smash and Dash at:

http://www.smashanddashgame.com/

Quote:Original post by Anonymous Poster
Quote:Original post by Dave
Well you can implicitly cast const onto things, but not off of things. You would either have to return a const char* or change the EMPTY, X and O to char*.

Dave


const_cast


implicit [smile]
Do we really want to be recommending a dirty hack like const_cast in For Beginners? It should only be used in extreme circumstances, and almost exclusively when working with code written by a dumbass who doesn't know how to properly use const in their interfaces.

Check out my new game Smash and Dash at:

http://www.smashanddashgame.com/

@rip-off & Dave
sorry I missed that :(

@JBourrie
Not really no, infact just NO.
I was just saying if the OP wanted to do it as he already had.

This topic is closed to new replies.

Advertisement