-
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.
-
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;
}
}
-
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...
-
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];
}
}
}
-
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 :)
-
hope the draw class helped. if youve got any questions about how it works, give us a yell.
-
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.
-
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)
-
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++.