#include <iostream> #include <string> #include <algorithm> #include <cassert> us
ID: 3551007 • Letter: #
Question
#include <iostream>
#include <string>
#include <algorithm>
#include <cassert>
using namespace std;
#define UNIT_TESTING
struct Node {
string q;
string answer;
unsigned short pointvalue;
Node *next;
};
typedef Node* NodePtr;
bool addquest(NodePtr &node, unsigned short &nodecount);
void askquest(NodePtr &node, unsigned int &totalscore, bool test);
void unittesting();
void buildtestNodes(NodePtr &testnode);
int main () {
NodePtr root = new Node;
unsigned short nodecount = 3;
unsigned int totalscore = 0;
bool cont;
#ifdef UNIT_TESTING
unittesting();
#else
cout << "*** Welcome to Joshua Ando's trivia quiz game! *** ";
buildtestNodes(root);
while(addquest(root, nodecount)) { }
cout << endl;
for(int i = 0; i < nodecount; i++) askquest(root, totalscore, false);
cout << "*** Thank you for playing the trivia quiz game *** ";
#endif
return 0;
}
void buildtestNodes(NodePtr &testnode) {
NodePtr curr = new Node;
curr -> q = "How long was the shortest war on record? ";
curr -> q += "(Hint: how many minutes)";
curr -> answer = "38";
curr -> pointvalue = 100;
curr -> next = NULL;
testnode = curr;
curr = new Node;
curr -> q = "What was Bank of America's original name? ";
curr -> q += "(Hint: Bank of Italy or Bank of Germany)";
curr -> answer = "Bank of Germany";
curr -> pointvalue = 50;
curr -> next = testnode;
testnode = curr;
curr = new Node;
curr -> q = "What is the best-selling video game of all time? ";
curr -> q += "(Hint: Call of Duty or Wii Sports)";
curr -> answer = "Wii Sports";
curr -> pointvalue = 20;
curr -> next = testnode;
testnode = curr;
}
bool addquest(NodePtr &node, unsigned short &nodecount) {
string input;
unsigned short points = 0;
NodePtr curr = new Node;
cout << "Enter a question: ";
cin >> input;
curr -> q = input;
cout << "Enter an answer: ";
cin >> input;
curr -> answer = input;
cout << "Enter award points: ";
cin >> points;
curr -> pointvalue = points;
cout << "Continue? (Yes/No): ";
cin >> input;
curr -> next = NULL;
if(node == NULL)
node = curr;
else {
curr -> next = node;
node = curr;
}
nodecount++;
if(input == "YES")
return true;
else
return false;
}
void askquest(NodePtr &node, unsigned int &totalscore, bool test) {
string input;
string realanswer;
input = node -> q;
if(input == "") {
cout << "Warning - The number of trivia to be asked must "
<< "equal to or larger than 1. ";
#ifdef UNIT_TESTING
assert(true);
return;
#endif
return;
}
cout << "Question: " << node -> q << endl;
cout << "Answer: ";
cin >> input;
realanswer = node -> answer;
#ifdef UNIT_TESTING
if(test) {
if(node -> answer == input) {
cout << "Your answer is correct. You receive " << node -> pointvalue
<< " points. ";
totalscore += node -> pointvalue;
cout << "Your total points: " << totalscore << endl;
}
else if(node -> answer != input) {
cout << "Your answer is wrong. The correct answer is: "
<< realanswer << endl
<< "Your total points: " << totalscore << endl;
}
}
else if(totalscore == 0) {
assert(node -> answer != input);
cout << "Case 2.1 passed... " ;
totalscore = 1;
}
else if (totalscore == 1) {
assert(node -> answer == input);
cout << "Case 2.2 passed... ";
}
#else
if(node -> answer == input) {
cout << "Your answer is correct. You receive " << node -> pointvalue
<< " points. ";
totalscore += node -> pointvalue;
cout << "Your total points: " << totalscore << endl;
}
else if(node -> answer != input) {
cout << "Your answer is wrong. The correct answer is: "
<< realanswer << endl
<< "Your total points: " << totalscore << endl;
}
#endif
cout << endl;
node = node -> next;
}
void unittesting() {
NodePtr testnode = new Node;
unsigned int testcount = 0;
cout << "*** This is a debugging version *** ";
cout << "Unit Test Case 1: Ask no question. The program should"
<< " give a warning message. ";
askquest(testnode, testcount, false);
buildtestNodes(testnode);
cout << " Unit Test Case 2.1: Ask 1 question in the linked list. "
<< "The tester enters an incorrect answer. ";
askquest(testnode, testcount, true);
cout << "Unit Test Case 2.2: Ask 1 question in the linked list. "
<< "The tester enters a correct answer. ";
askquest(testnode, testcount, true);
cout << "Unit Test Case 3: Ask all the questions of the last trivia in "
<< "the linked list. ";
testnode = NULL;
buildtestNodes(testnode);
testcount = 0;
for(int i = 0; i < 3; i++)
askquest(testnode, testcount, true);
cout << "Unit Test Case 4: Ask five questions in the linked list. ";
cout << "Warning - there are only three trivia questions. ";
cout << "***End of the Debugging Version *** ";
}
It complise just right but when I run it, it doesnt let me answer the questions during the unit testing and it just says I got the wrong answer...any ideas??
Explanation / Answer
Hey i got your mistake.
While you reading answer you are using
cin >> input;
Which will take only a string not line eg when you type Wii Sport it will store only Wii not Sport.
So use this
getline(cin,input);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.