-
April 20th, 2015, 06:19 AM
#1
Press a key to continue
Hello
My problem is , i want to make a simple calculator , in the console i want afterall the user to choose what type of operation he want pressing a key like i mentioned in the title the keys would be F1,F2,F3 , i'm not finding in the internet the code to the detection of a press key like what i want.
Thank you
Code:
#include <iostream>
using namespace std;
int F1 (int x, int y)
{
cout << "Enter a number: " << endl;
cin >> x;
cout << "Enter a number: " << endl;
cin >> y;
int answer = x + y;
return answer;
}
int F2 (int z, int c)
{
cout << "Enter a number: " << endl;
cin >> z;
cout << "Enter a number: " << endl;
cin >> c;
int minusanswer = z - c;
return minusanswer;
}
int F3 (int v, int b)
{
cout << "Enter a number: " << endl;
cin >> v;
cout << "Enter a number: " << endl;
cin >> b;
int multipanswer = v * b;
return multipanswer;
}
int main()
{
cout << "What Type of calculation do you wanna make? " << endl;
cout << "F1--> addiction " << " F2-->subtraction " << " F3-->multiplication" << endl;
if (F1)
F1(1,1);
else if (F2)
F2(2, 2);
else if (F3)
F3(2, 2);
return 0;
}
-
April 20th, 2015, 07:03 AM
#2
Re: Press a key to continue
You can't type in F1-F3 in a console app.
or rather, you can't with std::cin.
if you include conio.h (not available on all C++ compilers), you can use getch().
In that case, Function keys will first return a 0, and then return the function value.
so a common way to handle it in that case is to make all 2nd code keys negatives.
Code:
int key = getch(); // get key
if (!key) // special key ?
key = -getch(); // get special key code.
in that case F1 = -59, F2 = -60, F3 = -61...
Also, there's a big different between addition and addiction. :-)
-
April 20th, 2015, 07:10 AM
#3
Re: Press a key to continue
i'll see what i can do with that , thank you , and i misspelled that ^^
-
April 20th, 2015, 07:47 AM
#4
Re: Press a key to continue
AFAIW, using just c++ i/o you can't do what you want - press a function key and then undertake processing based upon the pressed function key. To do this you need to use direct console i/o which is os specific and therefore non-portable.
For windows you can do something like the code below which will return a negative value for a special key pressed such as f1, f2 or up/down arrow etc. This windows test example will just show a value for the key pressed but once you know what values are obtained for the required keys then this can be used in your program.
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int input()
{
int ch;
return (ch = getch()) ? ch : -getch();
}
int main()
{
cout << input() << endl;
}
PS [Same as what OReubens said in his post - didn't see it before I posted]
Last edited by 2kaud; April 20th, 2015 at 07:51 AM.
Reason: PS
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 20th, 2015, 07:58 AM
#5
Re: Press a key to continue
1>c:\users\miguel\desktop\c++ projects\teste\teste\source.cpp(9): error C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\conio.h(131) : see declaration of 'getch'
it give me that error when i try to use your code 2kaud
-
April 20th, 2015, 08:02 AM
#6
Re: Press a key to continue
It compiles for me using MS VS 2013. But if the compiler you are using is complaining about getch(), then
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int input()
{
int ch;
return (ch = _getch()) ? ch : -_getch();
}
int main()
{
cout << input() << endl;
}
which uses _getch() rather than getch().
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 20th, 2015, 08:10 AM
#7
Re: Press a key to continue
i use visual studio 2013 too
-
April 20th, 2015, 08:36 AM
#8
Re: Press a key to continue
it gave me the codes that i need , but i need to study better this , im a complete newbie in that and some of the "Words" i don't understand for what i can use them . Well thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|