Thread: make him go away!

  1. #1

    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;
          } 
    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
    are you sure kbhit() and _sleep() work in dev?

  4. #4
    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 08: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
    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
    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 08:40 PM.

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

  11. #11
    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 08:46 PM.

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

  13. #13
    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
    i still getthe following error:

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

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, 09: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, 04:19 AM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM