Error in Output !

This is a program for a quiz competition.
The program seems to run fine but when I try to display the result it displays junk. Please HELP !!

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int no=1;
class quizz
{
public:
char question[100];
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;
float score;
public:

void add()
{
sno=0;
cout<<"Enter your name :"<<endl;
gets(name);
score=0;
}
void assign(int k)
{
sno=k;
}
void show()
{
cout<<sno<<" "<<name<<"\t"<<score<<endl;
}

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

int retsno()
{
return sno;
}
float retscore()
{
return score;
}
char* retname()
{
return name;
}
};

void main()
{
system("cls");
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 display reults"<<endl;
cout<<"Press 5 to exit"<<endl;
cin>>choice;

switch(choice)
{
case 1:
{
participant p; fstream f;
f.open("participant.dat",ios::out|ios::ate|ios::binary);
p.add();p.assign(no); no++;
f.write((char*)&p,sizeof(p));
cout<<"Your are enrolled succesfully."<<endl;
cout<<"Your participant number is "<<p.retsno()<<endl;
f.close();
break;
}
case 2:
{
participant p,r;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.retsno()==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(int i=0;i<3;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*)&r,sizeof(r));
if((strcmp(r.retname(),p.retname())==0)&&r.retsno()==p.retsno())
{
f1.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::out|ios::ate|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:
{
fstream f;participant o,m;float max=0;
f.open("particpant.dat",ios::in|ios::binary);
while(f.read((char*)&o,sizeof(o)))
{
o.show();
if(o.retscore()>max)
{
m=o;
max=o.retscore();
}
}
f.close();
cout<<"The winner of the quiz is..."<<endl;
m.show();
break;
}
case 5:
{
exit(0);
}
default:
break;
}
cout<<"Do you want to try again ? (Y/N)";
cin>>ch;
}
}
Next time, use the [code][/code] tags (the <> icon)

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int no = 1;
class quizz {
public:
  char question[100];
  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;
  float score;
public:

  void add() {
    sno = 0;
    cout << "Enter your name :" << endl;
    gets(name);
    score = 0;
  }
  void assign(int k) {
    sno = k;
  }
  void show() {
    cout << sno << " " << name << "\t" << score << endl;
  }
  void calc(float s) {
    score = s;
  }
  int retsno() {
    return sno;
  }
  float retscore() {
    return score;
  }
  char *retname() {
    return name;
  }
};

void main()
{
  system("cls");
  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 display reults" << endl;
    cout << "Press 5 to exit" << endl;
    cin >> choice;
    switch (choice) {
    case 1:
      {
        participant p;
        fstream f;
        f.open("participant.dat", ios::out | ios::ate | ios::binary);
        p.add();
        p.assign(no);
        no++;
        f.write((char *) &p, sizeof(p));
        cout << "Your are enrolled succesfully." << endl;
        cout << "Your participant number is " << p.retsno() << endl;
        f.close();
        break;
      }
    case 2:
      {
        participant p, r;
        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.retsno() == 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 (int i = 0; i < 3; 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 *) &r, sizeof(r));
            if ((strcmp(r.retname(), p.retname()) == 0)
                && r.retsno() == p.retsno()) {
              f1.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::out | ios::ate | 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:
      {
        fstream f;
        participant o, m;
        float max = 0;
        f.open("particpant.dat", ios::in | ios::binary);
        while (f.read((char *) &o, sizeof(o))) {
          o.show();
          if (o.retscore() > max) {
            m = o;
            max = o.retscore();
          }
        }
        f.close();
        cout << "The winner of the quiz is..." << endl;
        m.show();
        break;
      }
    case 5:
      {
        exit(0);
      }
    default:
      break;
    }
    cout << "Do you want to try again ? (Y/N)";
    cin >> ch;
  }
}


> The program seems to run fine but when I try to display the result it displays junk.
You need to say what combination of 1..5 menu options you chose, and what you typed in to create records.

You also need to delete any existing .dat files if you've been messing about with the layout of your structures.

Also, don't forget all your obsolescence problems!
http://www.cplusplus.com/forum/general/248648/

> Also, don't forget all your obsolescence problems!
http://www.cplusplus.com/forum/general/248648/

Yea, i fixed those errors.

> You need to say what combination of 1..5 menu options you chose, and what you typed in to create records.

So i have copied the output screen of what typed to create records and the combination from the menu.


Press 1 to Enroll for quiz.
Press 2 to take up the quiz                                                     
Press 3 to input a question into database                                       
Press 4 to display reults                                                       
Press 5 to exit                                                                 
3                                                                               
Enter the question                                                              
From which language is the word 'ketchup' derived ?                             
Enter the answer                                                                
chinese                                                                         
Do you want to input more questions ? (Y/N)y                                    
Enter the question                                                              
which is the country with the biggest population in Europe ?                    
Enter the answer                                                                
russia                                                                          
Do you want to input more questions ? (Y/N)y                                    
Enter the question                                                              
who portrayed Edward Scissorhands ?                                             
Enter the answer                                                                
johnny depp                                                                     
Do you want to input more questions ? (Y/N)n                                    
Do you want to try again ? (Y/N)n                                               
                                                                                
                                             
                                       
//I closed the program and ran it again.


Press 1 to Enroll for quiz.
Press 2 to take up the quiz                                                     
Press 3 to input a question into database                                       
Press 4 to display reults                                                       
Press 5 to exit                                                                 
1                                                                               
Enter your name   :                                                             
Jack                                                                            
Your are enrolled succesfully.                                                  
Your participant number is 1                                                    
Do you want to try again ? (Y/N)n                                               
                                                                                

//I closed the program and ran it again
                                                                                
                        
Press 1 to Enroll for quiz.
Press 2 to take up the quiz                                                     
Press 3 to input a question into database                                       
Press 4 to display reults                                                       
Press 5 to exit                                                                 
2                                                                               
Enter your participant number ! 1                                               
Enter your name ! Jack                                                          
Quiz has begun.                                                                 
1. For every correct answer you will get 4 marks                                
2. For every incorrect answer you will lose 1 mark                              
Please answer the following questions.                                          
From which language is the word 'ketchup' derived ?                             
chinese                                                                         
Which is the country with the biggest population in Europe ?                    
russia                                                                          
Who portrayed Edward Scissorhands ?                                             
johnny depp                                                                     
Do you want to try again ? (Y/N)n     

//I closed the program and ran it again

Press 1 to Enroll for quiz.
Press 2 to take up the quiz                                                     
Press 3 to input a question into database                                       
Press 4 to display reults                                                       
Press 5 to exit                                                                 
4                                                                               
The winner of the quiz is...                                                    
8308   orhands ?        3.04788e+32                                             
Do you want to try again ? (Y/N)n                                               
                                                                        



The final output where the winner of the quiz announced displays trash.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    case 4:
      {
        fstream f;
        participant o, m;
        float max = 0;
        f.open("particpant.dat", ios::in | ios::binary);
        while (f.read((char *) &o, sizeof(o))) {
          o.show();
          if (o.retscore() > max) {
            m = o;
            max = o.retscore();
          }
        }
        f.close();
        cout << "The winner of the quiz is..." << endl;
        m.show();
        break;
      }

Because the first thing you see is "The winner....", perhaps investigate why o.show() is never called.
Which in turn might explain why m = o; is never called.
Which might in turn explain why m.show() shows garbage.

Actually, the garbage thing would be resolved if your structs had a proper constructor to initialise the member variables to something you would recognise as "uninitialised".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class quizz {
public:
  char question[100];
  char answer[10];
  quizz() {
    strcpy(question,"question?");
    strcpy(answer,"dunno?");
  }
};

class participant {
  char name[10];
  int sno;
  float score;
public:
  participant() {
    strcpy(name,"nobody");
    sno = -1;
    score = -1;
  }
};


Topic archived. No new replies allowed.