One button input in C/C++ Console Applications:
Code:#include <iostream> #include <conio.h> #include <stdio.h> #include <windows.h> /* Some info on include files: -windows.h is not needed in DevC++ -stdio and conio are C header files, you can use C code in your C++ programs */ int main(void) { using namespace std; int input; // Get input: input = getch(); putchar (input); system("cls"); // Check key value if (input == 49) { //49 = value for the "1" key cout << "You entered " << 1; } else if (input == 50) { //50 = value for the "2" key cout << "You entered " << 2; } return main(); }
Now how do you find the key value of the button you pressed? Run this :)
Let me know if you have a question on the code, this can be used in place of the "cin" (Console Input) which you will have to input a value and then press Enter. Also with this you can input a letter or a number, but only one, not a string.Code:#include <iostream> #include <conio.h> #include <stdio.h> #include <windows.h> /* Some info on include files: -windows.h is not needed in DevC++ -stdio and conio are C header files, you can use C code in your C++ programs */ int main(void) { using namespace std; int input; // Get input: input = getch(); putchar (input); system("cls"); // Display value of key printf("Key Value: %d", input); return main(); }
Hope this helped some way.
-TEO






Reply With Quote
Bookmarks