make him go away!

This is a discussion on make him go away! within the Game Programming forums, part of the General Programming Boards category; I am writing my rpg program, and when the program asks for the user's name, the program puts the most ...

  1. #1
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281

    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(205880"Warrior");
         
    wizard.initialize(308570"Wizard");
         
    ghost.initialize(5001560"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;
          } 
    Attached Files Attached Files
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Use kbhit()
    Code:
    .....
    
    char key;
    if (kbhit())
    {
    key = getch();
    switch (key)
    {
    ...
    }
    }
    
    ...

  3. #3
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    are you sure kbhit() and _sleep() work in dev?

  4. #4
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    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

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    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
    Last edited by d00b; 07-29-2002 at 09:26 PM.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    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

  7. #7
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Aaaah. I see what you were saying! And thanks for the headers.

    ~Inquirer

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    I'm very familiar with Dev-C++ (still have it on my system) and if you need any specific help, just ask

  9. #9
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    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
    Last edited by Inquirer; 07-29-2002 at 09:40 PM.

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    show me the code

  11. #11
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    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;
          }
          
    chargetName(){return name;}

          
    void initialize(int hint rint sint achartype){
             
    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;
    }
    humanwizardghostcharacter;

    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(205880"Warrior");
         
    wizard.initialize(308570"Wizard");
         
    ghost.initialize(5001560"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();

    Last edited by Inquirer; 07-29-2002 at 09:46 PM.

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    I don't see kbhit()

  13. #13
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    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

  14. #14
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    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?

  15. #15
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    i still getthe following error:

    Code:
    76 players.h
     implicit declaration of function `int kbhit(...)'

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Establishing 'make clean' with GNU make
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 04-11-2009, 10:10 AM
  2. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 05:19 AM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 02:05 PM
  5. Replies: 6
    Last Post: 04-20-2002, 07:35 PM

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