Help for data conversion error

This is a discussion on Help for data conversion error within the C++ Programming forums, part of the General Programming Boards category; Hi, I am learning to write some simple program. My program below should convert integer 13 to character 'K' but ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    18

    Help for data conversion error

    Hi, I am learning to write some simple program.

    My program below should convert integer 13 to character 'K' but there is nothing when I tried to write it to file.

    Nothing in card_array[P_1st_card-1]

    Why is it so?

    Could anyone can help?

    Thank you very much!

    Code:
    #include <iostream>
    #include <fstream>
    #include <ctime>
    #include <cstdlib>
    #include <cassert>
    
    
    using namespace std;
    
    
    int P_1st_card;
    int B_1st_card;
    int P_2nd_card;
    int B_2nd_card;
    int cardvalue;
    int player_value;
    int banker_value;
    
    
    char char_P_1st_card;
    char char_P_2nd_card;
    char char_B_1st_card;
    char char_B_2nd_card;
    char char_cardvalue;
    char card_array[13];
    
    
    //const char card_array[]="A23456789TJQK";
    
    
    char stringconversion(int);
    int numericconversion(int);
    
    
    int main()
    {
        ofstream mygame("c:\\cpp\\testing\\game_baccarat101.rtf");
        srand(time(0));
        for(int i=1;i!=20;i++)
        {
            P_1st_card=1+rand()%13;
            B_1st_card=1+rand()%13;
            P_2nd_card=1+rand()%13;
            B_2nd_card=1+rand()%13;
    
    
            stringconversion(P_1st_card);
            stringconversion(B_1st_card);
            stringconversion(P_2nd_card);
            stringconversion(B_2nd_card);
    
    
            numericconversion(P_1st_card);
            numericconversion(B_1st_card);
            numericconversion(P_2nd_card);
            numericconversion(B_2nd_card);
    
    
            int player_value=(P_1st_card+P_2nd_card)%10;
            int banker_value=(B_1st_card+B_2nd_card)%10;
    
    
            mygame<<" P1 = "<<P_1st_card<<" Char = "<<card_array[P_1st_card-1];
            mygame<<" P2 = "<<P_2nd_card<<" Char = "<<card_array[P_2nd_card-1];
            mygame<<" Player = "<<player_value<<endl;
            mygame<<" B1 = "<<B_1st_card<<" Char = "<<card_array[B_1st_card-1];
            mygame<<" B2 = "<<B_2nd_card<<" Char = "<<card_array[B_2nd_card-1];
            mygame<<" Banker = "<<banker_value<<endl;
        }
        return 0;
    mygame.close();
    }
    
    
    char stringconversion(int cardvalue)
    {
      const char card_array[]="A23456789TJQK";
      assert (cardvalue>=1 && cardvalue<=13);
      return card_array[cardvalue-1];
    }
    
    
      int numericconversion(int cardvalue)
      {
        if (cardvalue>=10)
            cardvalue=0;
        return cardvalue;
      }
    
    

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So when you convert that card into a char, maybe you shouldn't just throw it away, maybe?
    Code:
    character_you_want_to_print_later = stringconversion(P_1st_card);
    and then you print that character later.

    Right now you're using the global card_array, which has no characters in it all and thus you are printing no characters.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    Yes it work. Thank tabstop for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data type conversion issue
    By SDhiraj in forum C Programming
    Replies: 3
    Last Post: 07-02-2012, 07:41 AM
  2. Visual C++ data conversion 4 n00b
    By dcboy in forum Windows Programming
    Replies: 3
    Last Post: 05-07-2006, 04:24 AM
  3. Data conversion
    By vishalbhingarka in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2005, 04:36 PM
  4. Received data conversion
    By jmd15 in forum Networking/Device Communication
    Replies: 14
    Last Post: 08-06-2005, 08:37 AM
  5. Data conversion
    By abu in forum Windows Programming
    Replies: 5
    Last Post: 09-04-2004, 01:46 AM

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