473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

whooops

Techshrimp
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? :)


Expand|Select|Wrap|Line Numbers
  1. #include <conio.h>
  2. #include <string.h>
  3. #include <fstream.h>
  4. #include <stdlib.h>
  5. #include<stdio.h>
  6.  
  7.     int noq=100;int qctr=0;
  8.     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};
  9.     void quiz(char name[]);
  10.     int qrand(void);//To randomize questions
  11.     void ready(void);
  12.     void reset(void);
  13.     struct player{char name[];
  14.               int score;
  15.              }player;
  16.     class qs
  17.         {int abit;
  18.          char q[],a1[],a2[],a3[],a4[]; //Declare variables for  question and answers
  19.          public:
  20.          void disp(void);//To display questions
  21.          void r_in(int x);//To read in values of answers,questions and flags
  22.          void check(int ch);//To check answer
  23.          int qno;
  24.         }Qstn;
  25.     int offset=sizeof(Qstn);
  26.     void qs::r_in(int x)
  27.         {
  28.           fstream file;
  29.           file.open("Gdata.txt",ios::out);
  30.           file.seekg((offset*x),ios::beg);
  31.           file.read((char*) &Qstn,sizeof(Qstn));
  32.           file.getline(q,"\t");
  33.           return;
  34.         }
  35.  
  36.     int qrand(void)
  37.         {int a=-1,b=0;
  38.          do
  39.          {rerun:randomize();
  40.           a=rand()%noq+1;
  41.           for (int ctr=0;ctr<24;ctr++) { if(qnchk[ctr]==a) {b=1;}}
  42.           if (b==1)
  43.           goto rerun;
  44.          }while(a>noq || a<0);
  45.          return(a);
  46.         }
  47.  
  48.     void qs::check(int ch)
  49.         {clrscr();
  50.          if (ch==abit)
  51.          {cout<<"\n\n\t\tRight Answer!\n";
  52.           player.score+=10;
  53.          }
  54.           else {cout<<"\n\n\t\tWrong Answer!\n";}
  55.           return;
  56.         }
  57.  
  58.     void qs::disp(void)
  59.         {cout<<"\n\nQ."<<qctr<<") ";
  60.          puts(q);
  61.          cout<<"\n\n<1> ";
  62.          puts(a1);cout<<"\n\t<2> ";puts(a2);
  63.          cout<<"\n<3> ";
  64.          puts(a3);cout<<"\n\t<4> ";puts(a4);
  65.          cout<<"\n\nEnter Your Choice (1,2,3 or 4) ";
  66.          return;
  67.         }
  68.  
  69.     void quiz(char name[])
  70.         {clrscr();int qvar,ch;struct player temp; player.score=0;
  71.          cout<<"\t\t\tQUIZMASTER\n\t\t\t----------\n\t\tSCORE: "<<player.score;
  72.          cout<<"\n\nReady ";
  73.          puts(name);
  74.          cout<<" (Y/N)?";
  75.          char choice;    cin>>choice;
  76.          if (choice=='N' || choice=='n') {return;}
  77.          for (int i=0;i<25;i++)
  78.          {ch=0;
  79.           qvar=qrand();
  80.           Qstn.r_in(qvar);
  81.           qnchk[i]=Qstn.qno;
  82.           qctr=i+1;Qstn.disp();
  83.           cin>>ch;
  84.           Qstn.check(ch);
  85.           cout<<"\nNext Question (Y/N) ? ";
  86.           cin>>choice; if(choice=='N' || choice=='n') {cout<<"\nExiting...\n";break;}
  87.          }
  88.          fstream f1,f2; f1.open("Player.rec",ios::app|ios::out|ios::in);f2.open("temp.dat",ios::out|ios::trunc);i=1;
  89.          if(!f1)
  90.          {
  91.           cout<<"\nERROR : No file exists!\n";
  92.           return;
  93.          }
  94.          while(!f1.eof() && i<10)
  95.          {
  96.           f1.read((char*) &temp,sizeof(temp));
  97.           if(player.score<temp.score)
  98.           {
  99.            f2.write((char*)&temp,sizeof(temp)); i++;
  100.           }
  101.           if(player.score==temp.score)
  102.            {
  103.             f2.write((char*)&temp,sizeof(temp));
  104.             f2.write((char*)&player,sizeof(player));
  105.             i+=2;
  106.            }
  107.           if(player.score>temp.score)
  108.            {
  109.             f2.write((char*)&player,sizeof(player));
  110.             i++;
  111.            }
  112.          }
  113.           f1.close(); f2.close();
  114.           remove("Player.rec"); rename("temp.dat","Player.rec");
  115.           clrscr();
  116.           if(i<=10)
  117.           {cout<<"\n\n\t\tHI-SCORE!!!";
  118.           }
  119.           clrscr();
  120.           return;
  121.         }
  122.  
  123.  
Jan 26 '07 #1
2 1593
contd...
Expand|Select|Wrap|Line Numbers
  1. void main()
  2.         {
  3.          char opt;
  4.          ready();
  5.          while(opt!='E' || opt!='e')
  6.          {clrscr();char xb=' ';
  7.           gotoxy(30,7);    cprintf("QUIZ MASTER");
  8.           gotoxy(29,8);    cprintf("-------------");
  9.           gotoxy(31,10);    cprintf("Main Menu");
  10.           gotoxy(30,11);    cprintf("-----------");
  11.           gotoxy(34,14);    cprintf("New");
  12.           gotoxy(32,16);    cprintf("HISCORE");
  13.           gotoxy(29,18);    cprintf("Instructions");
  14.           gotoxy(32,20);    cprintf("E X I T");
  15.           gotoxy(0,17);
  16.           cout<<"\n\n\tSelect an option (N for new game,H for hiscores,I for\n";
  17.           cout<<"\tinstructions or E to exit ";
  18.           cin>>opt;
  19.           if(opt=='N' || opt=='n')
  20.            {
  21.             cout<<"\n\t\tEnter player name ";
  22.             gets(player.name);
  23.             quiz(player.name);
  24.             opt=' ';continue;
  25.            }
  26.           if(opt=='H' || opt=='h')
  27.            {clrscr();    opt=' ';
  28.             struct player t1;
  29.             gotoxy(32,10);    cprintf("~HI SCORES~");
  30.             gotoxy(31,11);    cprintf("-------------");
  31.             gotoxy(0,15);
  32.             ifstream f1;
  33.             f1.open("Player.rec",ios::in|ios::nocreate);
  34.             f1.seekg(0);cout<<"\tRank\tName\tScore\n";
  35.             for(int i=0;i<10;i++)
  36.             {
  37.              f1.read((char *) &t1,sizeof(t1));
  38.              cout<<"\t"<<i<<"\t"<<t1.name<<"\t"<<t1.score<<endl;
  39.             }
  40.             f1.close();
  41.             cout<<"\n\n\tPress R to reset hiscores or any other key to go back :";
  42.             cin>>xb;
  43.             if(xb=='R' || xb=='r')
  44.              {reset();
  45.              }
  46.             continue;
  47.            }
  48.           if(opt=='I' || opt=='i')
  49.            {
  50.             clrscr();
  51.             cout<<"\n\n\t\tINSTRUCTIONS\n\n\n";
  52.             cout<<"\tAnswer the questions by entering the number of the option you think\n";
  53.             cout<<"\tis correct.\n\n\n\nPress any key to return to main menu... ";
  54.             opt=' ';cin>>xb;
  55.             continue;
  56.            }
  57.           if(opt=='E' || opt=='e')
  58.            {clrscr();
  59.             cout<<"\n\n\n\n\tHave a nice day!";
  60.             break;
  61.            }
  62.          }
  63.          getch();
  64.         }
  65.  
  66.     void ready(void)
  67.         {
  68.          fstream f1;
  69.          f1.open("Player.rec",ios::out|ios::nocreate);
  70.          if(f1.fail())
  71.          {f1.close(); f1.open("Player.rec",ios::in);
  72.           strcpy(player.name,"H4x0r");player.score=1;
  73.           for(int i=0;i<10;i++)
  74.            {f1.write((char*)&player,sizeof(player));
  75.             player.score*=i;
  76.            }
  77.          strcpy(player.name,"");player.score=0;
  78.          f1.close(); f1.open("Gdata.txt",ios::out|ios::in|ios::nocreate);f1.close();
  79.          }
  80.          return;
  81.         }
  82.  
  83.     void reset(void)
  84.         {clrscr();
  85.          remove("Player.rec");
  86.          ready();
  87.          cout<<"Hiscores cleared!";
  88.          return;
  89.         }
Jan 26 '07 #2
willakawill
1,646 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. struct player{
  2.    char name[50];
  3.    int score;
  4. };
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:
Expand|Select|Wrap|Line Numbers
  1. struct question {
  2.    char question[100];
  3.    char answer[100];
  4.    int CorrectChoice;
  5. };
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.
Jan 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
2
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...
3
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...
7
Frinavale
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...
5
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...
0
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
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.