Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 _getch problem with unbuffered values
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
copthesaint




PostPosted: Thu Oct 04, 2012 4:24 pm   Post subject: _getch problem with unbuffered values

I am trying to use _getch in C++ using window 7 visual studio 2010. The issue I am having is when I press any character after the up or down key, every other character acts the same as the last pressed up or down. Here is my code:

c:

#include <iostream>
#include <string>
#include <stdio.h>
#include <conio.h>

using namespace std;

int mainMenu (void){

        /*menu dependand varibles*/
        bool terminate = 0;
        const int menuEvents = 4;
        const int menuEventChr [4] = {72,80,13,27}; // up, down, enter, esc
        int inputVal = 0;
        int menuOption = 0;
        char menuChar [5] = {'>',' ',' ',' ',' '};     
        int ch = 0;
       
        /*Main loop menu*/
        while ((terminate == 0) == true){
               
                system ("cls");
                system ("color 02");
                /*Menu Message*/
                cout << "ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM" << endl;
                cout << "COPYRIGHT 2075-2077 ROBCO INDUSTRIES" << endl;
                cout << "-Server 6-" << endl << endl;
                cout << "-Robco Unified System Terminal-" << endl;
                cout << "========================================" << endl;
                cout << "________________________________________" << endl << endl;
                cout << " " << menuChar [0] << " " << "User Login" << endl << endl;
                cout << " " << menuChar [1] << " " << "User Database" << endl << endl;
                cout << " " << menuChar [2] << " " << "Account Creation" << endl << endl;
                cout << " " << menuChar [3] << " " << "Registry List" << endl << endl;
                cout << " " << menuChar [4] << " " << "Terminate" << endl << endl;
               
                /*Checks for key events*/
                //inputVal = 0;
                ch = _getch ();
                if (ch == 0 || ch == 224){

                        ch = _getch ();
                        inputVal = 0;

                        for (int i = 0; i < menuEvents; i++){

                                if (ch == menuEventChr [i]){
                                        inputVal = i + 1;
                                }              
                        }
                }              
               
                /*Manages Key Events*/
                switch (inputVal){
                        default:
                                cout << ch<< endl;
                                break;
                        case 1:
                                menuOption = menuOption - 1;
                                cout << "up" << endl;
                                system ("pause");
                                break;
                        case 2:
                                menuOption = menuOption + 1;
                                cout << "down" << endl;
                                system ("pause");
                                break;
                        case 3:
                                cout << "enter" << endl;
                                system ("pause");
                                break;
                        case 4:
                                terminate = 1;
                                cout << "terminated" << endl;
                                system ("pause");
                                break;
                }       
                /*Update consol menu*/
                if (menuOption <0){

                        menuOption = 4;

                } else if (menuOption > 4){

                        menuOption = 0;

                }

                for (int i = 0; i < 5; i++){
                        menuChar [i] = ' ';
                }
                menuChar [menuOption] = '>';

        } // end of main loop

        return 0;
}

int main (int argc, char args[]){

        if (mainMenu () == 0) {


        }
        system ("pause");
        return 0;
}


Any help is appreciated. thanks.
Sponsor
Sponsor
Sponsor
sponsor
2goto1




PostPosted: Thu Oct 04, 2012 6:37 pm   Post subject: RE:_getch problem with unbuffered values

In your source code, under what condition are you setting a value for inputVal?
copthesaint




PostPosted: Thu Oct 04, 2012 8:32 pm   Post subject: Re: _getch problem with unbuffered values

2goto1 wrote:

In your source code, under what condition are you setting a value for inputVal?


I solved that issue moving inputVal = 0 after the switch statement, thanks for making me focus on that.
It was just something simple and stupid lol.

c:
/*Checks for key events*/
                //inputVal = 0;
                ch = _getch ();
                if (ch == 0 || ch == 224){

                        ch = _getch ();
                        inputVal = 0//<- this line is moved

                        for (int i = 0; i < menuEvents; i++){

                                if (ch == menuEventChr [i]){
                                        inputVal = i + 1;
                                }               
                        }
                }


My question now is "why wont my program terminate?" whenever I run my program the inputVal is only changed for the two arrow keys, It will not change to values 2 and 3.

EDIT: never mind, enter and esc don't seem to have pressed and release states.
QuantumPhysics




PostPosted: Mon Oct 08, 2012 1:05 am   Post subject: RE:_getch problem with unbuffered values

Are you asking why your program doesn't stop when you hit esc or are you asking why the runtime console won't close?
copthesaint




PostPosted: Mon Oct 08, 2012 6:27 pm   Post subject: RE:_getch problem with unbuffered values

No I am not, I already solved this thread.

"EDIT: never mind, enter and esc don't seem to have pressed and release states."
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: