C++ ERROR - Crashes upon compilation

Please help me I did a program on participating in quiz competition.
Upon compilation the program crashes.
The source code:



#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int no=1;
class quizz
{
public:
char question[20];
char answer[10];
void get()
{
cout<<"Enter the question"<<endl;
gets(question);
cout<<"Enter the answer"<<endl;
gets(answer);
}
};

class participant
{
char name[10];
int sno;
public:
int score;

void add()
{
sno=no++;
cout<<"Enter your name :"<<endl;
gets(name);
score=0;
}

void show()
{
cout<<sno<<" "<<name<<"\t"<<"\t"<<score<<endl;
}

void calc(float s)
{
score=s;
}

int retsno()
{
return sno;
}

char* retname()
{
return name;
}
};

void main()
{
clrscr();
char ch='Y'; int choice;
while(ch=='Y'||ch=='y')
{
cout<<"Press 1 to Enroll for quiz."<<endl;
cout<<"Press 2 to take up the quiz"<<endl;
cout<<"Press 3 to input a question into database";<<endl;
cout<<"Press 4 to exit"<<endl;
cin>>choice;

switch(choice)
{
case 1:
{
product p; fstream f;
f.open("list.dat",ios::out|ios::ate|ios::binary);
p.add();
f.write((char*)&p,sizeof(p));
cout<<"Your are enrolled succesfully."<<endl;
cout<<"Your participant number is "<<p.sno;
f.close();
break;
}
case 2:
{
participant p;quizz q; char na[25];float s=0;
fstream f,f1;char ques[30],ans[10];int n,flag=0;
f.open("quiz.dat",ios::in|ios::binary);
f1.open("participant.dat",ios::in|ios::binary);
cout<<"Enter your participant number ! "; cin>>n;
cout<<"Enter your name ! "; gets(na);
while(!f.eof())
{
f1.read((char*)&p,sizeof(p));
if(strcmp(na,p.retname();)==0)&&p.sno==n)
{
flag=1;
break;
}
}
f1.close();
if(flag==1)
{
cout<<"Quiz has begun."<<endl<<"1. For every correct answer you will get 4 marks"<<endl;
cout<<"2. For every incorrect answer you will lose 1 mark"<<endl;
cout<<"Please answer the following questions."<<endl;
for(i=0;i<n;i++)
{
f.read((char*)&q,sizeof(q));
cout<<q.question<<endl;
gets(ans);
if(strcmp(q.answer,ans)==0)
s+=4;
else
s-=1;
}
f.close();
p.calc(s);
f1.open("participant.dat",ios::in|ios::out|ios::binary);
while(!f.eof())
{
f1.read((char*)&q,sizeof(q));
if(strcmp(q.retname();,p.retname();)==0)&&q.sno==p.sno)
{
seekg(-sizeof(p),ios::cur);
f1.write((char*)&p,sizeof(p));
break;
}
}
f1.close();
break;
}
case 3:
{char ch;
do
{
fstream f;quizz q;
f.open("quiz.dat",ios::app|ios::binary);
q.get;
f.write((char*)&q,sizeof(q));
f.close();
cout<<"Do you want to input more questions ? (Y/N)";
cin>>ch;
}
while(ch=="y"||ch=="Y")
break;
}
case 4:
{
exit(0);
}
default:
break;
}
cout<<"Do you want to try again ? (Y/N)";
cin>>ch;
}
}
You need to throw away your book and your compiler.
Whatever it is, it's been obsolete for at least 20 years.

> if(strcmp(na,p.retname();)==0)&&p.sno==n)
What's that ; right after retname() ?
Not to mention the unbalanced ( )

You're also missing a final closing brace for main as well.

> Upon compilation the program crashes.
Probably because most online quizzes use modern compilers which expect a better class of program.

Watch the //!! comments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//!! The modern style would be #include <fstream>
#include<fstream.h>
//!! The modern sytle for C header files is to drop the .h and prefix
//!! the name with c.  So string.h becomes cstring, stdio.h becomes cstdio
#include<string.h>
#include<conio.h>   //!! remove this, it's a DOS anachronism from years gone by
#include<stdio.h>
#include<stdlib.h>
int no = 1;

class quizz {
public:
  //!! why are these public?
  char question[20];
  char answer[10];
  void get() {
    cout << "Enter the question" << endl;
    //!! NEVER EVER USE gets()
    //!! There is NO protection at all against buffer overflow
    gets(question);
    cout << "Enter the answer" << endl;
    gets(answer);   //!! see above
  }
};

class participant {
  char name[10];
  int sno;
public:
  //!! why is this public?
  int score;

  void add() {
    sno = no++;
    cout << "Enter your name :" << endl;
    gets(name);   //!! see above
    score = 0;
  }
  void show() {
    cout << sno << " " << name << "\t" << "\t" << score << endl;
  }
  void calc(float s) {
    score = s;
  }
  int retsno() {
    return sno;
  }
  char *retname() {
    return name;
  }
};

//!! main returns an int.
//!! modern compilers tend to enforce this
void main()
{
  clrscr(); //!! not appropriate for online competitions
  char ch = 'Y';
  int choice;
  while (ch == 'Y' || ch == 'y') {
    cout << "Press 1 to Enroll for quiz." << endl;
    cout << "Press 2 to take up the quiz" << endl;
    cout << "Press 3 to input a question into database";
    <<endl;
    cout << "Press 4 to exit" << endl;
    cin >> choice;

    switch (choice) {
    case 1:
      {
        product p;  //!! where is this declared (oh, you meant participant)
        fstream f;
        f.open("list.dat", ios::out | ios::ate | ios::binary);
        p.add();
        f.write((char *) &p, sizeof(p));
        cout << "Your are enrolled succesfully." << endl;
        cout << "Your participant number is " << p.sno; //!! .sno is not public
        f.close();
        break;
      }
    case 2:
      {
        participant p;
        quizz q;
        char na[25];
        float s = 0;
        fstream f, f1;
        char ques[30], ans[10];
        int n, flag = 0;
        f.open("quiz.dat", ios::in | ios::binary);
        f1.open("participant.dat", ios::in | ios::binary);
        cout << "Enter your participant number ! ";
        cin >> n;
        cout << "Enter your name ! ";
        gets(na);   //!! see above
        while (!f.eof()) {
          f1.read((char *) &p, sizeof(p));
          if (strcmp(na, p.retname()) == 0 && p.sno == n) {
            flag = 1;
            break;
          }
        }
        f1.close();
        if (flag == 1) {
          cout << "Quiz has begun." << endl <<
              "1. For every correct answer you will get 4 marks" << endl;
          cout << "2. For every incorrect answer you will lose 1 mark" << endl;
          cout << "Please answer the following questions." << endl;
          for (i = 0; i < n; i++) {
            f.read((char *) &q, sizeof(q));
            cout << q.question << endl;
            gets(ans);   //!! see above
            if (strcmp(q.answer, ans) == 0)
              s += 4;
            else
              s -= 1;
          }
          f.close();
          p.calc(s);
          f1.open("participant.dat", ios::in | ios::out | ios::binary);
          while (!f.eof()) {
            f1.read((char *) &q, sizeof(q));
            if (strcmp(q.retname();, p.retname()) == 0 && q.sno == p.sno) {
              seekg(-sizeof(p), ios::cur);
              f1.write((char *) &p, sizeof(p));
              break;
            }
          }
          f1.close();
          break;
        }
    case 3:
        {
          char ch;
          do {
            fstream f;
            quizz q;
            f.open("quiz.dat", ios::app | ios::binary);
            q.get;
            f.write((char *) &q, sizeof(q));
            f.close();
            cout << "Do you want to input more questions ? (Y/N)";
            cin >> ch;
          }
          while (ch == "y" || ch == "Y")
          break;
        }
    case 4:
        {
          exit(0);
        }
    default:
        break;
      }
      cout << "Do you want to try again ? (Y/N)";
      cin >> ch;
    }
  }
}

There's more to fix beyond those.
Topic archived. No new replies allowed.