Code:
#include <iostream>
#include <string>
#include <fstream>
using std::cin;
using std::cout;
using std::string;
using std::ifstream;
using std::endl;
using std::getline;
int main(int argc, char *argv[])
{
ifstream theFile;
string theFileName;
while(1)
{
cout<<"\t\t\tEnter the option:"<<endl;
cout<<"\t\t\t1 = Lab 1.cpp"<<endl;
cout<<"\t\t\t2 = Lab 2.cpp"<<endl;
cout<<"\t\t\t3 = Lab 3.cpp"<<endl;
cout<<"\t\t\t4 = Lab 4.cpp"<<endl;
cout<<"\t\t\tCTRL+Z/D = quit."<<endl;
getline(cin, theFileName);
//User wants to quit?
if( cin.eof() )
{
//User wants to quit. Get out of the while-loop.
break;
}
//Assuming files are named like: Lab_1.cpp, Lab_2.cpp etcetera.
theFileName.insert(0, "Lab_");
theFileName += ".cpp";
theFile.open( theFileName.c_str() );
if( !theFile )
{
cout<<endl<<"ERROR!"<<endl;
cout<<"Reason: could not open the file."<<endl;
cout<<"Make sure your entry is valid."<<endl<<endl;
theFile.close();
theFile.clear();
}
if( theFile.is_open() )
{
cout<<"*** File starts below ***"<<endl<<endl;
cout<<theFile.rdbuf();
cout<<endl<<"*** File ended ***"<<endl<<endl;
theFile.close();
}
}
return 0;
}