Switch cases and getch() don't work together - c++

I've done a pretty simple switch where if I press a character, the integer of it adds one to itself and prints a message, it worked pretty fine with the "cin>>" and the "getchar()", but when I added the getch, It still prints the message but it stops updating the integer "how I know"? cause I made a random value that the integer has to get to, and it's between 1 and 20, and with the getch it simply goes on without stopping itself when arrived to the value
'''
void Journ(char const *bypassto = "x")
{
int m = 4;
int VAL[m];
for (int j=0; j<m; j++)
{
VAL[j] = rand()%20 + 1;
}
char comm;
int W;
int A;
int S;
int D;
char journkey;
if (bypassto != "x" ) {
journkey = *bypassto;
} else {
cout << "Choose a direction: \n";
cout <<"[FOWARD]-[LEFT]-[RIGHT]-[BACKWARDS]\n";
cout << "w for foward - a for left - d for right - s for backwards \n";
}
while (comm!=4)
{
// cin >> comm;
comm = getch();
switch(comm)
{
case 'w':
{
cout << "You went foward. \n";
W++;
break;
}
case 'a':
{
cout << "You went left. \n";
A++;
break;
}
case 's':
{
cout << "You went backwards. \n";
S++;
break;
}
case 'd':
{
cout << "You went right. \n";
D++;
break;
}
}
if(W == VAL[0])
{
BattleIntro();
break;
}
else if(A == VAL[1])
{
BattleIntro();
break;
}
else if(S == VAL[2])
{
BattleIntro();
break;
}
else if(D == VAL[3])
{
BattleIntro();
break;
}
}
}
'''

Related

Graphics Menu in c++

I am working on a project and I want to make a Graphics Menu. Issue which I am facing is that after it shows any text written in the function I've put in switch. it goes back to main menu. I want to make a function which stays on new function and once it directs to new function, It has nothing to do with main menu anymore. Until this function is called again.
I want it to be a simple menu function which directs me to function. Nothing else.
your help would mean allot!
Thanks in advance.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<iomanip>
#include<fstream>
#include<windows.h>
using namespace std;
int main() {
system("cls");
string Menu[3] = { " Admin", " Customer", " Exit" };
int pointer = 0;
bool flag=true;
while (flag==true)
{
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout << "Main Menu\n\n";
for (int i = 0; i < 3; ++i)
{
if (i == pointer)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout << Menu[i] << endl;
}
else
{
SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE), 15);
cout << Menu[i] << endl;
}
}
while (true)
{
if (GetAsyncKeyState(VK_UP) != 0)
{
pointer =pointer-1;
if (pointer == -1)
{
pointer = 2;
}
break;
}
else if (GetAsyncKeyState(VK_DOWN) != 0)
{
pointer += 1;
if (pointer == 3)
{
pointer = 0;
}
break;
}
else if (GetAsyncKeyState(VK_RETURN) != 0)
{
switch (pointer)
{
case 0:
{
admin_login();
Sleep(500);
break;
}
case 1:
{
customer_sign();
Sleep(500);
break;
}
case 2:
{
thank_you();
Sleep(800);
break;
}
default:
{
cout<<"Invalid Input! ";
}
}
}
}
Sleep(150);
}
return 0;
}
Your 'flag' boolean needs to be checked as the condition for both while loops, not just the first one. You also need to clear the console when you press enter on a menu selection.
Inside your switch case after detecting key press, in each case you need to set your flag boolean to false so you stop drawing the main menu, and instead start drawing the sub menus. I don't have access to your other functions so here is a solution emulating that behaviour in a simplified and minimal reproducible proof of concept:
#include<windows.h>
#include <iostream>
using namespace std;
int main()
{
system("cls");
string Menu[3] = { " Admin", " Customer", " Exit" };
int pointer = 0;
bool bMainMenu = true;
while (bMainMenu)
{
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout << "Main Menu\n\n";
for (int i = 0; i < 3; ++i)
{
if (i == pointer)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout << Menu[i] << endl;
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout << Menu[i] << endl;
}
}
while (bMainMenu)
{
if (GetAsyncKeyState(VK_UP)&1)
{
pointer = pointer - 1;
if (pointer == -1)
{
pointer = 2;
}
break;
}
else if (GetAsyncKeyState(VK_DOWN)&1)
{
pointer += 1;
if (pointer == 3)
{
pointer = 0;
}
break;
}
else if (GetAsyncKeyState(VK_RETURN)&1)
{
switch (pointer)
{
case 0:
{
//admin_login();
system("cls");
std::cout << "admin selected\n";
Sleep(1000);
bMainMenu = false;
break;
}
case 1:
{
//customer_sign();
system("cls");
std::cout << "customer selected\n";
Sleep(1000);
bMainMenu = false;
break;
}
case 2:
{
//thank_you();
system("cls");
std::cout << "thank you selected\n";
Sleep(1000);
bMainMenu = false;
break;
}
default:
{
cout << "Invalid Input! ";
}
}
}
}
Sleep(150);
}
return 0;
}

C++ with menu system: How to eliminate input buffers when pressing arrow keys

I was able to make a program that has a menu on it. At first, the program is running smoothly. The problem starts to appear when, at a certain point in the program, the user starts inputting something and then navigating the menus again.
Sometimes it will print out integers/characters that were previously entered by the user when pressing the ENTER key in the menu.
I managed to block the inputs with cin.ignore(). But how can I eliminate the display of the (I don't know if its excess or garbage) inside the input buffer whenever the user presses the LEFT and RIGHT arrow keys? Or I think there's even one for UP and DOWN arrow key?
int main()
{
color(FG_WHITE);
bool progFinished = false;
while (!progFinished) {
cout << "[ACTIVITY FOR 26 FEBRUARY 2018]\n";
vector<string> menu = { "Currency Converter","Student Information System" };
int choice = menu_std(menu, FG_YELLOW, false);
bool currencyFinished = false;
bool studentFinished = false;
switch (choice) {
case 0:
while (!currencyFinished) {
cout << "\n\n[CURRENCY CONVERT]\n";
vector<string> curr_menu = { "PHP TO OTHERS","USD TO PHP","YEN TO PHP","EUR TO PHP","BHD TO PHP" };
int currency = menu_std(curr_menu, FG_YELLOW, false);
double value = validation("\nEnter the value", 0, false);
CurrencyConverter convert(currency, value);
cout << "\n\nConvert again? ";
currencyFinished = !menu_yn(FG_YELLOW);
}
system("cls");
break;
case 1:
Studentv2 stud;
stud.LoadFile();
while (!studentFinished) {
cout << "\n\n[STUDENT INFO SYSTEM]\n";
vector<string> stud_menu = { "Add Student","View All Students" };
int stud_choice = menu_std(stud_menu, FG_YELLOW, false);
switch (stud_choice) {
case 0:
stud.AddStudent();
cout << "\n\nDo you want to continue with the Student Info System? ";
stud.SaveFile();
studentFinished = !menu_yn(FG_YELLOW);
break;
case 1:
stud.DisplayStudentInformation();
cout << "\n\nDo you want to continue with the Student Info System? ";
studentFinished = !menu_yn(FG_YELLOW);
break;
}
}
system("cls");
break;
}
cout << "\nContinue using the program? ";
progFinished = !menu_yn(FG_YELLOW);
system("cls");
}
cout << "Press any key to exit.\n";
cin.get();
return 0;
}
Code snippets for some of the functions I used inside my program.
Function menu_std():
int menu_std(std::vector<std::string> &menu, WORD atrribute, bool argument)
{
sleep_for(milliseconds(150));
int elements = menu.size();
int elements_max_size = menu.size() - 1;
int menu_position = 0;
COORD position = getcoordinates();
while (true) {
for (int i = 0; i < elements; i++) {
move(position.X, position.Y + i);
if (i == menu_position) {
color(atrribute);
std::cout << menu[i];
color(FG_WHITE);
}
else {
std::cout << menu[i];
}
}
while (true) {
if (GetAsyncKeyState(VK_UP) < 0 && menu_position != 0) {
menu_position--;
break;
}
if (GetAsyncKeyState(VK_DOWN) < 0 && menu_position != elements_max_size) {
menu_position++;
break;
}
if (GetAsyncKeyState(VK_RETURN) < 0) {
std::cin.ignore(10000, '\n');
if (argument == true) {
sleep_for(milliseconds(100));
COORD previous = getcoordinates();
std::cout << "\n\nAre you sure? ";
bool sure = menu_yn(atrribute);
if (sure) {
clear_output(previous);
return menu_position;
}
else {
clear_output(previous);
break;
}
}
else {
return menu_position;
}
}
sleep_for(milliseconds(50));
}
sleep_for(milliseconds(175));
}
}
Function menu_yn():
bool menu_yn(WORD attribute)
{
sleep_for(milliseconds(150));
COORD currentpos = getcoordinates();
int pos = 0;
while (true) {
move(currentpos);
switch (pos) {
case 0:
color(attribute);
std::cout << "YES";
color(FG_WHITE);
std::cout << " NO";
break;
case 1:
std::cout << "YES";
color(attribute);
std::cout << " NO";
color(FG_WHITE);
break;
}
while (true) {
if (GetAsyncKeyState(VK_RIGHT) < 0 && pos != 1) {
pos++;
break;
}
if (GetAsyncKeyState(VK_LEFT) < 0 && pos != 0) {
pos--;
break;
}
if (GetAsyncKeyState(VK_RETURN) < 0) {
std::cin.ignore(10000, '\n');
if (pos == 0) {
return true;
}
else if (pos == 1) {
return false;
}
}
}
sleep_for(milliseconds(175));
}
}
Function validation()
double validation(std::string question, double limit, bool argument)
{
double userInput;
while (true) {
std::cout << question << " >> ";
while (!(std::cin >> userInput)) {
std::cout << "Error: Non-integer input. " << question << " >> ";;
std::cin.clear();
std::cin.ignore(256, '\n');
}
std::cin.ignore(10000, '\n');
while (userInput > limit && limit != 0) {
std::cout << "Error: Input out-of-bounds. " << question << " >> ";
std::cin >> userInput;
std::cin.ignore(10000, '\n');
}
if (argument) {
return (userInput <= 0) ? 1 : userInput;
}
else {
return userInput;
}
}
}
Screenshot of the problem:
There are numbers appearing whenever I press enter on the menu. I suspect that it comes whenever the user presses the arrow keys before entering the enter.

Is there a proper way to close program from within function?

I am currently making a program that has menu and sub menus and i'd like to have an opportunity to close entire thing from within any sub menu (which is inside function). I know i could do it different way (both way i have it now and way i know it could be done are below) but for future use (i could prove useful) i would like to know:
Is there a way to properly close the program from within a function ?
I've read a few answers here and here and from what i've gotten out of it is:
I should not simply terminate the code
I should not use any system specific code as to avoid compatibilty issues
A bit of clarifiacation - that is program for my personal educational purposes so it is kind of unnecessary to worry about it but i'd rather learn propper ways of dealing with those situations at the beginning than have to learn them anew once i discover how to do them later and have to go back and fix it.
As for code samples (using visual studio 2017):
//skipping unrelevant to question bits of code
void sub_menu()
{
char a=0;
system("cls"); //yet to get changed when i'll find other way of clearing screen
std::cout << "Pick function:\n"
<< "---------------------------------------------------------\n"
<< "1.Foobar_1\n"
<<"---------------------------------------------------------\n"
<< "ESC to go back to menu menu\n"
<< "x to close";
while (a != 27)
{
a = _getch();
switch (a)
{
case 49:foobar_1(); break;
case 120:
case 88: system("exit"); break; //i'd like to replace that part
default: break;
};
};
return;
}
void main()
{
char a=0;
system("cls"); //yet to get changed when i'll find other way of clearing screen
std::cout << "Pick what you want to do:\n"
<< "---------------------------------------------------------\n"
<< "1.Sub_menu\n"
<< "2.foo\n"
<< "3.bar\n"
<<"---------------------------------------------------------\n"
<< "ESC to close\n"
while (a != 27)
{
a = _getch();
switch (a)
{
case 49:sub_menu(); break;
case 50:foo(); break;
case 51:bar(); break;
case 120:
case 88: system("exit"); break; //i'd like to replace that part
default: break;
};
};
return;
}
The way i could implement it (i think it is merely byapssing the issue):
//skipping unrelevant to question bits of code
void main()
{
char a=0;
system("cls"); //yet to get changed when i'll find other way of clearing screen
std::cout << "Pick what you want to do:\n"
<< "---------------------------------------------------------\n"
<< "1.Sub_menu\n"
<< "2.foo\n"
<< "3.bar\n"
<<"---------------------------------------------------------\n"
<< "ESC to close\n"
while (a != 27)
{
a = _getch();
switch (a)
{
case 49:
{
{
char b=0;
system("cls");
std::cout << "Pick function:\n"
<< "---------------------------------------------------------\n"
<< "1.Foobar_1\n"
<<"---------------------------------------------------------\n"
<< "ESC to go back to menu menu\n"
<< "x to close";
while (b != 27)
{
b = _getch();
switch (b)
{
case 49:foobar_1(); break;
case 120:
case 88: a=27; break;
default: break;
};
};
return;
}
}
; break;
case 50:foo(); break;
case 51:bar(); break;
case 120:
case 88: system("exit"); break; //i'd like to replace that part
default: break;
};
};
return;
}
This Code will work as you mentioned.
int main()
{
int a;
while (true)
{
system("cls"); //yet to get changed when i'll find other way of clearing screen
cout << "Pick what you want to do:\n"
<< "---------------------------------------------------------\n"
<< "1.Sub_menu\n"
<< "2.foo\n"
<< "3.bar\n"
<< "---------------------------------------------------------\n"
<< "0. to close\n";
cin >> a ;
if (a == 0)
{
return 0 ;
}
else if (a == 1)
{
char b;
while (true)
{
system("cls");
cout << "Pick function:\n"
<< "---------------------------------------------------------\n"
<< "1.Foobar_1\n"
<< "---------------------------------------------------------\n"
<< "b to go back to menu menu\n"
<< "x to close\n";
cin >> b;
if (b == '1')
{
foobar_1();
}
else if (b == 'B' || b == 'b')
{
break;
}
else if (b == 'x' || b == 'X')
{
return 0;
}
else
{
cout << "invalid input";
}
}
}
else if (a == 2)
{
foo();
}
else if (a == 3)
{
bar();
}
else
{
cout << "invalid input";
}
}
return 0;
}

My program won't print anything...

So I'm working on building a calculator that mimics the ones we use everyday. What I have shown is the logic within my function.
Previously when I had my cout lines (deleted, not shown anymore) to see if my "y" was being correctly stores, and my two variables, finalNum1 & 2, everything was working, but when going back, trying to add new cout lines, nothing prints. If there are more issues, feel free to point them out, but this code is unfinished, my main concern is nothing is printing, I understand this code still needs work. If anyone can help that'd be greatly appreciated!
int Calculator::calculate()
{
if (userInput[0] != 'q' || 'Q') // Checks for user input "Quit" or "quit"
{
int stringSize;
std::cin >> userInput; // User Input
stringSize = userInput.length();
int y = 0;
while (y < stringSize)
{
if (isdigit(userInput[y]))
{}
else
{
posi = y;
}
y++;
}
first = userInput.substr(0,posi);
second = userInput.substr(posi+1,y);
finalNum1 = std::stoi(first); // Sigbart error
finalNum2 = std::stoi(second);
std::cout << finalNum1 + finalNum2; // won't print
switch (userInput[posi])
{
case '+':
std::cout << finalNum1 + finalNum2;
break;
case '-':
std::cout << finalNum1 - finalNum2;
break;
case '*':
std::cout << finalNum1 * finalNum2;
break;
case '/':
std::cout << finalNum1 / finalNum2;
break;
case '%':
std::cout << finalNum1 % finalNum2;
break;
}
}
else
std::cout << "Goodbye";
return 0;
}
For printing integer it is easier if you cast them to string.
using namespace std;
float tmp = finalNumber1 + finalNumber2;
string out = to_string(tmp);
cout << tmp << endl;
Then another thing. You should use for instead of while.

Decimal to binary/hex using arrays

I'm having trouble with the hexadecimal part of my c++ program. When I use the switch for hexadecimal nothing returns. also for some reason my binary conversion has a leading 0 I cant seem to get rid of.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
void binary(int, int);
void hex(int, int);
int _tmain(int argc, _TCHAR* argv[])
{
int numb, base;
cout << "Enter a decimel number : ";
cin >> numb;
cout << "Enter a base you want number switched to: ";
cin >> base;
switch (base){
case 2: binary(numb, base); break;
case 8: break;
case 16: hex(numb, base); break;
default: break;
}
}
void binary(int numb, int base)
{
int bin[32] = { 0 };
int i = 0;
do
{
bin[i] = numb%base;
numb = numb / base;
i++;
} while (numb != 0);
cout << "\n";
while (i >= 0){
cout << bin[i];
i--;
}
cout << endl << endl;
}
void hex(int numb, int base){
int i;
int hex[10] = { 0 };
for (i=0; i > 10; i++){
hex[i] = numb%base;
numb = numb / base;
for (i; i > 0; i--)
{
if (hex[i] >= 10)
{
switch (hex[i]){
case 10: cout << "A"; break;
case 11: cout << "B"; break;
case 12: cout << "C"; break;
case 13: cout << "D"; break;
case 14: cout << "E"; break;
case 15: cout << "F"; break;
default: break;
}
}
cout << hex[i];
}
}
cout << endl;
}
binary
The problem is after the first loop, i is one greater than the last index. Just for example, say you enter 1: the do...while loop is entered, the digit 1 is put in array index 0, then i is incremented to 1.
Then, in the second loop, both indexes 1 and 0 are printed. You can solve this by decrementing i before entering this loop:
i--;
while (i >= 0){...}
You should be doing something like that anyway, because if you ended up using all 32 digits, you would try to access bin[32] and the program may crash or output gibberish.
hex
The first loop's condition is infinite:
for (i = 0; i >= 0; i++){...}
It should be the same as your condition in binary:
for (i = 0; numb != 0; i++){...}
But you are not done yet because I've noticed you also have a bug in your printing:
if (hex[i] >= 10)
{
switch (hex[i])
{
case 10:
cout << "A";
break;
...
}
}
cout << hex[i];
If hex[i] is greater than or equal to 10, it gets printed twice, once as a hex letter and once as a decimal number. To solve this you could, for example, use continue instead of break in your switch (to skip the second print), or use else:
if (hex[i] >= 10)
{
switch (hex[i])
{
case 10:
cout << "A";
break;
...
}
}
else
{
cout << hex[i];
}
You also need to make the same correction as in binary:
// decrementing i before entering the loop
// vvv
for (i--; i >= 0; i--){...}
Your revision is not correct, hex should not have a nested loop. It was fine before, just with the corrections I've pointed out.