Can anyone help me?

0 Lane Beals · June 14, 2014

#include <iostream>
#include "windows.h"
#include <fstream>
#include <ctime>

using namespace std;

int main()
{
cout << "Advanced calculator by pdxzombiekiller123.
" << endl;
cout << "Press 1 for the calculator or press ESC to exit." << endl;

int choose;

cin >> choose;

switch(choose)
{
case 1:
cout << "Test" << endl;

case (GetAsyncKeyState(VK_ESCAPE)):
break;
}

return 0;
}


I get the error for the "case (GetAsyncKeyState(VK_ESCAPE)):

I think it might be because of the value of "choose." (an integer) Can anyone help me?

Post a Reply

Replies

Oldest  Newest  Rating
0 Lane Beals · June 15, 2014
I have tried, the while loop. It doesn't work too good as compared to Cases. I have seen some examples before, I just can't remember where.
0 Alexander Taylor · June 14, 2014
I would recommend you just change your code so you don't use escape, instead you use a keyboard input, such as 0 to check against what the user puts in and then take action from there, getting keyboard input like that for console is not very common, at least from what i am aware and if there is a way to do it properly, its probably a lot more work than is really needed. But if you really have to use it, then you will probably use some sort of while loop to wait for user input then check each frame if the user inputs either escape or the nr 1 button
0 Lane Beals · June 14, 2014
So can someone help me re-write my code?
0 Alexander Taylor · June 14, 2014
@Linguist Llama thank you for correcting me
-3 Lane Beals · June 14, 2014
Well, I want it so that if I press the Escape key, the program breaks (ends).
0 Alexander Taylor · June 14, 2014
None of this code really works together,

Linguist has already pointed to the exact problem in the code but, from reading your code you are going at the whole concept wrong.

You are trying to capture the key press for Esc, while getting the input from a user on a numeric value, this doesn't really work quite like that.
The GetAsyncKeyState will get the virtual key's inputted value, that is to say if it is down or not but firstly, that is a bool, not an integer, you cant really put bools into switch statements.
Also you wont be able to capture the key state during the input as the cin function will wait until the user presses enter before continuing, which means even if they did press escape, nothing would happen.

Here is an example how i personally would have written this code


#include <iostream>

enum class eActions
{
eQuit,
eDoStuff
};

int main()
{
std::cout << "Advanced calculator by pdxzombiekiller123." << std::endl;
std::cout << "Press 1 to do stuff, or press 0 to quit. (press enter to confirm your choice)" << std::endl;

int choose = 0;

cin >> choose;

switch(static_cast<eActions>(choose))
{
case eActions::eQuit:
return 0;
case eActions::eDoStuff:
std::cout << "test" << std::endl;
break;
}

return 0;
}




Hope this will help you in your task
<!--?prettify lang=html linenums=true?-->
  • 1

C++

107,009 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator