#include <iostream>
#include <vector>
using namespace std;
int main()
{
//would rather have something more fitting like a vector of pairs
vector<string> question {"What is your name?", "thaostra",
"What is your quest?", "make this program better",
"What is your favourite color?", "black"};
string input;
auto incorrect(0);
if(question.size() % 2 == 0){
//want a range-based for succinctness
for(auto i(0); i < (int)question.size(); ++i){
cout << question[i];
getline(cin, input);
if(input.compare(question[++i]) != 0){
incorrect++;
cout << "Correct answer is: " << question[i] << endl;
}
}
cout << "Number correct out of " << (question.size() / 2) << ": "
<< (question.size() / 2) - incorrect << endl;
} else
cout << "Uneven count of questions to answers" << endl;
}
Refactorings
No refactoring yet !
bob
October 4, 2011, October 04, 2011 23:03, permalink
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main()
{
//would rather have something more fitting like a vector of pairs
vector<pair<string,string> > question {{"What is your name?", "thaostra"},
{"What is your quest?", "make this program better"},
{"What is your favourite color?", "black"}};
string input;
auto incorrect(0);
//want a range-based for succinctness
for(auto x : question){
cout << x.first;
getline(cin, input);
if(input != x.second){
incorrect++;
cout << "Correct answer is: " << x.second << endl;
}
}
cout << "Number correct out of " << question.size() << ": "
<< question.size() - incorrect << endl;
return 0;
}
Ants
October 5, 2011, October 05, 2011 02:02, permalink
More verbose but goes down the path of object orientation...
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
class QuestionAnswer
{
public:
QuestionAnswer(const string & question,
const string & answer)
: _question(question)
, _answer(answer)
{
}
const string & GetQuestion() const
{
return _question;
}
const string & GetAnswer() const
{
return _answer;
}
bool IsAnswerCorrect(const string & candidate) const
{
return !_answer.compare(candidate);
}
private:
string _question;
string _answer;
};
int main()
{
//would rather have something more fitting like a vector of pairs
vector<QuestionAnswer> questions
{
QuestionAnswer("What is your name?", "thaostra"),
QuestionAnswer("What is your quest?", "make this program better"),
QuestionAnswer("What is your favourite color?", "black"),
};
string input;
auto incorrect(0);
for(auto x : questions)
{
cout << x.GetQuestion();
getline(cin, input);
if(x.IsAnswerCorrect(input))
{
incorrect++;
cout << "Correct answer is: " << x.GetAnswer() << endl;
}
}
cout << "Number correct out of " << questions.size() << ": "
<< questions.size() - incorrect << endl;
return 0;
}
thaostra.myopenid.com
October 6, 2011, October 06, 2011 07:58, permalink
These code refactorings have been very helpful, especially since they helped show the correct syntax for the vector of pairs. Thank you very much!
//compiles on gcc-4.6.1
//g++ -W -Wall -Wextra -O2 -pedantic -std=c++0x -o quiz ./quiz.cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<pair<string,string>> question {{"What is your name?", "thaostra"},
{"What is your quest?", "make this program better"},
{"What is your favourite color?", "black"}};
string input;
auto incorrect(0);
for(auto x : question){
cout << x.first << " ";
getline(cin, input);
if(input != x.second){
incorrect++;
cout << "Correct answer is: " << x.second << endl;
}
}
cout << "Number correct out of " << question.size() << ": "
<< question.size() - incorrect << endl;
}
thaostra.myopenid.com
October 6, 2011, October 06, 2011 07:59, permalink
These code refactorings have been very helpful, especially since they helped show the correct syntax for the vector of pairs. Thank you very much!
//compiles on gcc-4.6.1
//g++ -W -Wall -Wextra -O2 -pedantic -std=c++0x -o quiz ./quiz.cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<pair<string,string>> question {{"What is your name?", "thaostra"},
{"What is your quest?", "make this program better"},
{"What is your favourite color?", "black"}};
string input;
auto incorrect(0);
for(auto x : question){
cout << x.first << " ";
getline(cin, input);
if(input != x.second){
incorrect++;
cout << "Correct answer is: " << x.second << endl;
}
}
cout << "Number correct out of " << question.size() << ": "
<< question.size() - incorrect << endl;
}
thaostra.myopenid.com
October 6, 2011, October 06, 2011 08:31, permalink
OOPS! Accidentally created a double post.
This is a working game, but I am not satisfied with it as you can tell from the comments. Also, I'm not sure how to have this be able to correctly read questions and answers from a configuration file.