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()?
ifis checking whetherstd::cinis in a good state after reading.if (std::cin >> answer)just tests to see ifcinwas 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 useif (correct == true)- it's long-winded. You can just useif (correct).void scoreSystem(bool correct)and then you can dostd::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.