saving a bool variable and using it in an if statement

const bool IS_CARD_VALUE=(next_char=='a'||next_char=='A'||next_char=='j'||next_char=='J'
||next_char=='k'||next_char=='K'||next_char=='Q'||next_char=='q'
||isdigit(next_char))

void read_player1_hand(ifstream& in_stream)
{
char next_char
while(!in_stream.eof()
{
in_stream.get(next_char);
if (IS_CARD_VALUE)



// if the next_char is 'a' this will evaluate as true? is this how you save a bool expression?
IS_CARD_VALUE is a bool constant, not an expression.

make a function instead
1
2
3
4
5
6
bool is_card_value(char next_char)
{
	return (next_char=='a'||next_char=='A'||next_char=='j'
	       ||next_char=='J'||next_char=='k'||next_char=='K'
	       ||next_char=='Q'||next_char=='q'||isdigit(next_char));
}
where do you go to get the code to post like that?
I put code tags around the code [code] [/code]. If you don't want to write them manually you can just select the code and press the <> button to the right.
Last edited on
ok thanks
1
2
3
4
5
6
7
8
9
10
11
 
bool is_card_value(char next_char)
 for(int i=0;i<NUMBER_OF_CARD_VALUES;i++)
           {
                if(static_cast<sting>(next_char))==CARD_VALUES[i])
                  {
                       return(true);
                  }
                
           }       
        return(false)


CARD_VALUES[] is a string array holding all possible values and next_char comes from an ifstream would this work to check if the incoming character is a possible card value?
Don't use static_cast to convert a char to a string. Use proper string constructor instead.
string(1, next_char)

If CARD_VALUES only contains strings of length 1 why not make CARD_VALUES an array of char?

If what you have written there is a function definition you are missing the { and } around the function body
Last edited on
it could contain two characters for values like ten and i'm reading in from a file I have no control over. so the ifstream comes in as characters then i have to convert each to a string to use the easy comparison operators(i.e. ==) that can't be used with c-strings. i did miss the brackets thanks.

what other way can I use to convert them into a string?
sorry i missed your string constructor example when i first looked
It sounds like you want next_char to be a string.
even if the ifstream is just a list of chars?
ifstream is a file input stream. You can read int, double, char, string and other types from it.

To read a word
1
2
string word;
in_stream >> word;


to read a line
1
2
string line;
getline(in_stream, line);
okay thanks again.
Topic archived. No new replies allowed.