Sign in to follow this  
Chrono1081

Asciis stored in integers?

Recommended Posts

Hello, Ive been out of the programming loop for awhile and im just getting back in, doing some projects but this one has me stumped! Its for a blackjack game and I have this function: void card(int x, int y) { cout<<" --- "<<endl; cout<<"|"<<(char)4<<" |"<<endl; cout<<"| "<<y<<" |"<<endl; cout<<"| "<<(char)4<<"|"<<endl; cout<<" --- "<<endl; } Which works fine, it makes a card, however when I try to do this: void card(int x, int y) { cout<<" --- "<<endl; cout<<"|"<<(char)x<<" |"<<endl; cout<<"| "<<y<<" |"<<endl; cout<<"| "<<(char)x<<"|"<<endl; cout<<" --- "<<endl; } (Notice how I replaced the 4's next to the (char) with x's) all I get is an empty space. I need this to be a variable to randomly generate the card. Anyone have any idea how to get these to display the little diamonds, hears, clubs and spades their supposed to? Any help would be greatly appreciated. Thank you for your time.

Share this post


Link to post
Share on other sites
Appreciate you are just coming back into all this, and don't want to get overcomplicated, but something you can do to help prevent errors like the one you just had, and make some more readable code:


#include <iostream>

enum suit { heart=3,diamond=4,club=5,spade=6 };

std::ostream &operator<<(std::ostream &os,suit s)
{
return os << char(s);
}

void f(int v,suit s)
{
std::cout << v << s << std::endl;
}

int main()
{
std::cout << diamond << std::endl;

f(3,heart);
f(10,club);
}




I've created an enum called suit, with each value set to the ascii value of the relevant symbol. I've then written an operator<< overload for std::ostream that takes a suit and uses the method you already employ to print the ascii character.

Hope this sparks some interest. Just a thought really.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this