Jump to content
  • Advertisement
Sign in to follow this  
kevtimc

char* pointing to char help

This topic is 4525 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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*' 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Yes, but it's for practice :) And I don't really understand what your saying. . .return a pointer to a const character?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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');


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest 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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
@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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!