hw6.h
//Filename: hw6.h Class: CS 1570
//Description: Contains header information for the quiz program
#ifndef HW6_H
#define HW6_H
#include<cmath>
#include<iostream>
using namespace std;
const int MIN_EASY = 0;
const int MAX_EASY = 9;
const int MIN_MODEREATE = 10;
const int MAX_MODEREATE = 99;
const float MIN_ADVANCED = 0;
const float MAX_ADVANCED = 99.99;
const int PLACES = 2; // number of decimal places for floats,
// should be between 1 and 6
const float PRECISION = 1.0/pow(10,PLACES); //Do not change this one. Ever.
const int NUM_PROBLEMS = 3;
const int MAX_TRIES = 2;
//This function greets the user
//Pre: None
//Post: Greeting is output to the screen
void greeting();
//This function displays the menu and gets the user's choice
//Pre: None
//Post: Menu is output and user's choice is returned
char menu();
//This function gets a random integer
//Pre: max must be greater than (or equal to) min
//Post: returns a random int between min and max (inclusive)
int randNum(const int min, const int max);
//This function gets a random float
//Pre: max must be greater than (or equal to) min
//Post: returns a random float between min and max (inclusive)
// with a number of decimal places based on the const DECIMAL_PLACES
float randNum(const float min, const float max);
//This function displays the results of the test to the user
//Pre: numProblems must not be 0 and should be greater than numCorrect
//Post: Displays the count and percentage to the user
void showResults(const int numCorrect, const int numProblems);
//This function says goodbye to the user
//Pre: None
//Post: Signoff is output to the screen
void signoff();
//This function presents the problem to the user and scores it.
//Pre: T must be defined for =, +, -, < float, and 'T randNum(T, T)'
//Post: Returns true if the user gave the correct answer, false otherwise
template<typename T>
bool theProblem(const T min, const T max);
//This function gives the user the test and gets the results.
//Pre: T must be defined for =, +, -, < float, and 'T randNum(T, T)'
//Post: Returns the number of correct answers.
template<typename T>
int runTest(const T min, const T max, const int numProblems);
template<typename T>
bool theProblem(const T min, const T max)
{
int tries = 0;
bool correct;
T lhs = randNum(min, max);
T rhs = randNum(min, max);
char oper = rand()%2 ? '+' : '-';
T answer = (oper == '+' ? lhs + rhs : lhs - rhs);
T userAnswer;
do
{
correct = false;
cout<<endl<<lhs<<' '<<oper<<' '<<rhs<<" = ";
cin>>userAnswer;
if(abs(userAnswer - answer) < PRECISION) //user was correct
{ //abs fixes rounding error
correct = true;
cout<<"\nCorrect!"<<endl;
}
else
{
tries++;
cout<<"\nIncorrect"<<(tries == MAX_TRIES ? "\n" : ", try again: ");
}
}while(!correct && tries < MAX_TRIES);
return correct;
}
template<typename T>
int runTest(const T min, const T max, const int numProblems)
{
int numCorrect = 0;
for(int i = 0; i < numProblems; ++i)
{
cout<<"\nProblem "<<i+1<<": ";
numCorrect += theProblem(min, max);
}
return numCorrect;
}
#endif