Error with getch() - c++

void move_player(int& x, int& y) {
int old_x, old_y, new_x, new_y;
switch (getch()) {
case 0x31: // miscare stanga
if (current_x>min_x) {
old_x=x;
new_x=x-1;
if (y==1) {
path[1][old_x]=' ';
path[1][new_x]=player;
system("cls");
for (int y=0;y<3;y++) {
for (int x=0;x<20;x++) {
cout << path[y][x];
}
cout << endl;
}
} else if (y==2) {
path[2][old_x]='_';
path[2][new_x]=player;
system("cls");
for (int y1=0;y1<3;y1++) {
for (int x1=0;x1<20;x1++) {
cout << path[y1][x1];
}
cout << endl;
}
}
}
break;
case 0x32:
if (x<max_x) {
old_x=current_x;
new_x=current_x+1;
if (y==1) {
path[1][old_x]=' ';
path[1][new_x]=player;
system("cls");
for (int y2=0;y2<3;y2++) {
for (int x2=0;x2<20;x2++) {
cout << path[y2][x2];
}
cout << endl;
}
} else if (y==2) {
path[2][old_x]='_';
path[2][new_x]=player;
system("cls");
for (int y3=0;y3<3;y3++) {
for (int x3=0;x3<20;x3++) {
cout << path[y3][x3];
}
cout << endl;
}
}
}
break;
case 0x33:
if (y>min_y) {
old_y=y;
new_y=y-1;
path[old_y][x]='_';
path[new_y][x]=player;
system("cls");
for (int y4=0;y4<3;y4++) {
for (int x4=0;x4<20;x4++) {
cout << path[y4][x4];
}
cout << endl;
}
}
break;
case 0x34:
if (y<max_y) {
old_y=y;
new_y=y+1;
path[old_y][x]=' ';
path[new_y][x]=player;
system("cls");
for (int y5=0;y5<3;y5++) {
for (int x5=0;x5<20;x5++) {
cout << path[y5][x5];
}
cout << endl;
}
}
}
x=new_x;
y=new_y;
}
This is the function for moving a player in a two dimensional space.
It is represented by an array which I declared as char path[3][20] and the player can only move in path[1][20] or path[2][20]. The first layer of the array is just represented by a bunch of lines, kind of setting the boundaries for the map. Btw, the player starts at path[2][0] or lower left corner.
(I can't move it left or down in the starting position, only right or up)
The problem is, that when I run this, I can move it fine just once right, but then when I try it to move left or right it does nothing, and when I try to move it up or down the console just frezees and it quits.
How can I solve it?

Related

I having problem with my searching function C++

i'am begginner in C++, i want to ask question about what's wrong about my coding, when i doing my work then found out problem i can't solve in searching function, there's no error notice or anything from visual studio but my function still didn't work,
sorry for my bad english.
here is my full code
#include <iostream>
#include <cstdlib>
#define MAX 5
using namespace std;
struct College {
int nim;
string name;
}mahasiswa;
struct queue {
College college[MAX];
int start,end = - 1;
}antrean;
void init() {
antrean.start= -1;
antrean.end = -1;
}
bool full() {
if (antrean.end== MAX - 1) {
return true;
}
else {
return false;
}
}
bool empty() {
if (antrean.end== -1) {
return true;
}
else {
return false;
}
}
void tampilData() {
int i;
if (!empty()) {
for (i = antrean.start; i < antrean.end; i++)
{
cout<<antrean.college[i].nim<<" | ";
cout << antrean.college[i].name << " | ";
cout << "\n";
}
}
cout<<"\n";
}
void inQueue() {
tampilData();
int elemennim;
string elemenname;
if (!full()) {
cout << "Input your NIM : ";
cin >> elemennim;
cout << "\n";
cout << "Input your name: ";
cin >> elemenname;
cout << "\n";
cout << "Succefully\n";
antrean.college[antrean.end].nim = elemennim;
antrean.college[antrean.end].name = elemenname;
antrean.end++;
}
else
{
cout << "Queue Penuh\n";
}
}
and this the error function i cant solve,
void searching(int key) {
for (int i = 0; i <= antrean.start; i++)
{
if (key == antrean.college[i].nim) {
cout << "Element found in index " << i;
}
else
{
cout << "Element not found\n";
}
cout << antrean.college[i].nim;
}
}
int main() {
int choice, elemen;
init();
cout << "Demo Queue dengan Linear Array" << endl;
do {
tampilData();
cout << "\nMenu Utama\n";
cout << "==============\n";
cout << "[1] Init \n[2] InQueue \n[3] Searching \n[4] out \n";
cout << "==============\n";
cout << "\nMasukkan pilihan: "; cin >> choice;
cout << "==============\n";
switch (choice) {
case 1: init(); break;
case 2: inQueue(); break;
case 3: cout << "masukkan NIM yang ingin dicari \n";
cin >> choice;
searching(choice); break;
} while (choice != 0);
return 0;
}
please help me, thank you very much

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

static stack function does not 1) take input of 1st element 2)recognise old element when new element is added

There is an array of nodes(a structure) and is used as a
stack.
it has 3 functions
to add new elements (push)
to delete elements(pop)
to display(display)
problems:
does not save the first input
when a new input is added, it replaces the previous node with the new input.
please help me identify where i have gone wrong
#include <iostream>
#include<conio.h>
#include<process.h>
struct node
{
int x, y;
};
int top = -1;
class stack
{
node s[30];
public:
void push();
void pop();
void display();
};
void stack::push()
{
if (top < 29)
{
cout << "enter elements" << endl;
int a, b;
cin >> a >> b;
top = top + 1;
s[top].x = a;
s[top].y = b;
}
else
{
cout << "OVERFLOW" << endl;
}
}
void stack::pop()
{
node temp;
temp = s[top];
top--;
cout << "element" << temp.x << "&" << temp.y << " has been deleted" << endl;
}
void stack::display()
{
for (int i = 0; i < top; i++)
{
cout << s[top].x << "&" << s[top].y << endl;
}
}
void main()
{
clrscr();
stack sup;
int choice = 1;
do
{
cout << "1.add" << endl << "2.delete" << endl << "3.display" << endl;
int c;
cin >> c;
switch (c)
{
case 1:
sup.push();
sup.display();
break;
case 2:
sup.pop();
sup.display();
break;
case 3:
sup.display();
break;
default:
cout << "error in switch case" << endl;
}
cout << "enter 1 to perform more operations" << endl;
cin >> choice;
} while (choice == 1);
getch();
}
You delete and recreate you stack (sup) every iteration of your loop. The declaration should be before the do.

Car Instrument Simulator

So I'm working on a code to program a Car Instrument Simulator and I'm supposed to have a switch statement for a menu to: Show gauges, ask how far they are to Driving, ask how much Gas to add, and to Exit. I got it to show the menu but when I press one of the options it doesn't do anything. I would like someone to help me to figure out what I'm missing and not saying to help me do it because I want to learn so next time I won't have the same problem, been working on it for a while now and even looked up some videos about switch statements but I'm just not getting it.
#include <iostream>
#include <string>
using namespace std;
void printmenu();
class FuelGauge
{
private:
int CurrentFuel;
public:
FuelGauge();
~FuelGauge();
FuelGauge(int g)
{
CurrentFuel = g;
}
int getCurrentFuel()
{
return CurrentFuel;
}
void IncrementFuel()
{
int gas;
cout << "How much fuel are you putting in? " << endl;
cin >> gas;
for (int fuel = gas; gas > 0; gas--) {
CurrentFuel++;
}
}
void DecrementFuel()
{
if (CurrentFuel > 0)
CurrentFuel--;
}
};
FuelGauge::FuelGauge()
{
}
FuelGauge::~FuelGauge()
{
}
class Odometer
{
private:
int CurrentMileage;
FuelGauge* fuel;
public:
Odometer();
~Odometer();
Odometer(int miles, FuelGauge* f)
{
CurrentMileage = miles;
fuel = f;
}
int getCurrentMileage()
{
return CurrentMileage;
}
void incrementCurrentMileage()
{
if (CurrentMileage < 999999)
CurrentMileage++;
else
CurrentMileage = 0;
}
void decrementCurrentMileage()
{
if (CurrentMileage > 24)
CurrentMileage--;
(*fuel).DecrementFuel();
}
};
Odometer::Odometer()
{
}
Odometer::~Odometer()
{
}
int main()
{
FuelGauge fuel(15);
Odometer odo(0, &fuel);
int n = 0;
while (n != 5) {
printmenu();
cin >> n;
switch (n) {
case 1:
fuel.getCurrentFuel();
break;
case 2:
odo.getCurrentMileage();
break;
case 3:
break;
case 4:
fuel.IncrementFuel();
break;
default:
break;
}
return 0;
}
}
void printmenu() {
cout << "1. Show current fuel " << endl;
cout << "2. Show current status of the odometer " << endl;
cout << "3. How far are you going? " << endl;
cout << "4. How much gass are you putting in? " << endl;
}
What do you plan to observe? You call some methods that return values without any side effects, and you don't do anything with those values: you get lost them immediately. You should probably output them:
cout << fuel.getCurrentFuel();

Exceptions in C++ how to get back to "main menu"

Well, I'm studying for an exam so I tried to keep my code as simple as possible, but then something very weird happened: the exercise was to change the following code and, by using exceptions, get back to the main menu.
Here's the code of the question:
void ha_ha_loop()
{
int i, c;
while(1)
{
for(i=0; i < 3; i++)
{
cout << "Ha Ha Ha" << endl;
sleep(3);
} // for
ask_return();
} // while
} // ha_ha_loop
void dollar_loop()
{
int i;
while(1)
{
for(i=0; i < 3; i++)
{
cout << "$$$$$$$$$ " << endl;
sleep(3);
} // for
ask_return();
} /* while */
} // dollar_loop
void mainloop()
{
string answer;
while (1)
{
cout << "Press 1 for Ha Ha Ha." << endl;
cout << "Press 2 for $$$$$$$$." << endl;
cout << "Press 3 for to quit." << endl;
cin >> answer;
switch (answer[0])
{
case '1':
ha_ha_loop();
case '2':
dollar_loop();
case '3':
return;
} // switch
} // while
} // mainloop
And what I did was:
void ask_return() {
char c;
cout << "Return to main menu? y/n:"<<endl;
cin >> c;
if (c=='y') throw 1;
}
void ha_ha_loop()
{
int i, c;
while(1)
{
for(i=0; i < 3; i++)
{
cout << "Ha Ha Ha" << endl;
} // for
ask_return();
} // while
} // ha_ha_loop
void dollar_loop()
{
int i;
while(1)
{
for(i=0; i < 3; i++)
{
cout << "$$$$$$$$$ " << endl;
} // for
ask_return();
} /* while */
} // dollar_loop
void mainloop()
{
char answer;
while (1)
{
cout << "Press 1 for Ha Ha Ha." << endl;
cout << "Press 2 for $$$$$$$$." << endl;
cout << "Press 3 for to quit." << endl;
cin >> answer;
switch (answer)
{
case '1':
ha_ha_loop();
case '2':
dollar_loop();
case '3':
return;
}
}
}
int main() {
try {
mainloop();
} catch (...) {
mainloop();
}
}
It works fine at the beginning, but then after one time it terminates my program with an unhandled exception message. Why?
What is the simplest correct way to do that?
Edit:
this is a working way :
void ask_return() {
char c;
cout << "Return to main menu? y/n:"<<endl;
cin >> c;
if (c=='y') throw 1;
}
void ha_ha_loop()
{
int i, c;
while(1)
{
for(i=0; i < 3; i++)
{
cout << "Ha Ha Ha" << endl;
} // for
ask_return();
} // while
} // ha_ha_loop
void dollar_loop()
{
int i;
while(1)
{
for(i=0; i < 3; i++)
{
cout << "$$$$$$$$$ " << endl;
} // for
ask_return();
} /* while */
} // dollar_loop
void mainloop()
{
char answer;
while (1)
{
try {
cout << "Press 1 for Ha Ha Ha." << endl;
cout << "Press 2 for $$$$$$$$." << endl;
cout << "Press 3 for to quit." << endl;
cin >> answer;
switch (answer)
{
case '1':
ha_ha_loop();
case '2':
dollar_loop();
case '3':
return;
}
} catch (...) {
}
}
}
int main() {
mainloop();
}
I'll only answer the "how come" part, since the simplest correct way is what you're supposed to solve yourself. When you do
try {
mainloop();
} catch (...) {
mainloop();
}
you execute mainloop, catching any exceptions. When an exception is caught, the handler executes mainloop again, outside of a try block. You'll want to repeatedly go into mainloop, catching the exception every time.
OK, here's my suggestion.
First, ask_return() needs to get written. This is simple enough, and as far as I can tell you had it more-or-less right. I'm going to suggest actually using a std::exception though:
void ask_return()
{
char c;
cout << "Return to main menu? y/n" << endl;
cin >> c;
if ( c == 'y' )
{
throw std::exception(); // might as well use the class designed for this
}
}
There are numerous places where you could catch the exception, but the easiest place is within mainloop():
void mainloop()
{
string answer;
while (1)
{
cout << "Press 1 for Ha Ha Ha." << endl;
cout << "Press 2 for $$$$$$$$." << endl;
cout << "Press 3 for to quit." << endl;
cin >> answer;
try
{
switch (answer[0])
{
case '1':
ha_ha_loop();
case '2':
dollar_loop();
case '3':
return;
} // switch
}
catch (std::exception& e)
{
// do nothing, let the loop re-run.
}
} // while
} // mainloop
In this case, you don't even need to do anything about the exception: you just let the function keep looping, and it will work.