Thread: GetAsyncKeyState not working after using cin

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    19

    GetAsyncKeyState not working after using cin

    Hi,

    I have a menu option that the user will toggle through after the user enters some information however, when I get to the menu option I think that GetAsyncKeyState(VK_RETURN) is non zero once the program reaches my function - which makes it skip past the menu option screen entirely

    Header file:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    #include <ctime>
    #include <time.h>
    #include <windows.h>
    #include <conio.h>
    #include <limits>
    using namespace std;
    enum scroll
    {
     CALORIES, EXERCISE, STEPS, STATS, EDIT, ORGANIZE, EXIT, NEXTDAY
    };
    enum Gend
    {
     NONE, MALE, FEMALE
    };
    class GetStartingInfo
    {
    private:
     string Name;
     double Age;
     double Height;
     double Weight;
     double GoalWeight;
     double BMR;
     double DailyDefecitToMeetGoal;
     int Sex;
     int Days;
    public:
     string GetName();
     void GetSex();
     void SetName();
     double GetHeight();
     void SetHeight();
     double GetAge();
     void SetAge();
     double GetWeight();
     void SetWeight();
     double GetGoalWeight();
     void SetGoalWeight();
     int GetDaysToFinish();
     void SetDaysToFinish();
     void SetBMR();
     void SetDailyCaloriesToMeetGoal();
     void WriteToFile();
     GetStartingInfo();
    };
    class Menu
    {
    private:
     int option;
     int pointer;
     string DisplayMenu[8];
    public:
     void SetMenuOptions();
     void PrintMenu();
     void WrapMenu();
     int ReturnedOption();
    };
    definitions:
    Code:
    #include "header.h"
    
    string GetStartingInfo::GetName()
    {
     string name;
        cout << "Enter your name:";
     getline(cin, name);
     system("cls");
        return name;
    }
    void GetStartingInfo::SetName()
    {
     Name = GetName();
    }
    void GetStartingInfo::GetSex()
    {
     int sexPointer = 0;
     string sex[2] = { "Male", "Female" };
     while (!GetAsyncKeyState(VK_RETURN))
     {
      system("cls");
      cout << endl;
      cout << "Choose your sex:\n\n";
      for (int i = 0; i < 2; i++)
      {
       if (i == sexPointer)
       {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 115);
        cout << sex[i] << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
       }
       else
       {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
        cout << sex[i] << endl;
       }
      }
      if (GetAsyncKeyState(VK_UP) != 0)
      {
       sexPointer -= 1;
       if (sexPointer == -1)
        sexPointer = 1;
    
      }
      else if (GetAsyncKeyState(VK_DOWN) != 0)
      {
       sexPointer += 1;
       if (sexPointer == 2)
        sexPointer = 0;
      }
      else if (GetAsyncKeyState(VK_RETURN) != 0)
      {
       switch (sexPointer)
       {
       case 0:
       {
        Sex = MALE;
        return;
       }break;
       case 1:
       {
        Sex = FEMALE;;
        return;
       }break;
      
       }
      }
      _sleep(200);
     }
     system("cls");
     cin.ignore();
    }
    double GetStartingInfo::GetHeight()
    {
     double height;
     cout << "Enter your height(inches):";
     cin >> height;
     system("cls");
     return height;
    }
    void GetStartingInfo::SetHeight()
    {
     Height = GetHeight();
    }
    double GetStartingInfo::GetAge()
    {
     double age;
     cout << "Enter your age:";
     cin >> age;
     system("cls");
     return age;
    }
    void GetStartingInfo::SetAge()
    {
     Age = GetAge();
    }
    double GetStartingInfo::GetWeight()
    {
     double weight;
     cout << "Enter your weight:";
     cin >> weight;
     system("cls");
     return weight;
    }
    void GetStartingInfo::SetWeight()
    {
     Weight = GetWeight();
    }
    double GetStartingInfo::GetGoalWeight()
    {
     double goal;
     cout << "Enter your goal weight:";
     cin >> goal;
     system("cls");
     return goal;
    }
    int GetStartingInfo::GetDaysToFinish()
    {
     int target;
     cout << "How many days do you need to get to your target weight?";
     cin >> target;
     system("cls");
     return target;
    }
    void GetStartingInfo::SetDaysToFinish()
    {
     Days = GetDaysToFinish();
    }
    void GetStartingInfo::SetGoalWeight()
    {
     GoalWeight = GetGoalWeight();
    }
    void GetStartingInfo::SetBMR()
    {
     if (Sex == MALE)
        BMR = 66.47 + (13.7 * (Weight * 0.453592)) + (5 * (Height * 2.54)) - (6.8 * Age);
     else if(Sex == FEMALE)
      BMR = 655.1 + (9.6 * (Weight * 0.453592)) + (1.8 * (Height * 2.54)) - (4.7 * Age);
    }
    void GetStartingInfo::SetDailyCaloriesToMeetGoal()
    {
     DailyDefecitToMeetGoal = ((Weight - GoalWeight) / (Days)) * 3500;
    }
    void GetStartingInfo::WriteToFile()
    {
     ofstream file("userprofile.txt");
     file << Name << "," << Age << "," <<  Height << "," << Weight << "," << GoalWeight << "," << BMR << "," << DailyDefecitToMeetGoal << "," << Sex << "," << Days << endl;
     file.close();
    }
    GetStartingInfo::GetStartingInfo()
    {
     GetSex();
     SetName();
     SetHeight();
     SetAge();
     SetWeight();
     SetGoalWeight();
     SetDaysToFinish();
     SetBMR();
     SetDailyCaloriesToMeetGoal();
     WriteToFile();
    }
    void Menu::SetMenuOptions()
    {
     DisplayMenu[0] = "Log Calories";
     DisplayMenu[1] = "Log an Exercise";
     DisplayMenu[2] = "Log Steps";
     DisplayMenu[3] = "View Stats";
     DisplayMenu[4] = "Edit Profile";
     DisplayMenu[5] = "Organize";
     DisplayMenu[6] = "Exit";
     DisplayMenu[7] = "Next Day";
    }
    void Menu::PrintMenu()
    {
     pointer = 0;
     FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
     while (!GetAsyncKeyState(VK_DOWN))
     {
      system("cls");
      cout << endl;
      cout << "Main Menu\n\n";
      for (int i = 0; i < 8; i++)
      {
       if (i == pointer)
       {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 115);
        cout << DisplayMenu[i] << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
       }
       else
       {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
        cout << DisplayMenu[i] << endl;
       }
      }
      if (GetAsyncKeyState(VK_UP) != 0)
      {
       pointer -= 1;
       if (pointer == -1)
        pointer = 7;
    
      }
      else if (GetAsyncKeyState(VK_DOWN) != 0)
      {
       pointer += 1;
       if (pointer == 8)
        pointer = 0;
      }
      else if (GetAsyncKeyState(VK_RETURN) != 0)
      {
       switch (pointer)
       {
       case 0: 
       {
        option = CALORIES;
        return;
       }break;
       case 1:
       {
        option = EXERCISE;
        return;
       }break;
       case 2:
       {
        option = STEPS;
        return;
       }break;
       case 3:
       {
        option = STATS;
        return;
       }break;
       case 4:
       {
        option = EDIT;
        return;
       }break;
       case 5:
       {
        option = ORGANIZE;
        return;
       }break;
       case 6:
       {
        option = EXIT;
        return;
       }break;
       case 7:
       {
        option = NEXTDAY;
        return;
       }break;
      }
     }
      _sleep(200);
     }
    }
    void Menu::WrapMenu()
    {
     SetMenuOptions();
     PrintMenu();
     ReturnedOption();
     
    }
    int Menu::ReturnedOption()
    {
     return option;
    }
    main:
    Code:
    #include "header.h"
    int main(void)
    {
     Menu menu;
     GetStartingInfo Info;
    
     menu.WrapMenu();
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2017
    Posts
    19
    It actually prints the menu but when I attempt to toggle through the list - the program is already at the end and closes

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    19
    I also got rid of FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HAN DLE)); in the PrintMenu() function and changed the while loop to while(true) - it still does not work

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    19
    OK - I found a solution to my problem however, I think this is only a band - aid and I still haven't figured out why the program is responding the way it is. For the PrintMenu function, I make the function skip the switch statement during the first iteration. This somehow allows me to proceed with the program:
    Code:
    void Menu::PrintMenu()
    {
     pointer = 0;
     int i = 0;
     while (true)
     {
      system("cls");
      cout << endl;
      cout << "Main Menu\n\n";
      for (int i = 0; i < 8; i++)
      {
       if (i == pointer)
       {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 115);
        cout << DisplayMenu[i] << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
       }
       else
       {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
        cout << DisplayMenu[i] << endl;
       }
      }
      if (GetAsyncKeyState(VK_UP) != 0)
      {
       pointer -= 1;
       if (pointer == -1)
        pointer = 7;
    
      }
      else if (GetAsyncKeyState(VK_DOWN) != 0)
      {
       pointer += 1;
       if (pointer == 8)
        pointer = 0;
      }
      else if (GetAsyncKeyState(VK_RETURN) != 0)
      {
       /*Function must loop at least once. This is because the call for GetAsyncKeyState(VK_RETURN) returns a non zero value during the first iteration. Not sure why but
       this is the quick fix solution*/
       if (i > 0)
       {
        switch (pointer)
        {
        case 0:
        {
         option = CALORIES;
         return;
        }break;
        case 1:
        {
         option = EXERCISE;
         return;
        }break;
        case 2:
        {
         option = STEPS;
         return;
        }break;
        case 3:
        {
         option = STATS;
         return;
        }break;
        case 4:
        {
         option = EDIT;
         return;
        }break;
        case 5:
        {
         option = ORGANIZE;
         return;
        }break;
        case 6:
        {
         option = EXIT;
         return;
        }break;
        case 7:
        {
         option = NEXTDAY;
         return;
        }break;
        }
       }
     }
      _sleep(200);
      i++;
     }
    }

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    In order to determine if the key is pressed or not, you need to do this:
    Code:
    if (GetAsyncKeyState(VK_DOWN) & 0x8000)
    GetAsyncKeyState function (Windows)
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetAsyncKeyState for Mac?
    By Treeham in forum C++ Programming
    Replies: 1
    Last Post: 08-29-2010, 08:38 PM
  2. GetAsyncKeyState.
    By kevinawad in forum Windows Programming
    Replies: 9
    Last Post: 11-09-2008, 05:02 PM
  3. GetAsyncKeyState( ); Not Working!
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 06-13-2005, 01:13 PM
  4. GetAsyncKeyState - if or while?
    By johnnyd in forum C Programming
    Replies: 6
    Last Post: 03-11-2002, 10:29 AM

Tags for this Thread