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;
}