saving a bool variable and using it in an if statement

Nov 30, 2011 at 8:49pm
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?
Nov 30, 2011 at 9:11pm
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));
}
Nov 30, 2011 at 9:16pm
where do you go to get the code to post like that?
Nov 30, 2011 at 9:22pm
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 Nov 30, 2011 at 9:23pm
Nov 30, 2011 at 9:28pm
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?
Nov 30, 2011 at 9:36pm
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 Nov 30, 2011 at 9:37pm
Nov 30, 2011 at 9:42pm
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?
Nov 30, 2011 at 9:46pm
sorry i missed your string constructor example when i first looked
Nov 30, 2011 at 9:50pm
It sounds like you want next_char to be a string.
Nov 30, 2011 at 9:51pm
even if the ifstream is just a list of chars?
Nov 30, 2011 at 9:55pm
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);
Nov 30, 2011 at 10:04pm
okay thanks again.
Topic archived. No new replies allowed.