Need to create a binary file to diaplay questions ?

I am doing a project on quiz i need to create a class called test with char array to store questions and display them 1 by 1 in neat format with help of a binary file this is a multiple choice question type quiz program. please help with code

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>

class student
{ char school[15];
char name[20];
int nooftimes;
int score[20];
public:
student();
void input();
void update(int b);
int retscore();
char*retname()
{return name;}
} a,d;
student::student()
{nooftimes=0;
}
void student::update(int b)
{score[nooftimes]=b;
nooftimes+=1;
}
void student::input()
{
cout<<" Enter your name \n";
gets(name);
cout<<" Enter your school's name\n";
gets(school);
}
void main()
{
clrscr();
int x=0;
ifstream f1("text1.txt");
ifstream f2("text2.txt");
ifstream f3("text3.txt");
ifstream f4("text4.txt");
ifstream f5("text5.txt");
fstream f6("file.dat");
gotoxy(0,10);
char ch2,ch,ch1,c[20],choice,name;
cout<<" CCCC H H EEE MMMM MMMM III SSSSS TTTT RRR YY YY"<<'\n';
cout<<" C H H E M MM MM M I SS T R R YY YY"<<'\n';
cout<<" C H H E M MM MM M I SS T R R YY YY"<<'\n';
cout<<" C HHHHHH EEE M M M I SSSSS T RRR YY YY "<<'\n';
cout<<" C H H E M M I SS T R R YY"<<'\n';
cout<<" C H H E M M I SS T R R YY "<<'\n';
cout<<" CCCC H H EEE M M III SSSSS T R R YY" <<'\n';
cout<<'\n';
cout<<"if already a member,enter y ";
cin>>choice;
if(choice=='y')
{cout<<"enter your name";
cin>>name;
}
else
{
a.input();
f6.write((char*)&a,sizeof(a));
}
do{
cout<<"welcome to chemistry tutor"<<'\n';
cout<<"some basic concepts of chemistry"<<'\n';
cout<<"1.IMPORTANCE OF CHEMISTRY"<<'\n';
cout<<"2. NATURE OF MATTER"<<'\n';
cout<<"3. PROPERTIES OF MATTER AND THEIR MEASUREMENT"<<'\n';
cout<<"4. LAWS OF CHEMICAL COMBINATIONS"<<'\n';
cout<<"5. DALTON’S ATOMIC THEORY"<<'\n';
cout<<"6.Test based on the topics"<<'\n';
cout<<"enter number of the topic which you want to see"<<"\n\n\n";
getch();
cin>>ch;
if (ch=='1')
{
if(f1)
{
while(f1>>c)
{
cout<<c<<' ';
}
}
else
cout<<"file not opened";}
else if(ch=='2')
{
if(f2)
{
while(f2>>c)
{
cout<<c<<' ';
}
}
else
cout<<"file not opened";
}
else if (ch=='3')
{
if(f3)
{
while(f3>>c)
{
cout<<c<<' ';
}
}
else
cout<<"file not opened";
}
else if (ch=='4')
{
if(f4)
{
while(f4>>c)
{
cout<<c<<' ';
}
}
else
cout<<"file not opened";
}
else if (ch=='5')
{
if(f5)
{
while(f5>>c)
{
cout<<c<<' ';
}
}
else
cout<<"file not opened";
}
else if (ch=='6')
{
gotoxy(15,10);
clrscr();
gotoxy(15,10);cout<<"Welcome To The Chemistry test.";
gotoxy(15,5);cout<<"Enter answer in form of 'a','b' and'c'only.";
gotoxy(15,10);cout<< "1.What is the SI unit of density? " <<'\n'<<"a.g /m 3"<<'\n'<<"b.kg /cm3"<<'\n'<<"c.kg /m -3"<<'\n';
cin>>ch2;
clrscr();
if(ch2=='c')
{
x=x+10;
gotoxy(15,10);cout<<"Good Job.Your score is "<<x;
}
else
{
gotoxy(15,10);
cout<<"Sorry wrong answer.";}
getch();
clrscr();
gotoxy(15,10);
cout<<" 2.Which scientific notation represents the number 25380.305?"<<'\n'<<"a.2.5380305 × 10 4"<<'\n'<<"b.25.380305 × 10 4"<<'\n'<<"c.2.5380305 × 10 -4"<<'\n';
cin>>ch2;
clrscr();
if(ch2=='a')
{
x=x+10;
gotoxy(15,10);
cout<<"Good Job.Your score is "<<x;
}
else
{
gotoxy(15,10);
cout<<"Sorry wrong answer.";
}
getch();
clrscr();
gotoxy(15,10) ;cout<<" 3.Which number is represented by the scientific notation 3.709 × 10 -3?"<<'\n'<<"a.0.0003709"<<'\n';
cout<<"b.0.003709"<<'\n'<<"c.3709"<<'\n';
cin>>ch2;
clrscr();
if(ch2=='c')
{
x=x+10;
gotoxy(15,10);
cout<<"Good Job.Your score is "<<x;
}
else
{
gotoxy(15,10);
cout<<"Sorry wrong answer.";
}
getch();
clrscr();
gotoxy(15,10);
cout<<" 4.Which law talks about combination of elements in definite proportion"<<'\n'<<"a.Avogadro’s law"<<'\n'<<"b.Gay Lussac’s law"<<'\n'<<"c.Law of definite proportions"<<'\n';
cin>>ch2;
clrscr();
if(ch2=='a')
{
x=x+10;
gotoxy(15,10);
cout<<"Your score is "<<x;
}
else
{
gotoxy(15,10);
cout<<"Sorry wrong answer.";
}
getch();
clrscr();
gotoxy(15,10);
cout<<" 5.The empirical formula of a compound containing 7.74% hydrogen and 92.26% carbon is"<<'\n'<<"a.CH"<<'\n'<<"b.C2H"<<'\n'<<"c.CH2"<<'\n';
cin>>ch2;
clrscr();
if(ch2=='a')
{
x=x+10;
gotoxy(15,10);
cout<<"Your score is "<<x;
}
else
{
gotoxy(15,10);
cout<<"Sorry wrong answer.";
}
getch();
clrscr();
gotoxy(15,10);
cout<<"6.How many grams of water are produced on the combustion of 24 g of methane?"<<'\n'<<"a.18 g"<<'\n'<<"b.24 g"<<'\n'<<"c.27 g"<<'\n';
cin>>ch2;
clrscr();
if(ch2=='c')
{
x=x+10;
cout<<"Your score is "<<x;
}
else
{
gotoxy(15,10);
cout<<"Sorry wrong answer.";
}
getch();
clrscr();
gotoxy(15,10);
cout<<" 7.How many grams of NaCl are produced from 50 mL of 0.5 M NaOH?"<<'\n'<<"a.1.46 g"<<'\n'<<"b.2.92 g"<<'\n'<<"c.5.84 g"<<'\n';
cin>>ch2;
clrscr();
if(ch2=='c')
{
x=x+10;
gotoxy(15,10);
cout<<"Your score is "<<x;
}
else
{
gotoxy(15,10);
cout<<"Sorry wrong answer.";
}
getch();
clrscr();
gotoxy(15,10);
cout<<"8.The molarity of a solution of NaCl in water is 2 M. If density of the solution is 0.8 g m/L , then what is the mole fraction of NaCl?"<<'\n'<<"a.0.01"<<'\n'<<"b.0.03"<<'\n'<<"c.0.05"<<'\n';
cin>>ch2;
clrscr();
if(ch2=='a')
{
x=x+10;
gotoxy(15,10);
cout<<"Your score is "<<x;
}
else
{
gotoxy(15,10);
cout<<"Sorry wrong answer.";
}
getch();
clrscr();
gotoxy(15,10);
cout<<" 9.The addition of numbers 2.09 × 10 3, 4.5 × 10 4, and 3.71 × 10 5 gives"<<'\n'<<"a.4.1809 × 10 7"<<'\n'<<"b.4.1809 × 10 5"<<'\n'
<<"c.4.1809 × 10 4"<<'\n';
cin>>ch2;
clrscr();
if(ch2=='b')
{
x=x+10;
gotoxy(15,10);
cout<<"Your score is "<<x;
}
else
{
gotoxy(15,10);
cout<<"Sorry wrong answer.";
}
getch();
clrscr();
gotoxy(15,10);
cout<<" 10.The addition of the numbers14.2, 9.02, 8.310, and 19.1452 rounded off to first decimal place gives"<<'\n'<<"a.50.7"<<'\n'<<"b.50.68"<<'\n'<<"c.50.675"<<'\n';
cin>>ch2;
clrscr();
if(ch2=='a')
{
x=x+10;
gotoxy(15,10);
cout<<"Your score is "<<x;
}
else
{
gotoxy(15,10);
cout<<"Sorry wrong answer.";
}
getch();
clrscr();
if(x==100)
{
gotoxy(15,10);
cout<<"great...keep it up.";}
else if(x==90)
{
gotoxy(15,10);
cout<<"wow,... Your Score is 90!!";}
else if(x==80)
{gotoxy(15,10);
cout<<" Your Score is 80....you can improve"; }
else if(50==x||x==70||x==60)
{gotoxy(15,10);
cout<<"average"<<'\n';
cout<<"Your Score is "<<x<<"Better luck next time";}
if(x<=40)
{
gotoxy(15,10);
cout<<"need to work hard";}
getch();
}
cout<<"\n\ndo you want to continue";
cin>>ch1;
}
while(ch1=='y');
while(f6.read((char*)&d,sizeof(d)))
{
if(strcmpi(name,d.retname())==0)
{
d.update(x);
f6.write((char*)&d,sizeof(d));
}
}
}



PLEASE HELP !!!
This is some horrific code - do you realise that you're allowed to use more than one letter when naming variables? The idea of variable names is that you give them a name so that you know what the variable is for by reading the variable name - in your code, that's impossible).

Anyway, what's your question? We're not going to do your homework for you. Do you want us to just tell you everything bad in your code, or do you have a specific question?

Out of interest, are you using a twenty year old copy of Turbo C++, or a 15 year old copy of Dev-Cpp?
Last edited on
And there is my omnipresent request to
edit your post so that it uses code tags - the <> button on the right
and always post compiler output.

That way we can see all the bad bits in colour with indenting.

We look forward to helping out - cheers :)
Topic archived. No new replies allowed.