|
From: Per W. <pw...@ia...> - 2004-08-27 08:59:04
|
I haven't the time to discuss your stream problem - there are a _large_
number of questions and answers about buffering and switching between
input and output in the archives of this list. Anyway, a quick note: don't
mix stream input/output with FILE-based input/output. The two methods have
their own internal implementation - including input/output caching - and
don't know about each other.
However, I wanted to comment a bit about your class implementation.
1) You have a class where each object is > 5kB large.
2) Quiz::createQuestion() is not static, i.e. you need a Quiz object to
call this member function.
3) Quiz::createQuestion() creates a new Quiz object as autovariable, and
later returns it.
You should rewrite createQuestion to either work directly on itself (the
'this' object), or to be static and perform all work on a created object.
When passing a Quiz object as a parameter, you should pass it as a
reference, instead of as value. Your Quiz::writeQuestion() method requires
that a new Quiz object is created as parameter.
Note that objects - just as C structs - have size and making extra copies
of them requires more memory and a lot of copying of information between
the copies. In the C++ case, assignment operators, copy constructors etc
might contain a lot of code that gets run.
One other thing. You haven't written any constructor for the Quiz class.
It is almost alwas a good idea to write your own constructor, instead of
letting the compiler supply a default constructor.
A third thing. You have static variable 'type'. Why static? If the
information is needed for processing of the question, it should be a
non-static member of the Quiz class. Then the writeQuestion method don't
need 'type' as input parameter.
Obviously Quiz::writeQuestion shouldn't need a quiz, a type etc as input
parameters. Since Quiz::writeQuestion is a method of the Quiz class, it
should write out itself.
/Per W
On Thu, 26 Aug 2004, Noorez Kassam wrote:
> #ifndef quiz_h //quiz.h
> #define quiz_h
> class Quiz
> {
> private:
> char question[1000];
> char answerA[1000];
> char answerB[1000];
> char answerC[1000];
> char answerD[1000];
> char answerT;
> char answerF;
> char actual;
>
> public:
> static int questionNumb;
> static int type;
> Quiz createQuestion();
> void writeQuestion(Quiz ques, int type,char* quizName);
> };
> #endif
> //quiz.h
>
> #include <iostream>
> #include <fstream>
> #include <cstdlib>
> #include <conio.h>
> #include <myfs.h>//this is mine, connects to my static lib
> #include <conio.h>
> using namespace std;
> using namespace myf;
>
> #include "Quiz.h"
>
> int Quiz::questionNumb =3D 1;
> int Quiz::type =3D 0;
>
> Quiz Quiz::createQuestion()
> {
> Quiz ques;
> cls();//lib function
> cout << "What kind of question do you want question " <<
> Quiz::questionNumb
> << " to be?\n";
> real1:
> cout << "A. Multiple choice or B. True/False [a\\b] ";
> char choice;
> choice=3Dgetche();//this char that is inputted
> if(!(choice =3D=3D 'a' || choice =3D=3D 'b'))
> {
> cout << "\nChoice must be a or b\n";
> goto real1;
> }
> if(choice =3D=3D 'a')
> {
> type =3D 1;
> cout << "\n\nType the question in\n\n";
> cin.getline(ques.question,1000); //shows up hear as if i typed it
> but didn't push enter
> cout << "Type in answer A. ";
> cin.getline(ques.answerA,1000);
> cout << "Type in answer B. ";
> cin.getline(ques.answerB,1000);
> cout << "Type in answer C. ";
> cin.getline(ques.answerC,1000);
> cout << "Type in answer D. ";
> cin.getline(ques.answerD,1000);
> real2:
> cout << "Give us the real answer [a\\b\\c\\d] ";
> ques.actual=3Dgetche();
> if(!(ques.actual =3D=3D 'a' || ques.actual =3D=3D 'b'
> || ques.actual =3D=3D 'c' || ques.actual =3D=3D 'd') )
> {
> cout << "\nReal answer must be a,b,c or d\n";
> goto real2;
> }
> }
> if(choice =3D=3D 'b')
> {
> type =3D 2;
> cout << "\n\nType in the statement\n\n";
> cin.getline(ques.question,1000); //same hear
> real3:
> cout << "Give us the real answer [t\\f] ";
> ques.actual=3Dgetche();
> if(!(ques.actual =3D=3D 't' || ques.actual =3D=3D 'f'))
> {
> cout << "\nReal answer must be t or f\n";
> goto real3;
> }
> }
> return ques;
> }
>
> void Quiz::writeQuestion(Quiz ques, int type, char* quizName)
> {
> ofstream out_file(quizName,ios::app);
> if(type =3D=3D 1)
> {
> out_file << "~\n" << questionNumb << ". " << ques.question << '\n=
'
> << '\n';
> out_file << "A. " << ques.answerA << '\n';
> out_file << "B. " << ques.answerB << '\n';
> out_file << "C. " << ques.answerC << '\n';
> out_file << "D. " << ques.answerD << '\n';
> out_file << '~' << ques.actual << '\n' << '|';
> }
> if(type =3D=3D 2)
> {
> out_file << "~\n" << questionNumb << ". " << ques.question << '\=
n';
> out_file << "True or False [t\\f]\n";
> out_file << '~' << ques.actual << '\n' << '|';
> }
> }
>
> on the marked lines i put some errors that i am getting in my program.
>
> _________________________________________________________________
> Take charge with a pop-up guard built on patented Microsoft=AE SmartScree=
n
> Technology.
> http://join.msn.com/?pgmarket=3Den-ca&page=3Dbyoa/prem&xAPID=3D1994&DI=3D=
1034&SU=3Dhttp://hotmail.com/enca&HL=3DMarket_MSNIS_Taglines
> Start enjoying all the benefits of MSN=AE Premium right now and get the
> first two months FREE*.
>
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
> 100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
> Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
> http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|