• Advertisement
Sign in to follow this  

Asciis stored in integers?

This topic is 3645 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

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
Advertisement
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
Sign in to follow this  

  • Advertisement