From ead5cb67fec3ef0165e817d956fcb667f0ad337c Mon Sep 17 00:00:00 2001 From: Jared Sexton Date: Fri, 2 Mar 2018 01:10:17 +0000 Subject: [PATCH] Update Questions --- Question_driver.cpp | 8 +++--- Questions.cpp | 68 ++++++++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/Question_driver.cpp b/Question_driver.cpp index 799c4a3..c7c5cca 100644 --- a/Question_driver.cpp +++ b/Question_driver.cpp @@ -11,10 +11,10 @@ using namespace std; int main() { Questions questionList; - questionList.addSubQuestion(4); - questionList.addSubQuestion(4); - questionList.addSubQuestion(4); - questionList.addSubQuestion(4); + questionList.addSubQuestion(7); + questionList.addSubQuestion(7); + questionList.addSubQuestion(7); + questionList.addSubQuestion(7); return 0; } \ No newline at end of file diff --git a/Questions.cpp b/Questions.cpp index 40425fb..60e3c8a 100644 --- a/Questions.cpp +++ b/Questions.cpp @@ -5,8 +5,9 @@ */ #include "Questions.h" -#include -#include +#include // For cout and cin +#include // For srand and rand +#include // For time(0) using namespace std; void Questions::mainQuestionAsk(int level) { @@ -41,7 +42,11 @@ bool Questions::countingQuestion(int level) { cout << "# "; } cout << "\nHow many symbols are there? "; - cin >> answer; + if(!(cin >> answer)) { + cin.clear(); + cin.ignore(256,'\n'); + answer = -1; + } if(countTo == answer) { cout << "Good job!\n"; return true; @@ -52,56 +57,63 @@ bool Questions::countingQuestion(int level) { } bool Questions::addSubQuestion(int level) { + srand(time(0)); int maxNumber; switch(level) { - case 4: // 5's - maxNumber = rand() % 4; + case 4: // 4's + maxNumber = 4; break; - case 5: - maxNumber = rand() % 5; + case 5: // 5's + maxNumber = 5; break; - case 6: - maxNumber = rand() % 6; + case 6: // 6's + maxNumber = 6; break; - case 7: - maxNumber = rand() % 7; + case 7: // 7's + maxNumber = 7; break; - case 8: - maxNumber = rand() % 8; + case 8: // 8's + maxNumber = 8; break; - case 9: - maxNumber = rand() % 9; + case 9: // 9's' + maxNumber = 9; break; - case 10: - maxNumber = rand() % 10; + case 10: // 10's + maxNumber = 10; break; default: cerr << "Please see your teacher about your current level.\n"; break; } + // Initialize variables int userAnswer, correctAnswer; int term1, term2; bool add = true; + + // Determine if addition or subtraction + if((rand() % 2) == 0) { + add = false; + } + // Make sure term1 is greater than term2 within the desired range. do { term1 = (rand() % maxNumber) + 1; term2 = (rand() % maxNumber) + 1; - } while(term1 < term2); - - // Determine if addition or subtraction - // if((term1 + term2) < (maxNumber / 2)) { - // add = false; - // } - // NOT WORKING + } while((term1 < term2) || ((term1 + term2) > maxNumber)); + // Ask the Question if(add) { correctAnswer = term1 + term2; cout << "\t" << term1 << endl; cout << "+\t" << term2 << endl; cout << "-----------" << endl; cout << "\t"; - cin >> userAnswer; + if(!(cin >> userAnswer)) { + cin.clear(); + cin.ignore(256,'\n'); + userAnswer = -1; + } } else { correctAnswer = term1 - term2; @@ -109,7 +121,11 @@ bool Questions::addSubQuestion(int level) { cout << "-\t" << term2 << endl; cout << "-----------" << endl; cout << "\t"; - cin >> userAnswer; + if(!(cin >> userAnswer)) { + cin.clear(); + cin.ignore(256,'\n'); + userAnswer = -1; + } } if(userAnswer == correctAnswer) { -- GitLab