• Advertisement
Sign in to follow this  

char* pointing to char help

This topic is 4257 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
Thanks for eveyone's help, I just get rid of the annoying const in the global chars. Didn't feel like doing any const to non-const conversion, not like I'm going to accidentally change the chars anyway. :)

P.S. The program works thus far. . .

Share this post


Link to post
Share on other sites
Umm, wrong choice, removing const (or using const_cast). You should be as willing to remove const as you are to stick your hand into a garbage disposal that's turned on - you may need to do it from time to time, but you should really think about it first.

However, there are many layers here that could be improved. To start, if you're going to be returning a pointer to a const thing, then return a pointer to a const thing (this is, confusingly enough, not the same thing as returning a const pointer - check the language guides for help on this one).

But: There's (as someone pointed out) no reason to return a pointer - you could just return a char.

But: If you're going to be asking Yes or No, why is the AskYesOrNo() function returning (in a convoluted fashion) an 'X' or an 'O'?

But: If your function is meant to only return whether the user said "Yes" or "No", why allow it to (theoretically) return '3'? What would that mean to the calling function? So, you could fix this by doing something like:

enum BinaryAnswer
{
No,
Yes
};

BinaryAnswer SomeFunctionThatReturnsABinaryAnswer(void)
{
if (someConditionOfInput)
return(No);
else
return(Yes);
}


This clearly states your intent for the function, and there's no way for the function to return '3' or some other nonsense, unless you subsequently set out to really trick the compiler.

That being said, bool is a generally accepted shorthand for other binary answers, so you could write:

bool SomeFunctionThatReturnsABinaryAnswer(void)
{
if (someConditionOfInput)
return(false);
else
return(true);
}
and it's unlikely that you'd get whacked with the style stick (for that part, anyways).

Cheers,
Jason

Share this post


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

  • Advertisement