Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

char & int

Hello,

I am trying to write a simple card game,
which requires the user to input a playing card choice.
I wanted the user to be able to input e.g. 5H for the 5 of hearts

How may i program for input of a char and an int together so the user
does not have to answer in a drawn out way like selecting the suit then the
value.

Grateful for any help;
Dave
[399 byte] By [spursfc] at [2007-11-11 10:07:07]
# 1 Re: char & int
Hi,
enter them as strings 2S,3S,...,AS,2H,...AH,2D,... .
You can then get each character as s[0] and s[1].

something like:

#include <string>
#include <vector>
#include <algorithm>

vector<string> allCards;
allCards.push_back("2H");
allCards.push_back("3H");
// ... do this for all the cards in the deck
// allCards[0] equals to "2H" the two of hearts, allCards[1] equals to "3H", and so on
cout << allCards[0][0] << endl; // prints out '2'
cout << allCards[0][1] << endl; // prints out 'H'
cout << allCards[1][0] << endl; // prints out '3'
cout << allCards[1][1] << endl; // prints out 'H'

// you might want to use the std::random_shuffle - algorithm
random_shuffle(allCards.begin(),allCards.end());
// allCards[0] is now a random card of your deck
cout << allCards[0][0] << endl; // prints out the denomination of the random card
cout << allCards[0][1] << endl; // prints out the colour of the random card

Hope it helps to get you kick-started, although I don't condone gambling ;-)

Cheers,
D
drkybelk at 2007-11-11 20:59:26 >
# 2 Re: char & int
standard ascii has symbols for the 4 suits that you might find useful if this is a text game. The user cannot easily enter them but you can easily display them in the game.
jonnin at 2007-11-11 21:00:22 >
# 3 Re: char & int
Hopefully with the string approach you will now use JQKA for the ace jack etc.
jonnin at 2007-11-11 21:01:32 >
# 4 Re: char & int
Hello,
[...]
How may i program for input of a char and an int together so the user
does not have to answer in a drawn out way like selecting the suit then the
value.

I think in a simple text game, the following manner of entering value and suit in a single line is acceptable:
int value;
char suit;

scanf("%i%c", &value, &suit);
If user enters "5H", then value will be 5 and suit will be 'H'.

I hope this helps.
Viorel at 2007-11-11 21:02:26 >
# 5 Re: char & int
Hi,
the only criticism I have with functions like like scanf()/printf()/... is, that they
are not typesave and hence inherently a source of difficult to find bugs.
I think the the STL alternatives are far cleaner and I would recommend to any
new starter to skip the C-legacy I/O until she has a firmer grip on C++.
Yes, they might come in handy (particularly when analysing older code), but
don't use them in newer code.
The Boost-library has a type-safe version of C-style string formatting, but I haven't
tested it, so I don't know whether it's any good.
http://www.boost.org/libs/format/index.html

Cheers,
D
drkybelk at 2007-11-11 21:03:30 >
# 6 Re: char & int
I personally find scanf inappropriate for C++ forum, especially when talking about "simple text game" where efficiency is not the issue. So, we need some planing here (read: design). Altough I never wrote any text game, I can see some specific design solutions which may not be the best ones but I presume are better than none.

First thing you could do is to write simple Card class that deals with input, output and common set/get functions. Then you could write the Game class which will run the game (rule the players) and evaluate the hole game.

Of course, the code in attachment is far from complete, but it partialy resembles OO way of thinking. If the game you're making is more complex, you could introduce some other classes like Dealer, Player or such (whatever occurs in real game). Then you simply compose objects of these classes and create new games (well, things aren't always simple as that, actually, but as you learn more and more, you just might find out about e.g. Design Patterns or some other design methodologies).

(From reason unknown to me, my original post was reject with following message:

"You have included too many images in your signature or in your previous post. Please go back and correct the problem and then continue again.
Images include use of smilies, the vB code tag and HTML tags. The use of these is all subject to them being enabled by the administrator. "

so I put code in attachment with original message)
dcwexter at 2007-11-11 21:04:25 >
# 7 Re: char & int
okey this will work simply too and no afraid from it :

/*C++ pure*/
int i; char c;
cout<<"Enter your input: "; //for example 5H
cin >> i >> c ; //here i=5 and c=H

because i is an int , once you change from the range of integers , and start input a char like "H" the input for i stops and start input in c .
it's the same for "scanf()" only scanf is for C and cout for the new c++ i/o stream .
Amahdy at 2007-11-11 21:05:34 >
# 8 Re: char & int
that was my point, should be
char card,suit
cin >> card >> suit; //now, card can be jqka for ace king etc.
jonnin at 2007-11-11 21:06:32 >
# 9 Re: char & int
I have never used scanf but I assert that printf formatting is 100 times better than cout formatting. It is impossible to read cout formatting "at a glance" and its a step backwards in almost every way.
jonnin at 2007-11-11 21:07:30 >
# 10 Re: char & int
scanf() is danegours as it doesn't perform any type safety checks. I would simply read the card code as a string and validated it (quick and dirty) or overload the I/O operators for a card class, as others have suggested.
Danny at 2007-11-11 21:08:36 >
# 11 Re: char & int
that was my point, should be
char card,suit
cin >> card >> suit; //now, card can be jqka for ace king etc.

yea jonnin this is better if u want to support the jqka -wanted I think- , using the fact that char can hold one character only then once it recives the first "number" or "jqka" , the next inpu will be stored in "suit" and any other input will be ignored .. the only way u need is to convert "card" to the proper integer if it's not one of the jqka , u can make this by many methods say i = card - '0'; ... if i isn't in the range from 2 -> 9 then it must be one of jqka and that's all .
Amahdy at 2007-11-11 21:09:34 >
# 12 Re: char & int
If you need controlled input, you could check on one of my previous posts also.

previous thread that could appear relevant ( http://forums.dev-archive.com/showthread.php?t=153925)
dcwexter at 2007-11-11 21:10:33 >
# 13 Re: char & int
If you need controlled input, you could check on one of my previous posts also.

previous thread that could appear relevant[/URL]

lols u had a very good discussion there :)
He want to get 4 numbers only , so cin.getline(str, 4) is enough I think ..
or maybe some thing like this :
cin>>ch[0]>>ch[1]>>ch[2]>>ch[3]
what will be better with :
for(int i=0; i<4; ) ch[i++] = getche();
//or maybe : for(int i=0; i<4; ch[i++] = getche()); // !!!

and when I want to check for the input char to be in the range I must integrate your isValidAnswerChar() and here I'll be back to your code !

just only wanted to demonstrate another methods :WAVE:
Amahdy at 2007-11-11 21:11:34 >