keyboard presses

Hi I'm looking to write a text viewing console app in visual C++ 2010, but i'd like to know if there is anyone who would be able to tell me how I could write some code to record what key I'm pressing - I.E. '1'. If I press this key - load a text file from which I can read from it. If I press the '2' key, load another text file and read from it. Thank you

1
2
3
4
5
6
7
  pseudo code
  if(is_key_press('1')){
  load file
}
  pseudo code
  if(is_key_press('2')){
  load another file
I'm not entirely sure if this is what you are asking for.

This would be a simple way to open files based off a key press though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    int selection;
    std::ifstream fin; // create a file-reading object

        std::cout << "Please Choose a File to open:\n";
        std::cout << "1. File Number 1\n";
        std::cout << "2. File Number 2\n";
        std::cout << "3. Exit the program\n";
        std::cin >> selection;
        std::cout << "\n";

        if (selection == 1){
            fin.open("File_Number_2.txt");//open the file
                if (!fin.good()){
                std::cout << "File Not Found";
                return 1; // exit if file not found
                }
                std::cout << "File Number 1 Opened\n";
        }
        if (selection == 2){
            fin.open("File_Number_2.txt");//open the file
                if (!fin.good()){
                std::cout << "File Not Found";
                return 1; // exit if file not found
                }
                std::cout << "File Number 2 Opened\n";
        }
        if (selection == 3){
                std::cout << "Exiting Program";
                return 1;
                }
    }
Last edited on
Thank you very much. This is pretty much what I need. I'll try to play with this and see if i can get this to work
Topic archived. No new replies allowed.