Questions.cpp 3.74 KB
Newer Older
Jared Sexton's avatar
Jared Sexton committed
1
2
3
4
5
6
/*  ============================================================================
        Question class function file
        Jared Sexton
    ============================================================================
*/

7
#include "Questions.h"
Jared Sexton's avatar
Jared Sexton committed
8
9
10
#include <iostream> // For cout and cin
#include <cstdlib> // For srand and rand
#include <ctime> // For time(0)
Jared Sexton's avatar
Jared Sexton committed
11
using namespace std;
Jared Sexton's avatar
Jared Sexton committed
12

Jared Sexton's avatar
Jared Sexton committed
13
bool Questions::mainQuestionAsk(int level) {
Jared Sexton's avatar
Jared Sexton committed
14
15
16
17
18
19
20
21
22
    if((level >= 1) && (level <= 3)) {
        return countingQuestion(level);
    }
    else if ((level >= 4) && (level <= 10)) {
        return addSubQuestion(level);
    }
    else {
        return wordQuestion(level);
    }
Jared Sexton's avatar
Jared Sexton committed
23
}
Jared Sexton's avatar
Jared Sexton committed
24

Jared Sexton's avatar
Jared Sexton committed
25
bool Questions::countingQuestion(int level) {
Jared Sexton's avatar
Jared Sexton committed
26
    srand(time(0));
Jared Sexton's avatar
Jared Sexton committed
27
    int countTo;
Jared Sexton's avatar
Jared Sexton committed
28
29
    switch(level) {
        case 1:
Jared Sexton's avatar
Jared Sexton committed
30
            countTo = rand() % 5;
Jared Sexton's avatar
Jared Sexton committed
31
32
            break;
        case 2:
Jared Sexton's avatar
Jared Sexton committed
33
            countTo = rand() % 10;
Jared Sexton's avatar
Jared Sexton committed
34
35
            break;
        case 3:
Jared Sexton's avatar
Jared Sexton committed
36
            countTo = rand() % 20;
Jared Sexton's avatar
Jared Sexton committed
37
            break;
Jared Sexton's avatar
Jared Sexton committed
38
        default:
Jared Sexton's avatar
Jared Sexton committed
39
            cerr << "Please see your teacher about your current level.";
Jared Sexton's avatar
Jared Sexton committed
40
            return false;
Jared Sexton's avatar
Jared Sexton committed
41
42
43
            break;
    }
    int answer;
Jared Sexton's avatar
Jared Sexton committed
44
    countTo++; //To set the possible range to 1 -> maximum, instead of 0 -> (maximum -1)
Jared Sexton's avatar
Jared Sexton committed
45
46
47
48
49
50
    for(int i = 0; i < countTo; i++) {
        if(countTo % 2 == 0)
            cout << "X ";
        else 
            cout << "# ";
    }
Jared Sexton's avatar
Jared Sexton committed
51
    cout << "\nHow many symbols are there? ";
Jared Sexton's avatar
Jared Sexton committed
52
53
54
55
56
    if(!(cin >> answer)) {
      cin.clear();
      cin.ignore(256,'\n');
      answer = -1;
    }
Jared Sexton's avatar
Jared Sexton committed
57
    if(countTo == answer) {
Jared Sexton's avatar
Jared Sexton committed
58
        cout << "Good job!\n";
Jared Sexton's avatar
Jared Sexton committed
59
60
61
        return true;
    }
    else
Jared Sexton's avatar
Jared Sexton committed
62
        cout << "Better luck next time.\n";
Jared Sexton's avatar
Jared Sexton committed
63
64
65
        return false;
}

Jared Sexton's avatar
Jared Sexton committed
66
bool Questions::addSubQuestion(int level) {
Jared Sexton's avatar
Jared Sexton committed
67
    srand(time(0));
Jared Sexton's avatar
Jared Sexton committed
68
69
    int maxNumber;
    switch(level) {
Jared Sexton's avatar
Jared Sexton committed
70
71
        case 4: // 4's
            maxNumber = 4;
Jared Sexton's avatar
Jared Sexton committed
72
            break;
Jared Sexton's avatar
Jared Sexton committed
73
74
        case 5: // 5's
            maxNumber = 5;
Jared Sexton's avatar
Jared Sexton committed
75
            break;
Jared Sexton's avatar
Jared Sexton committed
76
77
        case 6: // 6's
            maxNumber = 6;
Jared Sexton's avatar
Jared Sexton committed
78
            break;
Jared Sexton's avatar
Jared Sexton committed
79
80
        case 7: // 7's
            maxNumber = 7;
Jared Sexton's avatar
Jared Sexton committed
81
            break;
Jared Sexton's avatar
Jared Sexton committed
82
83
        case 8: // 8's
            maxNumber = 8;
Jared Sexton's avatar
Jared Sexton committed
84
            break;
Jared Sexton's avatar
Jared Sexton committed
85
86
        case 9: // 9's'
            maxNumber = 9;
Jared Sexton's avatar
Jared Sexton committed
87
            break;
Jared Sexton's avatar
Jared Sexton committed
88
89
        case 10: // 10's
            maxNumber = 10;
Jared Sexton's avatar
Jared Sexton committed
90
91
            break;
        default:
Jared Sexton's avatar
Jared Sexton committed
92
            cerr << "Please see your teacher about your current level.\n";
Jared Sexton's avatar
Jared Sexton committed
93
            return false;
Jared Sexton's avatar
Jared Sexton committed
94
95
            break;
    }
Jared Sexton's avatar
Jared Sexton committed
96
    
Jared Sexton's avatar
Jared Sexton committed
97
    // Initialize variables
Jared Sexton's avatar
Jared Sexton committed
98
    int userAnswer, correctAnswer;
Jared Sexton's avatar
Jared Sexton committed
99
100
    int term1, term2;
    bool add = true;
Jared Sexton's avatar
Jared Sexton committed
101
102
103
104
105
106
    
    // Determine if addition or subtraction
    if((rand() % 2) == 0) {
        add = false;
    }
    
Jared Sexton's avatar
Jared Sexton committed
107
108
109
110
    // Make sure term1 is greater than term2 within the desired range.
    do {
        term1 = (rand() % maxNumber) + 1;
        term2 = (rand() % maxNumber) + 1;
Jared Sexton's avatar
Jared Sexton committed
111
    } while((term1 < term2) || ((term1 + term2) > maxNumber));
Jared Sexton's avatar
Jared Sexton committed
112
    
Jared Sexton's avatar
Jared Sexton committed
113
    // Ask the Question
Jared Sexton's avatar
Jared Sexton committed
114
115
116
117
118
119
    if(add) {
        correctAnswer = term1 + term2;
        cout << "\t" << term1 << endl;
        cout << "+\t" << term2 << endl;
        cout << "-----------" << endl;
        cout << "\t";
Jared Sexton's avatar
Jared Sexton committed
120
121
122
123
124
        if(!(cin >> userAnswer)) {
            cin.clear();
            cin.ignore(256,'\n');
            userAnswer = -1;
        }
Jared Sexton's avatar
Jared Sexton committed
125
126
127
128
129
130
131
    }
    else {
        correctAnswer = term1 - term2;
        cout << "\t" << term1 << endl;
        cout << "-\t" << term2 << endl;
        cout << "-----------" << endl;
        cout << "\t";
Jared Sexton's avatar
Jared Sexton committed
132
133
134
135
136
        if(!(cin >> userAnswer)) {
            cin.clear();
            cin.ignore(256,'\n');
            userAnswer = -1;
        }
Jared Sexton's avatar
Jared Sexton committed
137
    }
Jared Sexton's avatar
Jared Sexton committed
138
139
140
141
142
143
144
145
146
147
    
    if(userAnswer == correctAnswer) {
        cout << "Good job!\n";
        return true;
    }
    else
        cout << "Better luck next time.\n";
        return false;
    
}
Jared Sexton's avatar
Jared Sexton committed
148

Jared Sexton's avatar
Jared Sexton committed
149
bool Questions::wordQuestion(int level) {
Jared Sexton's avatar
Jared Sexton committed
150
151
    cout << "Question type not configured.";
    return false;
Jared Sexton's avatar
Jared Sexton committed
152
}