Bit O' Help Please

This is a discussion on Bit O' Help Please within the C++ Programming forums, part of the General Programming Boards category; I am just trying to draw a simple card to dos console.. I am following this tutorial.. and for some ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Bit O' Help Please

    I am just trying to draw a simple card to dos console.. I am following this tutorial.. and for some reason, this small program is yielding over 102 compilation errors... compiler doesn't seem to like the #define's but they are written just like the tutorial.

    Code:
    #include<iostream>
    #include<cstdlib>
    #include<iomanip>
    #include<cstring>
    #include<windows.h>
    using namespace std;
    
    #define WHITE   BACKGROUND_RED		| \ 
                    BACKGROUND_GREEN	| \ 
                    BACKGROUND_BLUE		| \
                    BACKGROUND_INTENSITY
    
    #define RED	FOREGROUND_RED | FOREGROUND_INTENSITY	
    #define BLACK   0
    
    
    typedef struct card
    {
    	unsigned int face_value;
    	char suit;
    	card *next;
    
    	card(){next=NULL;}
    
    }*mycard;
    
    
    int main(void)
    {
    	HANDLE hOut;
    
    	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	card new_card = new card;
    
    	srand(GetTickCount());	
    
    	new_card->face_value = static_cast<unsigned>rand()%9+2;
    
    	new_card->suit = static_cast<char>rand()%4+3;
    
    	SetConsoleTextAttribute(hOut, new_card->suit<2?WHITE|RED:WHITE|BLACK);
    
    	cout     << new_card->face_value  << new_card->suit << "  \n"
    		 << "   \n" 
    		 << " " << new_card->suit << " \n"
    		 << "   \n" 
    		 << " " << new_card->suit << new_card->face_value;
    
    	return 0;
    
    }
    Last edited by The Brain; 05-24-2005 at 11:14 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,390
    You should not have any spaces after the backslash.
    Probably should write like:
    Code:
    #define WHITE   (BACKGROUND_RED		| \
                    BACKGROUND_GREEN	| \
                    BACKGROUND_BLUE		| \
                    BACKGROUND_INTENSITY)
    
    #define RED	(FOREGROUND_RED | FOREGROUND_INTENSITY)
    #define BLACK   0
    I'm not sure if it is legal to use
    typedef struct name {}*;
    But then if it works, it works.

    I think it should be:
    card* new_card = new card;
    Though maybe your typedef takes care of that, if it works.

    Also, your casts arent written correctly.
    For example:
    static_cast<unsigned>(rand()%9+2)
    not
    static_cast<unsigned>rand()%9+2
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Awesome help from the c++ witch


    Code:
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<windows.h>
    using namespace std;
    
    #define WHITE BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_INTENSITY
    #define RED   FOREGROUND_RED|FOREGROUND_INTENSITY	
    #define BLACK 0
    
    
    typedef struct card
    {
    	unsigned int face_value;
    	char suit;
    	card *next;
    
    	card(){next=NULL;}
    
    }*mycard;
    
    
    int main(void)
    {
    	HANDLE hOut;
    
    	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	mycard new_card = new card;
    
    	srand(GetTickCount());	
    
    	new_card->face_value = static_cast<unsigned>(rand()%9+2);
    
    	new_card->suit = static_cast<char>(rand()%4+3);
    
    	SetConsoleTextAttribute(hOut, new_card->suit<5?WHITE|RED:WHITE|BLACK);
    
    	cout     << new_card->face_value    << new_card->suit << "   \n"
    		 << "     \n" 
    		 << "  "  << new_card->suit << "  \n"
    		 << "     \n" 
    		 << "   " << new_card->suit << new_card->face_value;
    
    	return 0;
    
    }

    Although I couldn't figure out why the #defines where whacked.. I just put everything on a single line. Compile and run.. you'll get a descent looking playing card
    Last edited by The Brain; 05-24-2005 at 02:53 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    You have a space problem in your first #define. And, when I run it, the white background of the card extends all the way to the right of the console window.

    It's still pretty neat.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    The board keeps adding that space in my first #define (the typed version looks ok before I post, but after I post the board adds the space for some reason)

    Here is what I get when I run the program. Spacing problems will occur right now when the face value is a double digit but I can handle that as a seperate case:
    Attached Images Attached Images  
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit manipulation
    By Neo1 in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2008, 11:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Bitwise Operators - Setting and Retreiving a Bit
    By Spono in forum C Programming
    Replies: 2
    Last Post: 11-04-2003, 01:09 PM
  5. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21