Please use C code Lab Write Text Question File 1 Ask user fo
********Please use C++ code!**********
Lab Write Text Question File 1. Ask user for file name 2. open file for output 3. ask user for number of questions 1 && 11) 4. for that many questions, prompt user for the question 5. write the question to a file 6. close the file. What questions? The questions in your file would be C++ questions for a quiz.Solution
#include <iostream>
#include <fstream>
using namespace std;
//function to get question from user
void getQuestions(string questions[],int no)
{
for(int i=0;i<no;i++)
{
cout<<\"\ Enter question \"<<i<<\":\ \";
getline(cin,questions[i]);
cin.ignore();
}
}
//function to open the file for writing
//write list of questions to file and close it
void writeToFile(string questions[],int no,string fname)
{
ofstream myfile;
myfile.open (fname);
for(int i=0;i<no;i++)
myfile << questions[i]<<\"\ \";
myfile.close();
}
//main fucntion which promts the user for file name and no of questions
int main()
{
string fname;
int no;
bool flag=true;
//prompt the user for file name
cout << \"\ Enter file name: \";
cin>>fname;
//prompt the user for no of questions
do
{
cout<<\"\ Enter no of questions: \";
cin>>no;
if(no>1 && no<11)
flag=false;
else
cout<<\"\ Invalid Input!!\";
}while(flag);
//declare question array
string questions[no];
getQuestions(questions,no);
writeToFile(questions,no,fname);
return 0;
}
Sample Output:
Enter file name: questions.txt
Enter no of questions: 3
Enter question 0:
Operator used to resolve the scope of global variable
Enter question 1:
Function which is called automatically as soon as object gets created is
Enter question 2:
The code generated by compiler is

