@g10g2000cwb.googlegroups.com>, (e-mail address removed)
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:
stream &
operator<<(std:
stream &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.