Hi, im trying to create a quiz. What is the best way of store the questions and answers?
Hi, im trying to create a quiz. What is the best way of store the questions and answers?
map<string, string>
Kurt
I thought this was easy. But i don't works. I get theese errors:Code:int main() { string choose; string berit; cout << "Welcome to LivijnProductions new and really cool quiz! We call it Mayo.\nYou can choose between many alternatives!\n"; cout << "[S]tart new game - [H]ighscore - [E]xit\n"; cin >> choose; if (choose == "s" || choose == "S") { int grw; string ans; grw = 1; while (grw < 5) { question(grw); answer(grw) grw++; cin >> ans; } } } string question(qsin) { string question[4]; question[0] = "Whats the creator of this programs name?"; question[1] = "How many cars do i have?"; question[2] = "How many wifes do i have?"; question[3] = "How many poodles do i have?"; return question[qsin]; } string answer(awr) { string answer[4]; answer[0] = "Fredrik"; answer[1] = "3"; answer[2] = "0"; answer[3] = "0"; return answer[awr]; }
20 C:\Users\Fredrik\Desktop\Saker\Projekt\C++\main.cp p `question' undeclared (first use this function)
21 C:\Users\Fredrik\Desktop\Saker\Projekt\C++\main.cp p `answer' undeclared (first use this function)
22 C:\Users\Fredrik\Desktop\Saker\Projekt\C++\main.cp p expected `;' before "grw"
22 C:\Users\Fredrik\Desktop\Saker\Projekt\C++\main.cp p At global scope:
28 C:\Users\Fredrik\Desktop\Saker\Projekt\C++\main.cp p `std::string question' used prior to declaration
28 C:\Users\Fredrik\Desktop\Saker\Projekt\C++\main.cp p `qsin' was not declared in this scope
29 C:\Users\Fredrik\Desktop\Saker\Projekt\C++\main.cp p expected `,' or `;' before '{' token
39 C:\Users\Fredrik\Desktop\Saker\Projekt\C++\main.cp p `std::string answer' used prior to declaration
39 C:\Users\Fredrik\Desktop\Saker\Projekt\C++\main.cp p `awr' was not declared in this scope
40 C:\Users\Fredrik\Desktop\Saker\Projekt\C++\main.cp p expected `,' or `;' before '{' token
I'm new at C++. Need help!
You need to have function declarations (prototypes) before you use them (in main).
Another thing: question and answer return a string, but you don't store them anywhere in the calling code.
A simple data structure to hold the question/answer pair would be:
Then make an array of those, fill them in with questions and answers and you're all set.Code:struct QuizQuestion { string question; string answer; };
I don't understand
Should i change my questions and answers to a struct? Show me some more code please!
A simple program would look like this:
In another thread you asked, what to learn next. Well, learn about structs and classes.Code:void foo(int n); //declaration int main() { foo(10); //OK, compiler knows there's a foo that takes an int } void foo(int n) //implementation { //... }
if i call i function like this:
func(2);
how can i use the "2"?
Now i don't get any errors but if i write S or s nothing happens. I think i'm in the if but i want the program to cout the right question
Last edited by Livijn; 04-07-2007 at 05:12 AM.
Your compiler errors were related to the fact that the compiler didn't know about these functions at the point where you were trying to call it.
The compiler reads your code from top to bottom and it needs to know about all functions before it can compile function calls.
That's why you place prototypes before main().
As to your last question, have you considered looking for some tutorials. For example the FAQ link at the top might provide some interesting reading.
It is too much to expect someone explain what functions and structs are and how they are used, as the Web abounds with tutorials about these things.
Okej, i've tested and now almost everything works! Yeeey!
How do i stop a function from still drive itself? Like this:
wrong();
stop;
If the answer is wrong i don't want the program to still drive the wile. Off course, i can set the wile to false but i rather want to exit the function.
Code:if (wrong()) break;
Now i get this weird error, but this one comes up when i've answered the wrong question.
Error:Code:if (ans != answer(grw)) { wrong(points); break; }
This application has requested the runtime to terminate it in an unusual way. Contact...
Why is this error poping up? Whole code:
Code:#include <iostream> #include<windows.h> using namespace std; string question(int qsin); string answer(int awr); string wrong(int points); int main() { string choose; string berit; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); cout << "Welcome to LivijnProductions new and really cool quiz! We call it Mayo.\nYou can choose between many alternatives!\n"; cout << "[S]tart new game - [H]ighscore - [E]xit\n"; cin >> choose; if (choose == "s" || choose == "S") { int grw; int points; string ans; grw = 0; points = 0; while (grw < 5) { cout << question(grw); cin >> ans; if (ans != answer(grw)) { wrong(points); break; } else { points++; } grw++; } } } string question(int qsin) { string question[4]; question[0] = "Whats the creator of this programs name?"; question[1] = "How many cars do i have?"; question[2] = "How many wifes do i have?"; question[3] = "How many poodles do i have?"; return question[qsin]; } string answer(int awr) { string answer[4]; answer[0] = "Fredrik"; answer[1] = "3"; answer[2] = "0"; answer[3] = "0"; return answer[awr]; } string wrong(int points) { cout << "Wrong answer! You gathered "; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); cout << points; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); cout << " points!"; return 0; }
The problem could be
while the return value of the function is supposed to be a string (why?). Constructing a string out of a 0 (NULL) pointer throws an error. (If you don't catch it, the program terminates "in an unusual way".)Code:return 0;
Fix the return type (may-be should be void.)
Last edited by anon; 04-07-2007 at 07:16 AM.
Now when i have answered all the questions, the whole program goes jaOHDDDF(#¤!!! EDWD...
And when you answer wrong answer it just shuts down...
Code:
Code:#include <iostream> #include <windows.h> using namespace std; string question(int qsin); string answer(int awr); string wrong(int points); string finish(int points); int main() { string choose; string berit; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); cout << "Welcome to LivijnProductions new and really cool quiz! We call it Mayo.\nYou can choose between many alternatives!\n"; cout << "[S]tart new game - [H]ighscore - [E]xit\n"; cin >> choose; if (choose == "s" || choose == "S") { int grw; int points; string ans; grw = 0; points = 0; while (grw < 5) { cout << question(grw) << " "; cin >> ans; if (points == 4) { finish(points); break; } if (ans != answer(grw)) { wrong(points); break; } else { points++; } grw++; } } } string question(int qsin) { string question[4]; question[0] = "Whats the creator of this programs name?"; question[1] = "How many cars do i have?"; question[2] = "How many wifes do i have?"; question[3] = "How many poodles do i have?"; return question[qsin]; } string answer(int awr) { string answer[4]; answer[0] = "Fredrik"; answer[1] = "3"; answer[2] = "0"; answer[3] = "0"; return answer[awr]; } string wrong(int points) { cout << "Wrong answer! You gathered "; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); cout << points; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); cout << " points!"; cin.get(); } string finish(int points) { cout << "OMG! You've completed the whole quiz! You gathered "; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); cout << points; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); cout << " points!"; return "Finish"; }
Where did you get the idea that all functions should return a string?
You return nothing from wrong() so change return type to void. Not sure if what you are trying to return from finish is good, but you don't use it anyway. So again, why not make return type void? (Find out how to turn on compiler warnings with your IDE, the compiler can tell you if you are doing these things wrong.)
There may be problems with the logic (the order and conditions when you do something).
I'm not familiar with the Windows API, but specifying a foreground that is both red, green and blue, doesn't look too good. (The | operator between the Windows constants combines all the flags or properties.)