Hlo all!
I am in a bit of a jam with my C++ project for school. I am making a very simple program to randomly ask a 25 questions from a file storing any number of questions.Now i have two problems.
A)The program runs fine but the file it writes for scores seems currupt and doesnt reset to the values i have set as default.
B)Secondly I dunno how to make the file storing the various questions.
Plus I am having problems writing the class to a file.Can someone please take a look at the code and tell me all the things I am doing wrong? :) - #include <conio.h>
-
#include <string.h>
-
#include <fstream.h>
-
#include <stdlib.h>
-
#include<stdio.h>
-
-
int noq=100;int qctr=0;
-
int qnchk[24]={-1,-1,-1,-1,-1,1,-1,-1,-1,-1,1,-1,-1,-1,-1,1,-1,-1,-1,-1,1,-1,-1,-1};
-
void quiz(char name[]);
-
int qrand(void);//To randomize questions
-
void ready(void);
-
void reset(void);
-
struct player{char name[];
-
int score;
-
}player;
-
class qs
-
{int abit;
-
char q[],a1[],a2[],a3[],a4[]; //Declare variables for question and answers
-
public:
-
void disp(void);//To display questions
-
void r_in(int x);//To read in values of answers,questions and flags
-
void check(int ch);//To check answer
-
int qno;
-
}Qstn;
-
int offset=sizeof(Qstn);
-
void qs::r_in(int x)
-
{
-
fstream file;
-
file.open("Gdata.txt",ios::out);
-
file.seekg((offset*x),ios::beg);
-
file.read((char*) &Qstn,sizeof(Qstn));
-
file.getline(q,"\t");
-
return;
-
}
-
-
int qrand(void)
-
{int a=-1,b=0;
-
do
-
{rerun:randomize();
-
a=rand()%noq+1;
-
for (int ctr=0;ctr<24;ctr++) { if(qnchk[ctr]==a) {b=1;}}
-
if (b==1)
-
goto rerun;
-
}while(a>noq || a<0);
-
return(a);
-
}
-
-
void qs::check(int ch)
-
{clrscr();
-
if (ch==abit)
-
{cout<<"\n\n\t\tRight Answer!\n";
-
player.score+=10;
-
}
-
else {cout<<"\n\n\t\tWrong Answer!\n";}
-
return;
-
}
-
-
void qs::disp(void)
-
{cout<<"\n\nQ."<<qctr<<") ";
-
puts(q);
-
cout<<"\n\n<1> ";
-
puts(a1);cout<<"\n\t<2> ";puts(a2);
-
cout<<"\n<3> ";
-
puts(a3);cout<<"\n\t<4> ";puts(a4);
-
cout<<"\n\nEnter Your Choice (1,2,3 or 4) ";
-
return;
-
}
-
-
void quiz(char name[])
-
{clrscr();int qvar,ch;struct player temp; player.score=0;
-
cout<<"\t\t\tQUIZMASTER\n\t\t\t----------\n\t\tSCORE: "<<player.score;
-
cout<<"\n\nReady ";
-
puts(name);
-
cout<<" (Y/N)?";
-
char choice; cin>>choice;
-
if (choice=='N' || choice=='n') {return;}
-
for (int i=0;i<25;i++)
-
{ch=0;
-
qvar=qrand();
-
Qstn.r_in(qvar);
-
qnchk[i]=Qstn.qno;
-
qctr=i+1;Qstn.disp();
-
cin>>ch;
-
Qstn.check(ch);
-
cout<<"\nNext Question (Y/N) ? ";
-
cin>>choice; if(choice=='N' || choice=='n') {cout<<"\nExiting...\n";break;}
-
}
-
fstream f1,f2; f1.open("Player.rec",ios::app|ios::out|ios::in);f2.open("temp.dat",ios::out|ios::trunc);i=1;
-
if(!f1)
-
{
-
cout<<"\nERROR : No file exists!\n";
-
return;
-
}
-
while(!f1.eof() && i<10)
-
{
-
f1.read((char*) &temp,sizeof(temp));
-
if(player.score<temp.score)
-
{
-
f2.write((char*)&temp,sizeof(temp)); i++;
-
}
-
if(player.score==temp.score)
-
{
-
f2.write((char*)&temp,sizeof(temp));
-
f2.write((char*)&player,sizeof(player));
-
i+=2;
-
}
-
if(player.score>temp.score)
-
{
-
f2.write((char*)&player,sizeof(player));
-
i++;
-
}
-
}
-
f1.close(); f2.close();
-
remove("Player.rec"); rename("temp.dat","Player.rec");
-
clrscr();
-
if(i<=10)
-
{cout<<"\n\n\t\tHI-SCORE!!!";
-
}
-
clrscr();
-
return;
-
}
-
-
2 1593
contd... - void main()
-
{
-
char opt;
-
ready();
-
while(opt!='E' || opt!='e')
-
{clrscr();char xb=' ';
-
gotoxy(30,7); cprintf("QUIZ MASTER");
-
gotoxy(29,8); cprintf("-------------");
-
gotoxy(31,10); cprintf("Main Menu");
-
gotoxy(30,11); cprintf("-----------");
-
gotoxy(34,14); cprintf("New");
-
gotoxy(32,16); cprintf("HISCORE");
-
gotoxy(29,18); cprintf("Instructions");
-
gotoxy(32,20); cprintf("E X I T");
-
gotoxy(0,17);
-
cout<<"\n\n\tSelect an option (N for new game,H for hiscores,I for\n";
-
cout<<"\tinstructions or E to exit ";
-
cin>>opt;
-
if(opt=='N' || opt=='n')
-
{
-
cout<<"\n\t\tEnter player name ";
-
gets(player.name);
-
quiz(player.name);
-
opt=' ';continue;
-
}
-
if(opt=='H' || opt=='h')
-
{clrscr(); opt=' ';
-
struct player t1;
-
gotoxy(32,10); cprintf("~HI SCORES~");
-
gotoxy(31,11); cprintf("-------------");
-
gotoxy(0,15);
-
ifstream f1;
-
f1.open("Player.rec",ios::in|ios::nocreate);
-
f1.seekg(0);cout<<"\tRank\tName\tScore\n";
-
for(int i=0;i<10;i++)
-
{
-
f1.read((char *) &t1,sizeof(t1));
-
cout<<"\t"<<i<<"\t"<<t1.name<<"\t"<<t1.score<<endl;
-
}
-
f1.close();
-
cout<<"\n\n\tPress R to reset hiscores or any other key to go back :";
-
cin>>xb;
-
if(xb=='R' || xb=='r')
-
{reset();
-
}
-
continue;
-
}
-
if(opt=='I' || opt=='i')
-
{
-
clrscr();
-
cout<<"\n\n\t\tINSTRUCTIONS\n\n\n";
-
cout<<"\tAnswer the questions by entering the number of the option you think\n";
-
cout<<"\tis correct.\n\n\n\nPress any key to return to main menu... ";
-
opt=' ';cin>>xb;
-
continue;
-
}
-
if(opt=='E' || opt=='e')
-
{clrscr();
-
cout<<"\n\n\n\n\tHave a nice day!";
-
break;
-
}
-
}
-
getch();
-
}
-
-
void ready(void)
-
{
-
fstream f1;
-
f1.open("Player.rec",ios::out|ios::nocreate);
-
if(f1.fail())
-
{f1.close(); f1.open("Player.rec",ios::in);
-
strcpy(player.name,"H4x0r");player.score=1;
-
for(int i=0;i<10;i++)
-
{f1.write((char*)&player,sizeof(player));
-
player.score*=i;
-
}
-
strcpy(player.name,"");player.score=0;
-
f1.close(); f1.open("Gdata.txt",ios::out|ios::in|ios::nocreate);f1.close();
-
}
-
return;
-
}
-
-
void reset(void)
-
{clrscr();
-
remove("Player.rec");
-
ready();
-
cout<<"Hiscores cleared!";
-
return;
-
}
Hi. That's a lot of code to work through.
In general:
If you want to save data like this you need to know the size of the record to write and read accurately so you need to write a long value representing the size of your struct. This is because you have a variable length string and you don't know how many bytes to read from the file.
You could also change to using fixed length strings: - struct player{
-
char name[50];
-
int score;
-
};
This makes life so much easier saving and writing to a binary file
Incidentally, I would refrain from giving the instance of my player struct the same name as the typedef. Not the best practise.
With regard to the questions. The same thing applies. I assume you will need to store an answer: - struct question {
-
char question[100];
-
char answer[100];
-
int CorrectChoice;
-
};
When saving or reading your array of questions you will need to store a long value at the beginning of the file to let you know how many questions there are so you can set the array to the correct length.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Rob Meade |
last post by:
Hi all,
Ok - I'm going around in circles and would appreciate some help.
I have written a windows service, which up until I tried to be clever was
working well :o)
I have a main class which...
|
by: Arjen |
last post by:
Hi,
I have a list up to 100 records. I can use the datagrid with the pager
functionality. But I can also use a repeater with a self made pager control.
What is the best and why?
I think the...
|
by: Ben Hallert |
last post by:
Hi!
I'm using the zip functions for the first time and I'm having an odd
problem. I may be doing something boneheaded, here's hoping someone
here can recognize it.
I have a foreach loop that...
|
by: Frinavale |
last post by:
Hi there!
I don't know why I seem to be the only one with this problem...In Firefox when I'm displaying a table within a <div> and use the align="center" it does not center the table. I'm...
|
by: uzairahm |
last post by:
Hello Guys,
I have recently installed MS SQL Server 2005 On Microsoft XP. Before Installation there wre no Messages in system compatablity check. But after installation it hasn't created any...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |