Solution and Explanation:
//Header files
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
fstream fin;
char str[]="";
string question;
char filename[20];
char choice;
string answer;
//seed for random number
unsigned seed=0;
srand(seed);
cout<<"Enter your file name : ";
cin>>filename;
cout<<endl;
//if file not exist then gives message
//reading file
do
{
fin.open(filename,ios::in);
if(!fin)
{
cout<<"couldnot find the file...";
std::system("pause");
return 0;
}
while(!fin.fail())
{
cout<<endl<<"Enter your quesion"<<endl;
fflush(stdin);
getline(cin,question);
answer="";
char ch;
while((ch=fin.get())!='\\'&&!fin.fail())
{
//reading line
answer+=ch;
if(ch=='#')
{
int randNum=1+rand()%18;
answer +=randNum+'0';
ch=fin.get();
}
}
cout<<answer<<endl;
}
fin.close();
cout<<"Do you want to continue (Y/y to continue) :";
cin>>choice;
}while(choice=='y'||choice=='Y');
//To pause the output
system("pause");
return 0;
}//end of main..