Thread: Char to Int help

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    3

    Char to Int help

    I'm pretty new to C++, being in the freshman programming course. And I've searched through the FAQs and I couldn't find what I needed. So please bear with me.


    Basically, what I'm looking to do is convert a specific number on a character array to an integer. I'm working on a battle program, and I'm using "character cards" if you will. They look like this:

    Code:
    char BANDIT[#][#]={
    {" Name                                          |HP:0010       " }, (hp=100)
    {"              Graphic                          |MP:#####   "},
    {"               Graphic                         |POW:#####"},
    {"                 Graphic                       |INT:#####   "},
    {"              Graphic                          |HPP:#####  "},
    {"               Graphic                         |SPD:#####  "},
    {"                          Graphic              |EXP:#####  "},
    {"                                                    |CRED:#####"}};
    Before the battle I set a blank char array to whichever opponent will be in that battle. Call it blank.
    I then, during the battle, want to pull blank[#][#] and set it to an int. Say I want to get the enemy's Hitpoints. I want to pull the first number after Hp: and multiply it by 10,000, the second and multiply it by 1,000, the third by 100 and the fourth by 10, then add them to get the enemy's HP.

    However, I'm not sure of a way to do that. I get the name and graphic to show separate from the rest, but when I try to set the the numbers as an integer, it comes up with some absurd number that I have no idea where it came from. Again, I'm new to coding and inexperienced, so bear with me on this. Any help would be very much appreciated.

  2. #2
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    use a switch
    Code:
    int main()
    {
    	int foo;
    	char i;
    	switch ( i )
    		{
    		case '1' : foo = 1;
    		break;
    		case '2' : foo = 2;
    		break;
    		...
    		blah
    		blah
    		...
    		default : ; //(; means do nothing)
    		break;
    		}
    }
    Ph33r the sphericalCUBE

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    3
    However, if I were to do that, I'd need a LOT of cases.

    This is what I'm doing, more specifically:

    Code:
    char BANDIT[10][48]={
    {"Bandit                            |hp:0000      "},
    {"                                  |mp:4567      "},
    {"                                  |POW:50000    "},
    {"          ,-----,                 |HPP:50000    "},
    {"          | o *=|                 |WIS:30000    "},
    {"           \\ @-/                  |SPD:40000    "},
    {"         /' .  .'\\                |EXP:0005     "},
    {"        |  |__()  |               |CRD:0005     "},
    {"         \\___{||}_/               |ARM:40000    "},
    {"          |==&==!|                |ATKS:01114   "}};
    
    
    void combatmain()
    {
    	int enhp=0, enmp=0;
    	int enPOW=0, enHPP=0, enWIS=0, enSPD=0;
    	int encrd=0, enexp=0;
    	int enarm=0;
    	int eninit, init;
    	int turn=0;
    	//other vars
    	char ename[20];
    	int ppdmg=POW*10;
    	int mpdmg=WIS*20;
    	setenemy();
    	// Set enemy NAME
    	for(int col=0;col<19;col++)
    		{
    			ename[col]=blank[0][col];
    		}
    	//Set enemy hp, mp, exp and credits
    	int enhp1=blank[0][38]*10000;
    	int enhp2=blank[0][39]*1000;
    	int enhp3=blank[0][40]*100;
    	int enhp4=blank[0][41]*10;
    	enhp=enhp1+enhp2+enhp3+enhp4;
    	int enmp1=blank[1][38]*10000;
    	int enmp2=blank[1][39]*1000;
    	int enmp3=blank[1][40]*100;
    	int enmp4=blank[1][41]*10;
    	enmp=enmp1+enmp2+enmp3+enmp4;
    	enexp=(blank[7][38]*10000)+(blank[7][39]*1000)+(blank[7][40]*100)+(blank[7][41]*10);
    	encrd=(blank[8][38]*10000)+(blank[8][39]*1000)+(blank[8][40]*100)+(blank[8][41]*10);
    	enPOW=blank[3][39]+blank[3][40]+blank[3][41]+blank[3][42]+blank[3][43];
    	enHPP=blank[4][39]+blank[4][40]+blank[4][41]+blank[4][42]+blank[4][43];
    	enWIS=blank[5][39]+blank[5][40]+blank[5][41]+blank[5][42]+blank[5][43];
    	enSPD=blank[6][39]+blank[6][40]+blank[6][41]+blank[6][42]+blank[6][43];
    	enarm=blank[9][39]+blank[9][40]+blank[9][41]+blank[9][42]+blank[9][43];
    Or... Trying to do...

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    3
    Eh... I was going to edit to add this, but seeing how I cannot edit I'll just post it here.

    This is how I get blank[#][#]

    Code:
    //BANDIT
    	if (decide==1)
    	{
    		for(int row=0;row<10;row++)
    		{
    			for(int col=0;col<48;col++)
    			{
    				blank[row][col]=BANDIT[row][col];
    			}
    		}
    	}

  5. #5
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    dude, if you're going to be doing anything like that......

    use the Winblows Console API to reposition the cursor and type stuff at arbitrary locations. At the moment, i am making a console RPG, and i just debugged a friggin textbox class...... in any case, my draw class does boxes, cursor repositioning, text coloring..... no pain.

    Dont claim this code is your own, and dont change it, keep intact, dont use it to sacrifice a goat, etc. You know the drill.

    edit : btw, kudos to your ascii art
    Ph33r the sphericalCUBE

  6. #6
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    hope the draw class helped. if youve got any questions about how it works, give us a yell.
    Ph33r the sphericalCUBE

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    here is this again... it may be a bit overkill for what youre doing, but if youre going to be using ascii for a while (which, if you just started C++, is very possible) it may be fun to play with. its two classes, a display class and an animation class, that provides superfast drawing in console, giving smooth, nice looking ascii graphics...

    the zip also includes some example games that i wrote just to give you a rudimentary idea of the kind of things you can do. the only files you need to include in your project are display.h and .cpp and anim.h and .cpp

    there is a readme to tell you all you need to know.
    Last edited by ...; 11-11-2003 at 01:13 AM.
    I came up with a cool phrase to put down here, but i forgot it...

  8. #8
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    thanks! i like that.....
    maybe i should psot my GuiElement class later - dynamic textboxes, menus and stringboxes (think msn or the messge box in NWN or baldurs or sommat)
    Ph33r the sphericalCUBE

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    161
    Ouch. While this method might be a creative way of doing this, it is definitely not the easiest or most efficient.

    You should store the data in a structure or class. Then you write a function that accepts that structure as a parameter and from there, you print it onto the screen.

    You could use a struct like this:
    Code:
    struct Character
    {
      int hp;
      int mp;
      int pow;
      int int_;  // can't have a variable named int
      int hpp;
      int spd;
      int exp;
      int cred;
    };
    Then you can keep structures around with that information.
    Code:
    Character bandit;
    // ... others
    
    void setup_characters()
    {
      bandit.hp = 1000;
      bandit.mp = 4567;
      bandit.pow = 5000;
      // .. finish bandit setup and do others
    }
    And when you want to print out the "character cards"..
    Code:
    void print_character(Character& c)
    {
      // .. draw your cool ascii art 
      // reposition cursor and print stats
      printf("HP: %d", c.hp);
      // etc....
    }
    This is assuming of course that you're using C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM