Thread: Easy quiz!

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    188

    Easy quiz!

    Hi, im trying to create a quiz. What is the best way of store the questions and answers?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    map<string, string>
    Kurt

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    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];
    }
    I thought this was easy. But i don't works. I get theese errors:
    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!

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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:
    Code:
    struct QuizQuestion
    {
        string question;
        string answer;
    };
    Then make an array of those, fill them in with questions and answers and you're all set.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    I don't understand

    Should i change my questions and answers to a struct? Show me some more code please!

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A simple program would look like this:
    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
    { 
        //...
    }
    In another thread you asked, what to learn next. Well, learn about structs and classes.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    if i call i function like this:
    func(2);
    how can i use the "2"?

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    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.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    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.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if (wrong()) break;

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Now i get this weird error, but this one comes up when i've answered the wrong question.
    Code:
    if (ans != answer(grw)) {
                                     wrong(points);
                                     break;
                                     }
    Error:
    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;
    }

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The problem could be
    Code:
    return 0;
    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".)

    Fix the return type (may-be should be void.)
    Last edited by anon; 04-07-2007 at 07:16 AM.

  14. #14
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Now when i have answered all the questions, the whole program goes jaOHDDDF(#&#164;!!! 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";
    }

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  2. Try this quiz (just made it with C)
    By voltson4 in forum C Programming
    Replies: 1
    Last Post: 07-11-2003, 06:28 PM
  3. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  4. C programming language quiz
    By Trancongan in forum C Programming
    Replies: 7
    Last Post: 04-21-2002, 01:04 PM
  5. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM