Hi! I need help for my homework in c++. My professor wants us to write a program that ask a question and there is a multiple choice and the answer. like the gameshow "Who wants to be a millionaire?"

Please give me some example.. (^_^) thank you so much!

Dani AI

Generated

A simple, maintainable design works best: represent each question as a small struct with the text, a vector of choices, and the correct answer. That follows the flow suggested by (present question, read answer, check correctness) and the I/O hint from . The short example below shows a minimal console quiz in idiomatic C++ and uses std::getline for robust input handling instead of mixing >> and getline. 's C link is a useful reference for console flow; this is a C++-oriented starter.

#include <iostream>
#include <string>
#include <vector>
#include <cctype>

struct Question {
    std::string text;
    std::vector<std::string> choices;
    char answer; // 'A','B','C','D'
};

int main() {
    std::vector<Question> quiz;

    Question q;
    q.text = "What is the capital of France?";
    q.choices.push_back("A) Berlin");
    q.choices.push_back("B) Madrid");
    q.choices.push_back("C) Paris");
    q.choices.push_back("D) Rome");
    q.answer = 'C';
    quiz.push_back(q);

    q.text = "Which language is commonly used for system programming?";
    q.choices.clear();
    q.choices.push_back("A) Python");
    q.choices.push_back("B) C++");
    q.choices.push_back("C) HTML");
    q.choices.push_back("D) CSS");
    q.answer = 'B';
    quiz.push_back(q);

    int score = 0;
    for (std::size_t i = 0; i < quiz.size(); ++i) {
        const Question& cur = quiz[i];
        std::cout << "Question " << (i + 1) << ": " << cur.text << "\n";
        for (std::size_t j = 0; j < cur.choices.size(); ++j)
            std::cout << cur.choices[j] << "\n";

        std::cout << "Answer: ";
        std::string line;
        if (!std::getline(std::cin, line)) break;

        char pick = 0;
        for (std::size_t k = 0; k < line.size(); ++k) {
            char c = line[k];
            if (!std::isspace(static_cast<unsigned char>(c))) {
                pick = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
                break;
            }
        }

        if (pick == cur.answer) {
            std::cout << "Correct!\n\n";
            ++score;
        } else {
            std::cout << "Wrong. Correct answer: " << cur.answer << "\n\n";
        }
    }

    std::cout << "Score: " << score << " / " << quiz.size() << "\n";
    return 0;
}

Notes and quick extensions: store the correct answer as an index (0..n-1) if easier to compare; validate empty input and accept lowercase by converting with std::toupper as shown; avoid operator>> when using getline to prevent leftover newline issues. For larger quizzes, load questions from a file, use std::shuffle with <random> to randomize order, or serialize to JSON for easier editing. Reference material: iostream documentation and std::vector reference.

Recommended Answers

All 4 Replies

Well, if you paid any attention, that would be easy. But you didn't pay attention.... Therefor you show no effort in your work. You should have read the rules here.


BUT,

just to be nice. Look into cout, cin, and then the if statement.

commented: And very nice it was too :) +19

i can help u but the rules state "Dont give away code" but i can always help

It is C example but it is not hard to translate to C++
From here, Show your efforts as you have starting point

Output question.
Output possible answers.
Ask for input (A character)
Check if answer is correct.
Move to next question.

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.