Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Read Text Question File Please change this programming to C++ code #include <ios

ID: 3778642 • Letter: R

Question

Read Text Question File

Please change this programming to C++ code

#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
//using namespace std;
void main()
{
int quest_count,i=0;
char filename[20],quest[100],quest2[100],ch;
fstream file;
clrscr();
cout<<" Enter File Name : ";
gets(filename);
file.open(filename,ios::out);
if(file.fail())
{
cout<<" file failed to open ";
getch();
}
else
{
abc:
cout<<" Enter number of Question you want ot write : ";
cin>>quest_count;
if(quest_count>1&&quest_count<11)
{
while (i<quest_count)
{
cout<<" Enter Question "<< i+1<<" : ";
gets(quest);
file<<quest<<" ";
i++;
}
}
else
{
cout<<" Enter correct number!!";
goto abc;
}
cout<<" ----Data Written into file---- ";
}
file.close();
cout<<" ----Reading Data from file---- ";
file.open(filename,ios::in);
if(file.fail())
{
cout<<" file failed to open ";
getch();
}
else
{
i=1;
int j=0;
while (file.get(ch))
{
if(ch==' ')
{
cout<<" Question "<<i<<" : "<<quest2;
i++;
for(int k=0;k<j;k++)
quest2[k]='';
j=0;
}
else
{
quest2[j]=ch;
j++;
}
}
}
file.close();
getch();
}

Explanation / Answer

Here is the successfully compiled C++ version for you:

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
int quest_count,i=0;
char quest[100],quest2[100],ch;
string filename;
fstream file;
cout<<" Enter File Name : ";
cin>>filename;
file.open(filename,ios::out);
if(file.fail())
{
cout<<" file failed to open ";
return 0;
}
else
{
abc:
cout<<" Enter number of Question you want ot write : ";
cin>>quest_count;
if(quest_count>1&&quest_count<11)
{
while (i<quest_count)
{
cout<<" Enter Question "<< i+1<<" : ";
gets(quest);
file<<quest<<" ";
i++;
}
}
else
{
cout<<" Enter correct number!!";
goto abc;
}
cout<<" ----Data Written into file---- ";
}
file.close();
cout<<" ----Reading Data from file---- ";
file.open(filename,ios::in);
if(file.fail())
{
cout<<" file failed to open ";
}
else
{
i=1;
int j=0;
while (file.get(ch))
{
if(ch==' ')
{
cout<<" Question "<<i<<" : "<<quest2;
i++;
for(int k=0;k<j;k++)
quest2[k]='';
j=0;
}
else
{
quest2[j]=ch;
j++;
}
}
}
file.close();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote