-
make him go away!
I am writing my rpg program, and when the program asks for the user's name, the program puts the most recently hit key in the space. I can't figure out how to keep it from doing this.
This function executes first.
PHP Code:
void initPlayers(){
human.initialize(20, 5, 8, 80, "Warrior");
wizard.initialize(30, 8, 5, 70, "Wizard");
ghost.initialize(50, 0, 15, 60, "Specter");
// Get player type choice from the user
system("cls");
printf("Choose which character to assume:\n");
printf("1) Warrior\n");
printf("2) Wizard\n");
printf("Choice: ");
switch (getche()){
case '1':
character = human;
break;
case '2':
character = wizard;
break;
default:
character = ghost;
break;
}
character.setName();
}
And this is the character.setName() that is called.
PHP Code:
void setName(){
printf("\nWhat is your name? ");
cin >> name;
}
-
Use kbhit()
Code:
.....
char key;
if (kbhit())
{
key = getch();
switch (key)
{
...
}
}
...
-
are you sure kbhit() and _sleep() work in dev?
-
And, my problem is what happens with the cin >> name; not the other one. If you choose '1' on trhe menu, then when it displays the "What is your name?" thing there is automatically a 1 at the beginning.
Code:
Choose which character to assume:
1) Warrior
2) Wizard
Choice: 1
What is your name? 1
-
yes, they're both standard ANSI functions
kbhit() is defined in conio.h
_sleep() is defined in stdlib.h
so you need to include those headers to use them
-
Quote:
Originally posted by Inquirer
And, my problem is what happens with the cin >> name; not the other one. If you choose '1' on trhe menu, then when it displays the "What is your name?" thing there is automatically a 1 at the beginning.
Code:
Choose which character to assume:
1) Warrior
2) Wizard
Choice: 1
What is your name? 1
that's because of the nature of getch().... use kbhit() to detect a keypress, then use getch() to get what key it is
-
Aaaah. I see what you were saying! And thanks for the headers.
~Inquirer
-
I'm very familiar with Dev-C++ (still have it on my system) and if you need any specific help, just ask
-
nope. Stll get an error.
Code:
74 players.h
implicit declaration of function `int kbhit(...)'
Do you think it would be too huge a bug just to leave in there?
~Inquirer
-
-
PHP Code:
class player {
public:
int attack(){return strength;}
int receive(int damage){
if (resistance < damage) {
health -= (damage - resistance);
}
if (health <= 0){
dead = true;
return 0;
} else {
return health;
}
}
int getHealth(){return health;}
int getResistance(){return resistance;}
int getStrength(){return strength;}
int getAccuracy(){return accuracy;}
bool isDead(){return dead;}
void getInfo();
void setName(){
//cin.get();
printf("\nWhat is your name? ");
cin >> name;
}
char* getName(){return name;}
void initialize(int h, int r, int s, int a, char* type){
health = h;
resistance = r;
strength = s;
accuracy = a;
species = type;
dead = false;
initialized = true;
}
private:
int health;
int resistance;
int strength;
int accuracy;
bool dead;
bool initialized;
char name[41];
char *species;
}human, wizard, ghost, character;
void player::getInfo(){
system("cls");
cout << "Character Profile:\n"
<< "Name: " << name << " the "
<< species << "\n"
<< "Health: " << health << "\n"
<< "Strength: " << strength << "\n"
<< "Accuracy: " << accuracy << "%\n"
<< "Resistance: " << resistance << "\n";
getche();
}
void initPlayers(){
human.initialize(20, 5, 8, 80, "Warrior");
wizard.initialize(30, 8, 5, 70, "Wizard");
ghost.initialize(50, 0, 15, 60, "Specter");
// Get player type choice from the user
system("cls");
printf("Choose which character to assume:\n");
printf("1) Warrior\n");
printf("2) Wizard\n");
printf("Choice: ");
switch (getche()){
case '1':
character = human;
break;
case '2':
character = wizard;
break;
default:
character = ghost;
break;
}
character.setName();
}
-
-
PHP Code:
char key;
if (kbhit()){
key = getch();
}
switch (key){
case '1':
character = human;
break;
case '2':
character = wizard;
break;
default:
character = ghost;
break;
}
Put that where the switch was, i forgot and put in the last working piece of code
-
PHP Code:
char key;
if (kbhit())
{
key = getch();
switch (key)
{
case '1':
{
character = human;
break;
}
case '2':
{
character = wizard;
break;
}
default:
{
character = ghost;
break;
}
}
}
Try that.... see the difference?
-
i still getthe following error:
Code:
76 players.h
implicit declaration of function `int kbhit(...)'
-
did for include <conio.h>?
-
conio.h is #included in mainHeaders.h, which is #included in players.h