Problem Putting INTs Into a CHAR Array

This is a discussion on Problem Putting INTs Into a CHAR Array within the C++ Programming forums, part of the General Programming Boards category; Hello, I want to make a char array of a deck of playing cards. I have a 2-d char array ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    21

    Problem Putting INTs Into a CHAR Array

    Hello,

    I want to make a char array of a deck of playing cards. I have a 2-d char array called deck[52][2]. where each object in the array has a card value(1-10,J,Q,K,A) and a suit value(S,C,D,H). I made this loop to put the values into the array however i cant get the 1-10 values in because it thinks these are ascii values. My question is how do i make it so that i can put these int values into the char array without having to put them in one by one? Thanks a bunch!

    heres my code:

    Code:
    for ( int i = 0; i == 51; i++ ) {
    		
    		if ( i < 13 ) {
    		deck[i][1] = 'H';
    		if (i == 0){
    		int temp = 2;
    		}		
    		if (temp < 11) {
    			deck[i][2] = temp;
    			temp ++;
    			}
    			else if (temp == 11){
    			deck[i][2] = 'J';
    			temp ++;
    			}
    			else if (temp == 12){
    			deck[i][2] = 'Q';
    			temp ++;
    			}
    			else if (temp == 13){
    			deck[i][2] = 'K';
    			temp ++;
    			}
    			else if (temp == 14){
    			deck[i][2] = 'A';
    			temp = 2;
    			}
    		}
    		if (i > 12 && i < 26) {
    		deck[i][1] = 'D';
    			if (temp < 11) {
    			deck[i][2] = temp;
    			temp ++;
    			}
    			else if (temp == 11){
    			deck[i][2] = 'J';
    			temp ++;
    			}
    			else if (temp == 12){
    			deck[i][2] = 'Q';
    			temp ++;
    			}
    			else if (temp == 13){
    			deck[i][2] = 'K';
    			temp ++;
    			}
    			else if (temp == 14){
    			deck[i][2] = 'A';
    			temp = 2;
    			}
    		}
    		if (i > 25 && i < 39) {
    		deck[i][1] = 'C';
    			if (temp < 11) {
    			deck[i][2] = temp;
    			temp ++;
    			}
    			else if (temp == 11){
    			deck[i][2] = 'J';
    			temp ++;
    			}
    			else if (temp == 12){
    			deck[i][2] = 'Q';
    			temp ++;
    			}
    			else if (temp == 13){
    			deck[i][2] = 'K';
    			temp ++;
    			}
    			else if (temp == 14){
    			deck[i][2] = 'A';
    			temp = 2;
    			}
    		}
    		if (i > 38 ) {
    		deck[i][1] = 'S';
    			if (temp < 11) {
    			deck[i][2] = temp;
    			temp ++;
    			}
    			else if (temp == 11){
    			deck[i][2] = 'J';
    			temp ++;
    			}
    			else if (temp == 12){
    			deck[i][2] = 'Q';
    			temp ++;
    			}
    			else if (temp == 13){
    			deck[i][2] = 'K';
    			temp ++;
    			}
    			else if (temp == 14){
    			deck[i][2] = 'A';
    			temp = 2;
    			}
    		}
    	
    	}
    Last edited by cram; 10-12-2004 at 05:23 PM.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think you might want to check out THIS thread
    • "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

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    Hi thanks for the reply, but i having trouble understanding that other thread because i dont know how that "case" stuff works, if possible, could you make it specific to my code would be Extremely helpful, thanks so much!

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Problem #1:
    Code:
    for ( int i = 0; i == 51; i++ ) {
    Your for loop will never execute. The middle statement (in this case i == 51) is the condition. As long as that condition evaluates to true, the loop is run, and when that condition evaluates to false, the loop stops. Since i starts at 0, i == 51 will not be true the first time through and the loop won't ever get run.

    Problem #2:
    Code:
    if (i == 0){
        int temp = 2;
    }
    When you define a variable inside of a block of code, that variable is only usable inside that block of code. Here, you define a new variable called temp that goes out of scope and is unusable immediately after the bracket that closes the if statement. If you have another variable called temp that you defined earlier in the code that you didn't post, and you want to set that value to 2, then get rid of the int part of the code above. Otherwise, move the definition to the block of code that you want it to be valid for.

    Problem #3: If you want to convert a single digit integer into a character, just do this:
    Code:
    if (temp >= 0 && temp < 10)
        deck[i][2] = temp + '0';
    This is a trick that uses the fact that the nine characters that represent decimal digits are in order in the ASCII character set. You will have to use 'T' for 10, though, because it is two characters long and you are only storing a single character.

    Hope that helps.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    okay thanks a lot for the reply, so in order to store 10 i would have to do this?

    Code:
    else if (temp == 10){
    deck[i][2] = T;
    temp ++;
    }

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    No, use 'T' instead of T. The first is a character, the second is a name that the compiler won't understand.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    hmm okay but does that mean when i go to cout that array, will it display a T or a 10?

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    It will display T. The reason you can't use 10 in this instance is because 10 is two characters. You are storing a single character for each card in your deck. You can use anything you want, as long as its a single character. I suggested T because it made more sense to me than any other character.

    Once you learn more C++, you can make your program more sophisticated so that it can display 10 instead of T. If you want or have to do that now, then you are going to have to come up with an alternative to storing a single character per card.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    yah thats what i have to do, any suggestions on how to do that? thanks a bunch

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    It really depends on what you know. My best suggestion is to work on it and figure it out. Since there are dozens of ways to do it, I'm sure you can find one that works.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    haha dont bet on it, thanks for the help though

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    let me try to use like this:
    Code:
    enum deck {J, Q , K , A} cardval;

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    a short example of switch with your code
    Code:
    switch (temp) 
    {
         case 1: (temp < 11); 
         deck[i][2] = temp;
          temp ++;
          break;
    
          case 2: (temp == 11);
          deck[i][2] = 'J';
           temp ++;
           break;
    
    }

    remember to put break; or every case after the one that's true will be executed

  14. #14
    Registered User
    Join Date
    Oct 2004
    Posts
    17
    Did a project once that modeled playing poker. It was around 3 or 4 yrs ago, which in college language means recycle your projects . The code for card classes and the deck of cards is posted on my professor's web site. Here's the link. Just look at the card and deck class so you can get an idea of another way to structure your program. Don't just copy it! Study it.

    Good luck! If you have to rate the hands at the end of a game, make sure to alot plenty of time and don't let it catch up w/ you. My procratination cost me many hours of sleep

    just2peachy

    Oh yeah, PS---code is in java
    Last edited by just2peachy; 10-13-2004 at 08:57 AM. Reason: forgot to mention!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  3. help with array of char and char **
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 03:23 PM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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