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