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

 class qa
{ char question[35];
  char option[4][20];
  int ans;    //correct option no (eg 0 1 2 3)

public:
   void getques() 
	{ gets(question);
      cout<<endl;
	}
		
		
   void getopts()
	{cin>>option[0]>>option[1]>>option[2]>>option[3];
	
	}

	
		
  void getans() 
	{ cin>>ans;
	}

	
};    



void main()
{ int n;
  qa q;
  fstream file1;
  file1.open("questions.dat",ios::out);
  
  cout<<"\n Enter the limit of questions " <<endl;
  cin>>n;
  
  
  for(int i=0;i<n;i++)
  { cout<<"Enter the question" <<endl;
	q.getques();
    cout<<"enter the four options " <<endl;
    q.getopts();
	cout<<"Enter the answer for the respective questions " <<endl;
    q.getans();
    file1.write((char*)&q,sizeof(q));
  }

  file1.close();

}

The above is a quiz program..
it executes perfectly ....the problem i am facing is that ..which entering the questions to the binary file ...when it asks for options ...and when i am entering the options ...
suppose one of the options is with 2 words.. like " jovial bone" ..then it takes "jovial " as 1 option and "bone" as another...
how do i make "jovial bone" as 1 option ???
please reply ASAP ...thanksss!!!

Recommended Answers

All 15 Replies

And please tell me in terms of C++ only!! and not in terms of C or java or anything else :P
thanx!!!

Did you actually mean to mark this 'solved'? You seem to mark it almost as soon as it was posted.

Also, as has been explained before, void main() is incorrect, despite the fact that the compiler says it is acceptable. Please use int main() in C++ programs, always.

well void main and int main doesnt make a difference..
the only thing is that if i use int main instead of void main,..then i wil have to write return() at the end of the main program.

well, now the problem that i was facing is solved...But i am FACING A NEW PROBLEM NOW :((

I started writing the questions to the binary file ...i wrote 2 questions after entering the limit 2 and the loop exited perfectly but when i am writing the limit as 30 and entering 30 questions....the loop is going to infinite...its not stoppinggg!!!!!!!! pleasee some1 help ! :((

and the only thing that i modified in the code is that...
i changed the getopts() function..as

void getopts()
   { gets(option[0]);
     cout<<endl;
	
    gets(option[1]);
     cout<<endl;
	
    gets(option[2]);
     cout<<endl;
	
	gets(option[3]);
     cout<<endl;
	 


	}

Urk, I had missed the use of gets() in the original, or I would have mentioned it... using gets() , under any circumstances, is a serious problem, as it doesn't check to make sure that the input will fit into the buffer it reads into. Using gets() is just asking for buffer overruns; in C++, you should use cin.getline() instead.

void getopts()
{
    for (int i = 0; i < 4; i++)
    {
        cin.getline(option[i], 20);
    }
}

BTW, your indentation style is, to be blunt, atrocious. Please be more careful about indentation, or else use an auto-formatter (VC++ 6.0 includes one, IIRC).

ok let me try that,
thanks mate!

heyyy........i tried that cin.getline mate..!
it stil doesnt wrk!
same problem...infinite loop....i entered the limit as 15..and it still goes on beyond 20...dont knw whats the prblem ....some1 help plss.:(((((

urgent.!! :(

(Good God, why does everybody here beat around the bush when somebody has a problem, just help the guy!)

What I would do in your situation is to monitor all the variables involved (you don't even need a debugger for this, just make it print your variables in a loop). If your loop is going on forever, and the condition is i<n , then check the values of i and n to make sure they're not changing unexpectedly. At the end of your loop, do this:, and it may help you find the problem yourself.

cout << "(DEBUG) i = " << i << endl;
cout << "(DEBUG) n = " << n << endl;

There are some other things that you're doing that will cause you headaches in the future. As said above, always use int main() . Second, your formatting is horrible. Braces (the { and } characters) should, as a general rule, be given their own line. Your opening brace itself should not be indented further than the code before it, but code inside braces should be more indented than the surrounding code. For example, this main functions looks much cleaner:

int main()
{
    int n;
    qa  q;
    fstream file1;

    file1.open("questions.dat", ios::out);
    cout << "\n Enter the limit of questions " << endl;
    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cout << "Enter the question" << endl;
        q.getques();

        cout << "enter the four options " << endl;
        q.getopts();

        cout<<"Enter the answer for the respective questions " << endl;
        q.getans();

        file1.write((char*)&q, sizeof(q));   //This is probably wrong, by the way.
    }

    file1.close();
}

See how much cleaner that looks?

Also, whoever told you to use (char*)&q near the end... don't listen to them. It's dangerous to interperet a class as a char* . Instead, have it print the individual arrays, the ones which are arrays of characters.

Odd, I was having what is in some ways the opposite problem - when I compile and run the program, it accepts the first set of questions and answers, then prints out the series of inquiries for the remaining questions all at once, then ending.

Enter the limit of questions
4
Enter the question
why

enter the four options
why not
because
who knows
fish

Enter the answer for the respective questions
fish
Enter the question
enter the four options
Enter the answer for the respective questions
Enter the question
enter the four options
Enter the answer for the respective questions
Enter the question
enter the four options
Enter the answer for the respective questions

While I was working on that problem, I noticed one thing: as it is now, you are overwriting each question with the next one. You want to have q be a pointer to qa, and assign it a dynamically allocated array. You also need to change how you write to the file, as the way you're doing it now won't work - you need to 'serialize' the separate members of the object one at a time to get consistent results.

This code seems to work now:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>

using namespace std;

class qa
{
    char question[35];
    char option[4][20];
    int ans;    //correct option no (eg 0 1 2 3)

public:
    void getques()
    {
        cin.getline(question, 35);
    }


    void getopts()
    {
        for (int i = 0; i < 4; i++)
        {
            cout << i <<". ";
            cin.getline(option[i], 20);
        }
    }


    void getans()
    {
        cin>>ans;
        cin.ignore();
    }


    void write(ofstream& output)
    {
        output << question << ',';
        for (int j = 0; j < 4; j++)
        {
            output << option[j] << ',';
        }
        output << ans << endl;
    }

};


int main()
{
    int n;
    qa* q;
    ofstream file1;
    file1.open("questions.dat");

    cout<<"\n Enter the limit of questions " <<endl;
    cin>>n;
    cin.ignore();

    q = new qa[n]();

    for(int i=0; i<n; i++)
    {
        cout<<"Enter the question" <<endl;
        q[i].getques();
        cout<<"enter the four options " <<endl;
        q[i].getopts();
        cout<<"Enter the answer for the respective questions " <<endl;
        q[i].getans();

        q[i].write(file1);
    }

    delete q;
    file1.close();

    return 0;
}

It outputs a Comma Separated Values (CSV) file as CSV is both easier to work with than what you had in mind (you can read it in an ordinary text editor) and less brittle.

Since he's just dumping the question to a file immediately after input, he's really not "overwriting" anything but a temporary variable. So, no, he doesn't need to use an array.

Good point, I was overthinking things I suppose. My mistake.

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

 class qa
{ char question[50];
  char option[4][40];
  int ans;    //correct option no (eg 0 1 2 3)

public:
   void putques() 
	{ puts(question);
	}
		
		
   void putopts()
	{cout<< option[0] <<endl  << option[1] <<endl <<option[2] <<endl << option[3] <<endl;
	
	}

	
		
  

  int putans()
  {  return(ans);
  }


};



void main()
{   qa q;
	
    int n,m;
	
    fstream file2;
  
	file2.open("questions.dat",ios::in);
  
	


   
 int i=0;
 
 while(i!=10)
 
 {
     
    n=rand()%18;

	file2.seekg((n-1)*sizeof(q),ios::beg);

	file2.read((char*)&q,sizeof(q));

	
 q.putques();
 
 q.putopts();
 
 cout<<"enter your option";
 
 cin>>m;
 
 if(m==q.putans())
   
	 cout<<"Correct!!";
 
 else
   
	 cout<<"sorry your answer is Incorrect!!";
 
  
	i++;
 }

 file2.close();
}

What's wrong with this code.. the second part of the quiz..which is to display the questions and all..??
Please help...
Nothing is displayed properly .. :((

....??????

OK, to start with, since you are using more or less the same data members in both versions of the qa class, let's apply some basic reusable design by combining the two related classes into one, and separating this new class from the main program files. This will allow you to ensure that the two classes don't diverge in some way that would break them both, and make it less confusing for anyone trying to read these programs later. Since you are using an IDE that has code completion, let's write out the new class name in full as QuizAnswer .

Second, I notice you are including the <string> header, but don't seem to be using the string class itself. If you're allowed to use strings for this project, then I would recommend doing so, as it would simplify the data reading and writing if you didn't have to worry about the string lengths all the time the way you do with C-strings.

quiz.h

#ifndef QUIZ_H
#define QUIZ_H 1

#include <iostream>
#include <fstream>
#include <string>


const unsigned OPTION_COUNT = 4;

class QuizAnswer
{
    std::string question;
    std::string option[OPTION_COUNT];
    int answer;

public:
    void getques()
    {
        std::getline(std::cin, question);
    }

    void getopts();

    void getans()
    {
        std::cin >> answer;
        std::cin.ignore();
    }

    void write(std::ofstream& output);
    void read(std::ifstream& input);

    void putques()
    {
        std::cout << question << std::endl;
    }

    void putopts();
    int putans();
};

#endif

quiz.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "quiz.h"


void QuizAnswer::getopts()
{
    for (unsigned i = 0; i < OPTION_COUNT ; i++)
    {
        std::cout << i <<". ";
        std::getline(std::cin, option[i]);
    }
}


void QuizAnswer::write(std::ofstream& output)
{
    output << question << ',';
    for (unsigned j = 0; j < OPTION_COUNT; j++)
    {
        output << option[j] << ',';
    }
    output << answer << std::endl;
}


void QuizAnswer::read(std::ifstream& input)
{
    std::getline(input, question, ',');

    for (unsigned i = 0; i < OPTION_COUNT; i++)
    {
        std::getline(input, option[i], ',');
    }

    input >> answer;
}

void QuizAnswer::putopts()
{
    for (unsigned i = 0; i < OPTION_COUNT; i++)
    {
        std::cout << i << ". " << option[i] << std::endl;
    }
}


int QuizAnswer::putans()
{
    return(answer);
}

And just to make sure that we haven't broken anything important:

quiz_input.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "quiz.h"

using namespace std;

int main()
{
    int n;
    QuizAnswer* q;
    ofstream file1;
    file1.open("questions.dat");

    cout<<"\n Enter the limit of questions " <<endl;
    cin>>n;
    cin.ignore();

    q = new QuizAnswer[n]();

    for(int i=0; i<n; i++)
    {
        cout<<"Enter the question" <<endl;
        q[i].getques();
        cout<<"enter the four options " <<endl;
        q[i].getopts();
        cout<<"Enter the answer for the respective questions " <<endl;
        q[i].getans();

        q[i].write(file1);

    }

    delete q;
    file1.close();

    return 0;
}

Now, getting on with the problem of reading the information back in, the best solution is to have a read() function in the class, which I have quite conveniently provided for you. It's a simple enough task from there to read in an answer and print it out.

commented: Just Awesome!! Mastermind!! +1

Thanks a lot !!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.