By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

Need help in designing a quiz

P: n/a
Hey everyone just designing a quiz for school and managed to write the
code for the questions and answers thou i have to add scores and at the
end an overall score does anyone have any idea what to do or what i am
talkin about

Apr 19 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
C++ Hell wrote:
Hey everyone just designing a quiz for school and managed to write the
code for the questions and answers thou i have to add scores and at the
end an overall score does anyone have any idea what to do or what i am
talkin about


What does that have to do with the C++ programming language?

Regards,
Ben
Apr 19 '06 #2

P: n/a

"C++ Hell" <jc*********@hotmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
Hey everyone just designing a quiz for school and managed to write the
code for the questions and answers thou i have to add scores and at the
end an overall score does anyone have any idea what to do or what i am
talkin about


I'm going to go out on a limb and assme that you're talking about a program
you're writing (in C++) that does what you're describing? If so, then
what's the problem you're having, exactly? What have you got written so
far, and what's it doing (or not doing) that's giving you problems? We're
not going to just write your code for you, you know. :-)

-Howard


Apr 19 '06 #3

P: n/a
Sorry guys yea its in C++ programmin in visual basic, dont expect you
to write it for me just wonderin in which direction i can go, #include
<iostream>
using namespace std;
void main () {
cout << "FUN RUN QUIZ \n";
cout << " \n";
cout << "Instructions \n";
cout << "1)This game consists of 6 questions with three possible
answers to each question \n";
cout << "2)In each question you must only choose one answer out of the
possibley three by enterin the number of the answer and pressing
enter\n";
cout << "3)After each question you will be given the correct answer,
weather you were right or wrong and your score once you are happy
with this press enter \n";
cout << "4)Repeat instrustions 1-3 till you finish all questions \n";
cout << "5)Your overall score will be displayed at the end of the quiz
\n";
int result;
cout << "\n";
cout << "Question 1 \n";
cout << "What is the captial of england? \n";
cout << "1)Birminham 2)London 3)Manchester \n";
cout << "\n";
cout << "Enter your answer \n";
cin >> result;
if (result == 1) {
cout<< "Wrong Answer (The correct answer was London)" << endl;
}
else if (result == 2) {
cout << "Correct Answer" << endl;
}
else if (result == 3) {
cout << "Wrong Answer (The correct answer was London)" << endl;
}
cout << "\n";
cout << "Question 2 \n";
cout << "Who wrote the Harry Potter books? \n";
cout << "1)J K Rowling 2)T K Rowling 3)H K Rowling \n";
cout << "\n";
cout << "Enter your answer \n";
cin >> result;
if (result == 1) {
cout<< "Correct Answer" << endl;
}
else if (result == 2) {
cout << "Wrong Answer (The correct answer was J K Rowling)" << endl;
}
else if (result == 3) {
cout << "Wrong Answer (The correct answer was J K Rowling)" << endl;
}
cout << "\n";
cout << "Question 3 \n";
cout << "Which actor stars in the film Into the Blue? \n";
cout << "1)Paul Walker 2)Tome Cruise 3)Will Smith \n";
cout << "\n";
cout << "Enter your answer \n";
cin >> result;
if (result == 1) {
cout<< "Correct Answer" << endl;
}
else if (result == 2) {
cout << "Wrong Answer (The correct answer was Paul Walker)" << endl;
}
else if (result == 3) {
cout << "Wrong Answer (The correct answer was Paul Walker)" << endl;
}
cout << "\n";
cout << "Question 4 \n";
cout << "What does DvD stand for? \n";
cout << "1)Digital Virtual Drive 2)Drive Video Disk 3)Digital Video
Disk \n";
cout << "\n";
cout << "Enter your answer \n";
cin >> result;
if (result == 1) {
cout<< "Wrong Answer (The correct answer was Digital Video Disk)" <<
endl;
}
else if (result == 2) {
cout << "Wrong Answer (The correct answer was Digital Video Disk)" <<
endl;
}
else if (result == 3) {
cout << "Correct Answer" << endl;
}
cout << "\n";
cout << "Question 5 \n";
cout << "In what year did Cadbury's first start producing chocolate?
\n";
cout << "1)1824 2)1830 3)1832 \n";
cout << "\n";
cout << "Enter your answer \n";
cin >> result;
if (result == 1) {
cout<< "Correct Answer" << endl;
}
else if (result == 2) {
cout << "Wrong Answer (The correct answer was 1824)" << endl;
}
else if (result == 3) {
cout << "Wrong Answer (The correct answer was 1824)" << endl;
}
cout << "\n";
cout << "Question 6 \n";
cout << "In what year sis mozart write his first three Symphonies? \n";
cout << "1)1761 2)1764 3)1765 \n";
cout << "\n";
cout << "Enter your answer \n";
cin >> result;
if (result == 1) {
cout<< "Wrong Answer (The correct answer was 1764)" << endl;
}
else if (result == 2) {
cout << "Correct Answer" << endl;
}
else if (result == 3) {
cout << "Wrong Answer (The correct answer was 1764)" << endl;
}
}

This is what i got, thanks for takin a look

Apr 19 '06 #4

P: n/a
C++ Hell wrote:
Sorry guys yea its in C++ programmin in visual basic, dont expect you
to write it for me just wonderin in which direction i can go,
What tutorials have you read? Have you curled up (preferably under a
blanket, before the fireplace, reading in the cherry light reflected from a
shovel) and read the stinkin' thing from the cover to at least the second
third?

(I admit I simply can't bring myself to read more than a page of /Advanced
CORBA Programming with C++/, but that's what I did with my first C book.
Modulo the fireplace [which is an oblique reference to Abraham Lincoln].)
#include
<iostream>
using namespace std;
void main () {
'void main' will increase the odds you get disappeared to GITMO. Use 'int
main'.
cout << "Question 1 \n";
cout << "What is the captial of england? \n";
cout << "1)Birminham 2)London 3)Manchester \n";


One important topic in programming is removing duplication. Your code is
essentially a list of questions. If so, make it a real list:

struct question {
string what_is;
string options[3];
} questions[] = { "what is the...", { "NY", "DC", "CD" }...

etc. Learn to put things into a little table like that, then write a loop
statement that indexes and uses each item in the table.

Further learner questions might get a better response at a newsgroup which I
suspect is called news:alt.comp.lang.learn.c-c++ , or something like that.

Good luck!

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 19 '06 #5

P: n/a

C++ Hell wrote:
Sorry guys yea its in C++ programmin in visual basic, dont expect you
to write it for me just wonderin in which direction i can go


[code]

First of all, why don't make this a more OO program. For example, you
can encapsulate the questions in a class with three fields: The
question text, an array of possible choices and a number representing
the correct answer index in the array... This way the program would be
much smaller, and adding a new question wouldn't that hard...

About the score, what are you having trouble with? If you followed what
I said above, the score would be as simple as one condition for the
correctness of the answer and increamenting the number of correct
answers, then finally multiplying the latter by the weight of a single
correct answer...

Abdo Haji-Ali
Programmer
In|Framez

Apr 19 '06 #6

P: n/a
C++ Hell wrote:
Sorry guys yea its in C++ programmin in visual basic, dont expect you
to write it for me just wonderin in which direction i can go
Well, first off, you could use some well placed white space to
make things a bit more readable. Also, proofread the text quite
carefully before letting students at it.
, #include
<iostream>
using namespace std;
I tend to resist this, choosing to include the std:: in the calls.
That may just be because I have been forced, in the past, to
use a buggy compiler that had whacky things going on in
it's implementation of std:: so that this line often revealed
problems that could be avoided (not solved) by avoiding
the line.
void main () {
main returns int.
cout << "FUN RUN QUIZ \n";
cout << " \n";
cout << "Instructions \n";
cout << "1)This game consists of 6 questions with three possible
answers to each question \n";
cout << "2)In each question you must only choose one answer out of the
possibley three by enterin the number of the answer and pressing
Possible.
enter\n";
You need to deal with the possibility the user types other than
the integer you want. For example, if you display choices like

1)Birminham 2)London 3)Manchester

you need to deal with the possibility the user types any of
the following and a bunch more.

1) Manchester
99
Hello
(*&^9d8*&
It's Birmingham

One way is to read a line rather than what you've done (which
is read an integer) and then do some parsing on the line.
You could, for example, tell the user that anything that is
not an integer means the line is invalid, and pester the user
to re-enter that value.
cout << "3)After each question you will be given the correct answer,
weather you were right or wrong and your score once you are happy
with this press enter \n";
cout << "4)Repeat instrustions 1-3 till you finish all questions \n";
cout << "5)Your overall score will be displayed at the end of the quiz
\n";
int result;
cout << "\n";
cout << "Question 1 \n";
cout << "What is the captial of england? \n";
Capital. England.
cout << "1)Birminham 2)London 3)Manchester \n";
cout << "\n";
You might want to experiment with better formatting of this.
Such as putting the answers in a column rather than a row.

What is the captial of England?
1)Birminham
2)London
3)Manchester

cout << "Enter your answer \n";
cin >> result;
if (result == 1) {
cout<< "Wrong Answer (The correct answer was London)" << endl;
}
else if (result == 2) {
cout << "Correct Answer" << endl;
}
else if (result == 3) {
cout << "Wrong Answer (The correct answer was London)" << endl;
}

[rest snipped]

You would produce something much more readable if you were
to collect your questions into some kind of table. For example,
make a class with some entries for: the text of the question,
the text of each guess, and the number of the correct answer.
Here I go coding at the terminal, so there are probably lots of
typos and syntax errors here.

class Question
{
public:
Question();
std::string GetQuestion();
void SetQuestion(std::string);
// other functions go here
private:
std::string question;
std::vector<std::string> answers;
int rightAnswer;
int givenAnswer;
};

Then you make a list (or maybe a vector) of questions and
fill it in. Then you step through the list in a loop, ask each
question, and record the answer. Note that you will need
to learn about std things string, vector, and list, and you
will probably want to learn something about iterators on
the last two. If your text has not yet introduced these things,
read ahead to the parts where it does. Learn them early,
use them often.

Look up the "rule of three" in the FAQ, to see what the
first few items are that go in at // other functions go here

This makes things much more flexible because the parts of
the test where you actually interact with the student are
in a single loop. You don't have to repeat all that stuff for
each question, the loop and list do that for you. You could
quite easily change such things as:
- give the right answer right away or prompt on wrong answers
to guess again or give the right answers only at the end
- keep a running score or only total up the score at the end
- only report the final right/wrong, produce a percent right,
change to a letter grade, or produce a full printout of the
test with marked up right/wrong answers.
- The test could be stored in a file and your prog could read
it in. Then the same prog could do any test, with an arbitrary
number of questions, each with an arbitrary number of
choices, and each worth an arbitrary score possibly based
on difficulty.
Socks

Apr 19 '06 #7

P: n/a
"C++ Hell" writes:
Sorry guys yea its in C++ programmin in visual basic, dont expect you
to write it for me just wonderin in which direction i can go,
<iostream>
using namespace std;
void main () {
cout << "FUN RUN QUIZ \n";
cout << " \n";
cout << "Instructions \n";
cout << "1)This game consists of 6 questions with three possible
answers to each question \n";
cout << "2)In each question you must only choose one answer out of the
possibley three by enterin the number of the answer and pressing
enter\n";

<snip>

You're doing it backwards! Write the program in a shell or stub form and get
it into a form you can live with. You can remember what "q 1" and "a 1"
stands for for a few minutes. Type it in concisely and then, after it is
working, and only then, type in the actual data. You have done all this
typing, there is so much of it that you can't see the forest for the trees,
and now you are going to have to type it all in again. As the others have
pointed out, get a structure of some form, minimize the redundancy and
duplication. You have a long linear list, OK for a novel but this is a
computer program. Certainly arrays or vectors of some sort are going to be
involved somehow in the final solution.

Do you want it to be fancy to impress someone. like an instructor? If not I
would use a simple array of const char* to contain the questions and another
(parallel) array for the answers and another parallel const array of int
for the proper numeric answers - the answer code, I guess. Clearly that
stuff all *belongs* in a struct or a class, parallel arrays are kind of out
of style. If they ever *were* in style.
Apr 19 '06 #8

P: n/a
osmium wrote:
You¬*have¬*a¬*long¬*linear¬*list,¬*OK¬*for¬*a¬*nov el¬*but¬*this¬*is¬*a
computer program.¬*¬*Certainly¬*arrays¬*or¬*vectors¬*of¬*so me¬*sort¬*are¬*going¬*to
be involved somehow in the final solution.


Part of being an advanced programmer is learning to go for the simple, not
advanced solution.

For example, my array was over-engineered. I could have gone simpler.
We just need a list of function calls:

ask("Who is buried in grant's tomb",
"grant",
"and his horse",
"nobody's buried in it because it's a tomb, not a plot");

ask("What is the eternal Tao",
"the known Tao",
"and the unknown Tao",
"eternally compiling ACE+TAO");

Note that my hypothetical ask() function will take care of the "?", the "1)
" numbers on the questions, etc.

The original poster is advised to start the program again, with 1 of these
ask() functions, and get it working before adding more. Then copy all the
strings over.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 19 '06 #9

P: n/a
Phlip wrote:
osmium wrote:
You have a long linear list, OK for a novel but this is a
computer program. Certainly arrays or vectors of some sort are going to
be involved somehow in the final solution.


Part of being an advanced programmer is learning to go for the simple, not
advanced solution.

For example, my array was over-engineered. I could have gone simpler.
We just need a list of function calls:

ask("Who is buried in grant's tomb",
"grant",
"and his horse",
"nobody's buried in it because it's a tomb, not a plot");

ask("What is the eternal Tao",
"the known Tao",
"and the unknown Tao",
"eternally compiling ACE+TAO");

Note that my hypothetical ask() function will take care of the "?", the "1)
" numbers on the questions, etc.

The original poster is advised to start the program again, with 1 of these
ask() functions, and get it working before adding more. Then copy all the
strings over.


The important feature you have retained here is that it is still
basically tabular. You've got a function with args that effectively
acts as the columns in a table. So it's quite easy to add
another question, and it divorces the creation of questions
from the machinery of asking the question. This is good.
It means that the problem is being solved in the space of
the problem, as opposed to the space of C++ syntax.

In some ways this approach is simpler. Now, suppose he wants
to maintain the answers and make a report of the entire test?
Maybe a collection holding instancs of a class is the way to go
after all.

The way to decide how fancy to get, the way to decide how
elaborate is too elaborate, is to consider the changes that
are likely to be requested over the life of the code. If the code
has a lifespan that consists of "long enough to learn how to
use feature x" then you don't want any elaboration at all.

If the code is likely to last long enough for some instructor
to moan about the answers not being printed out in a nice
format that can be put in a book of remembrance of students
she has flunked, then some elaboration is good.

If the code is likely to get included in a library, and have
change requests thrown like darts in a pub for years,
it is important to be fairly heavy on the design effort.
Socks

Apr 19 '06 #10

P: n/a
Puppet_Sock wrote:
If the code is likely to get included in a library, and have
change requests thrown like darts in a pub for years,
it is important to be fairly heavy on the design effort.


You have a typographical error there. You obviously intended to write
"should be fairly heavy on the unit test effort."

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 19 '06 #11

P: n/a
C++ Hell wrote:
Sorry guys yea its in C++ programmin in visual basic,


You lost me here.
Apr 20 '06 #12

P: n/a
On Thu, 20 Apr 2006 03:10:59 +0000, red floyd wrote:
C++ Hell wrote:
Sorry guys yea its in C++ programmin in visual basic,


You lost me here.


It's kind of like JavaScript programming in C++.

Apr 20 '06 #13

P: n/a
Phlip wrote:
Puppet_Sock wrote:
If the code is likely to get included in a library, and have
change requests thrown like darts in a pub for years,
it is important to be fairly heavy on the design effort.


You have a typographical error there. You obviously intended to write
"should be fairly heavy on the unit test effort."


These are not mutually exclusive, and in fact, contribute
to eachother. When it is code to go in a long-lived library,
you certainly want to do plenty-o unit tests. But you would
also want to be designing to make the unit tests easy,
effective, and obvious to understand. So you get such
things as self testing, regression testing, invariants in
classes, etc. etc.

But what I was getting at in the context you snipped was,
when code is expected to have a long life, you need to
work harder at accomodating expectable change requests.
Doing the design effort in advance in such a case means
you will, hopefully, save time over all. But it depends on
the code having a long life.
Socks

Apr 20 '06 #14

P: n/a
In article <1145468643.156794.105730
@g10g2000cwb.googlegroups.com>, jc*********@hotmail.com
says...
Sorry guys yea its in C++ programmin in visual basic, dont expect you
to write it for me just wonderin in which direction i can go,
#include <iostream>
using namespace std;
void main () {
cout << "FUN RUN QUIZ \n";
cout << " \n";
cout << "Instructions \n";


[ ... ]

You've gotten quite a few answers already, but hopefully
I can make it worth your time to read one more.

Most of the answers you've gotten have given roughly
similar advice -- basically that you separate the code
for presenting questions, collecting answers, etc., from
the data representing the questions and answers
themselves.

I'm going to suggest that while they have the right idea,
they generally haven't gone far enough. In particular,
I'd advise moving the data representing the questions and
answers out into a separate data file.

I'd design the data file something like this:

First question
3 2 // 3 = number of choices, 2 = correct answer
choice 1
choice 2
choice 3
second question
4 1
choice 1
choice 2
choice 3
choice 4

and so on for as many questions as you want in the test.
You can modify the exact structure to accommodate what
you need -- for example, if you're sure you're only ever
going to have three choices for every question, you can
skip over putting that number into the data file.
Likewise, there's nothing sacred about the order in which
I've arranged the fields. For example, if you wanted the
possible choices, then correct answer, and finally the
question, that would work perfectly well also.

Then design your program to read in this data file, and
present it to the user. You'd design a data structure to
hold the data for an individual question, something like
this:

struct question {
std::string q;
std::vector<std::string> choices;
int answer;
};

Then you'd write some code to read one question in from
the file:

std::istream &
operator>>(std::istream &is, question &q) {
std::getline(is, q.q);

int num_choices;
is >> num_choices;

// and so on for the rest of the data for one
// question.

return is;
}

Then you'd write some code to present a question to the
user. Even though this really only makes sense
interactively, it may be easiest to write it as a normal
operator<< for a 'question':

std::ostream &
operator<<(std::ostream &os, question const &q) {
os << q.question << "\n";
// and so on for the remaining items in a question
// structure.
}

Then we'll want to handle all of asking a question --
present the question to the user, get their choice,
inform them whether their choice was correct, and return
a bool to indicate whether they gave the right answer:

bool ask(question const &q) {
std::cout << q;
int response;
std::cin >> response;

// ...give the user feedback about whether their choice
// was correct. return true to indicate a correct answer,
// or false to indicate an incorrect one.
}

Finally, put it all together: read the data for a test
into a vector, present the questions to the user in
order, and count up how many of their answers were
correct:

int main(int argc, char **argv) {
std::vector<question> test;

std::ifstream data("test.dat");
std::copy(
std::istream_iterator<question>(data),
std::istream_iterator<question>(),
std::back_inserter(test));

// If you're going to save the data from the test,
// right here is were you'd probably ask the user for
// their student ID number or whatever you're going to
// use to identify them.

// Ask each question and count the correct answers:
int correct_answers = std::count_if(
test.begin(), test.end(), ask);

// Here is where we'd do what we're going to with the
// student's score -- report it to them (either as a
// percentage or a letter grade) save it under their
// student ID, etc.

return 0;
}

Final point: if you're worried about the student reading
the data file, it's pretty easy to incorporate some
trivial encryption that will make the data file more
difficult to read. One well-known method is to simply XOR
each byte from the data file with a byte from a string.
As encryption goes, this is quite weak -- but it's still
enough that most people won't bother breaking it.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Apr 21 '06 #15

P: n/a
They pay per line :-)

Apr 24 '06 #16

P: n/a
In article <1145899782.684067.119110
@u72g2000cwu.googlegroups.com>, jo********@gmail.com
says...
They pay per line :-)


Oh, so _that's_ why I was writing all those comments...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Apr 26 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.