0

Header file:

#include <iostream>
#include <string>

std::string correctText = "\nCorrect! +";
std::string incorrectText = "False!\n";
std::string answer = "paris";

bool correct = true;

int score;

void scoreSystem() {
    if (correct == true) {
        std::cout << correctText;
        score++;
    }
    else {
        std::cout << incorrectText;
    }
}

Source file:

#include <iostream>
#include <string>
#include "Primitive Quiz Header.h"

int main() {
    std::cout << "What is the capital of france?\n\n";
    if (std::cin >> answer) {
        correct = true;
    }
    else {
        correct = false;
    }
    scoreSystem();
    std::cout << score;
    return 0;
}

The code is intended to use the bool correct as a way to tell the scoreSystem() function if it should increment the score integer and output the string correctText, or output the incorrectText string. If the user answers correctly, correct should be true; false if answered incorrectly.

However, no matter what the user enters, correct remains true and the scoreSystem() always outputs with the correctText string and increments the score integer.

Example:

What is the capital of france?

gruh \\example of incorrect user input

Correct! +1

I don't understand what exactly is preventing correct from changing. Is it the scoreSystem() function, or how the user inputs their answer in main()?

5
  • Where you comparing what the user entered to the expected answer? Your if is checking whether std::cin is in a good state after reading. Commented Oct 16, 2024 at 3:14
  • Also, why so many globals? They're going to make your life more difficult down the line. Commented Oct 16, 2024 at 3:15
  • 1
    You never test to see if the user entered the correct answer. if (std::cin >> answer) just tests to see if cin was successful in reading the input. You're also replacing the value of "paris" with whatever the user inputs, so you have nothing to test against any longer. And you don't need to use if (correct == true) - it's long-winded. You can just use if (correct). Commented Oct 16, 2024 at 3:16
  • 1
    You shouldn't put the definition of any of those variables and functions in the header. Once you have multiple files including that same header you'll get a multiple definition error. Commented Oct 16, 2024 at 3:17
  • 1
    Never use global variables if you can avoid them, use a bool argument on scoresystem to pass that information on to the function. e.g. void scoreSystem(bool correct) and then you can do std::cin >> answer; scoreSystem( answer == "Paris" ); (there isn't even an if statement needed in main). Then use `int scoreSystem(bool correct) { static int score = 0; if (correct) ++score; return score; }); The excercise for you is also to something with the "answer" string so it doesn't have to be a global variable either. Commented Oct 16, 2024 at 3:21

1 Answer 1

3

A statement like if (x) y = true; is clumsy, and should generally be avoided. If x is already a bool, then you can usually use y = x;. If it's not already a bool, then you may want y = static_cast<bool>(x); instead (or, for people who prefer parsimony, y = !!x;).

It's also rarely useful to ask only a single question. Usually you'd want something along this general line:

struct Question {
    std::string question;
    std::string answer;

    bool correct(std::string const &user_answer) { 
       return user_answer == answer;
    }

    friend std::ostream &operator<<(std::ostream &os, Question const &q) { 
        return os << q.question;
    }
};

Then you could have a number of questions and answers, something like this:

const std::vector<Question> questions { 
    { "What is the capitol of France?", "Paris" },
    { "what is the capitol of Great Britain?", "London"},
    { "What is deepest lake on earth?", "Baikal"}
    // ...
};

int main() { 
    int score = 0;

    for (auto const &q : questions) {
        std::cout << q;
        std::string user_answer;
        std::cin >> user_answer;
        bool correct = q.correct(user_answer);
        std::cout << std::boolalpha << correct;
        score += correct;
    }
    std::cout << "You got: " << score << " questions right\n";
}

This has one minor defect though. It prints out true and false instead of "\nCorrect! +" and "\nFalse!". Fortunately, there's a way to fix that too.

#include <string>
#include <vector>
#include <locale>
#include <ios>
#include <iostream>

class TestPunct : public std::numpunct< char > {
protected:
    char do_decimal_point() const { return ','; }
    char do_thousands_sep() const { return '.'; }
    std::string do_grouping() const { return "\3"; }
    std::string do_truename() const { return "\nCorrect!\n";  }
    std::string do_falsename() const { return "\nFalse\n"; }
};

struct Question {
    std::string question;
    std::string answer;

    bool correct(std::string const &user_answer) const { 
       return user_answer == answer;
    }

    friend std::ostream &operator<<(std::ostream &os, Question const &q) { 
        return os << q.question << "? ";
    }
};

const std::vector<Question> questions { 
    { "What is the capitol of France", "Paris" },
    { "what is the capitol of Great Britain", "London"},
    { "What is deepest lake on earth", "Baikal"}
    // ...
};

int main() { 
    std::cout.imbue(std::locale(std::locale(), new TestPunct));

    int score = 0;

    for (auto const &q : questions) {
        std::cout << q;
        std::string user_answer;
        std::cin >> user_answer;
        bool correct = q.correct(user_answer);
        std::cout << std::boolalpha << correct;
        score += correct;
    }
    std::cout << "You got: " << score << " questions right\n";
}

Result:

$ ./a.out
What is the capitol of France? Paris

Correct!
what is the capitol of Great Britain? London

Correct!
What is deepest lake on earth? Baikal

Correct!
You got: 3 questions right
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.