gothiclight's Blog - 17/March/2019 14:26:00


View Journal







c++ maths test source code, addition, subtraction

Same quiz source code put up earlier again but i modified to ask, subtraction and multiplication and whole number division on top of addition as well.
building upon Previous source code, actually not planning on writing a book but. Writing one as go along might be amusing, but might have been better just asking addition questions to be honest but hey ho. No to doing any more on this, maybe, perhaps as the device I did this code on, is not the same as the one I'm writing blogs on either, so yeah whatever, do I want to go far as writing exam qualification software, seemed nice gig for some companies who did and charged for issuing bit of paper electronic record after multiple choice. Or even listing a mark or grade lol
---
#include <iostream>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <time.h>
using namespace std;

/*
Written by Gareth Bryn Bowen garethbowen.co.uk, thesextree.co.uk in 17th March 2019
simple maths question test
I did actually start it in 22nd Feb 2019 but went no to c++ casting, left it alone and decided to come back to the machine to debug it
after reminding myself about g++ inter alia again
wrote it because of a dream and I am glad I did, as it reminded me how troublesome casts in c++ was, as not used it for aeons,
and i'm told a certain co star of carol vorderman (whom I have never met, don't know) was asked to write something similar once, but that info
was via clairaudience, and I wouldn't know if any of that information was true. if thier is any truth in that information what was her code like?
*/

//int to string conversation routine
std::string CastToString(int iID)
{

std::ostringstream oStream ;
oStream << iID;
std::string oString = oStream.str();
return oString;
};

std::string CastToString(double iID)
{

std::ostringstream oStream ;
oStream << iID;
std::string oString = oStream.str();
return oString;
};


std::string FormatCount(int iSeconds)
{
std::ostringstream oStream ;

if(iSeconds < 61)
{
oStream << iSeconds << " Seconds";
}else{
int iMins = iSeconds / 60;
iSeconds = iSeconds - (iMins * 60);
oStream << iMins << " Mins, " << iSeconds << " Seconds";
}
std::string oString = oStream.str();
return oString;

}

std::string FormatTime(tm * tm)
{
return asctime(tm);
//std::stringstream sDate ;
// sDate << "" << tm->tm_mday << '/' << (tm->tm_mon+1) << '/' << (tm->tm_year-100+2000) << ' ' << tm->tm_hour << ':' << tm->tm_min << ':' << tm->tm_sec;
//return sDate.str();

}

//I had a bug calling asctime in main after localtime
// as same time was alreadt returned, putting in function made different start, end times returned, hence why decided to encapasulate in a wrapper
std::string FormatTime(time_t t)
{
struct tm* sTM;
sTM = localtime(&t);
return FormatTime(sTM);
}


int main(int argc, char **argv) {

int iQUESTLIMIT = 100;

time_t tStartTime;
time(&tStartTime);
std::cout << "Maths Test, type quit to exist and score for running score or answer " << iQUESTLIMIT << " questions to complete" << std::endl;
std::cout << "go!" << std::endl;


int iScore =0, iTotalAsked = 0;
int iDifficultySetting = 5;
for(int iIndex=0;iIndex <=iQUESTLIMIT;iIndex++)
{
int iFirst, iSecond, iTotal;
srand(time(0));
int iQuestionType = rand() % iDifficultySetting;

std::ostringstream sQuestion ;
switch(iQuestionType)
{
case 1:
iFirst = rand() %50;
iSecond = rand() %50;
iTotal = (iFirst + iSecond);
sQuestion << CastToString(iFirst) << " + " << CastToString(iSecond) ;
break;
case 2:
iFirst = rand() %50;
iSecond = rand() %50;
iTotal = (iFirst - iSecond);
sQuestion << CastToString(iFirst) << " - " << CastToString(iSecond) ;
break;

case 3:
iFirst = rand() %16;
iSecond = rand() %16;
iTotal = (iFirst * iSecond);
sQuestion << CastToString(iFirst) << " * " << CastToString(iSecond) ;
break;
case 4:
iFirst = rand() %10;
iSecond = rand() %10;
if(iFirst <= 0)
{
iFirst = 1;
}
if(iSecond <= 0)
{
iSecond = 1;
}
iTotal = (iFirst * iSecond);
sQuestion << CastToString(iTotal) << " / " << CastToString(iFirst) ;
iTotal = iSecond;
default:
iFirst = rand() %50;
iSecond = rand() %50;
iTotal = (iFirst + iSecond);
sQuestion << CastToString(iFirst) << " + " << CastToString(iSecond) ;
break;
break;
}

string sAnswer;
std::cout << "What is the answer to " << sQuestion.str() ;
std::cout << "?" << std::endl;
getline( std::cin, sAnswer);

int iTotal = (iFirst + iSecond);
std::string sTotal = CastToString(iTotal);


while(sAnswer == "score")
{
//insert optional sarcasm routines here if they keep doing the same thing, out of scope currently for this, track a counter or something
std::cout << "you wish, get it at the end or when you quit! fine, go on then" << std::endl;
std::cout << " Questions answered correctly: " << iScore << " (" << iScore << "/" << iTotalAsked << ")" < std::cout << "answer the question please, as your clock is ticking" << std::endl; howavput
getline( std::cin, sAnswer);
}

if(sAnswer == "quit")
{break;}
else if(sAnswer == sTotal)
{ iScore++; iTotalAsked++;
std::cout << "checking your answer" << std::endl;
std::cout << "correct, well done" << std::endl;;
}else{iTotalAsked++;
std::cout << "checking your answer" << std::endl;
std::cout << " sorry incorrect " << sAnswer << " does not equal " << sTotal << std::endl;

}


};

if(iTotalAsked > 0)
{
time_t tEndTime;
time(&tEndTime);

int dTotalTime = difftime(tEndTime,tStartTime);
double dAverageTime = (dTotalTime /iTotalAsked);

std::cout << "Your Final Score " << iScore << "/" << iTotalAsked << std::endl;
std::cout << "Test started on:" << FormatTime(tStartTime);
std::cout << "Test finished on:" << FormatTime(tEndTime);
std::cout << "Total Time Taken: " << dTotalTime << std::endl;
std:cout << "Average Time for An Answer: " << FormatCount(dAverageTime) << std::endl;
if(iScore > 0)
{
dAverageTime = ((double)dTotalTime /iScore);
}else{dAverageTime = dTotalTime;}
std::cout<< "Average Time for each correct answer: " << FormatCount( dAverageTime) << std::endl;
std::cout << "Total questions asked: " << iTotalAsked << std::endl ;
std::cout << " Questions answered correctly: " << iScore << " (" << iScore << "/" << iTotalAsked << ")" < double dPercent =0.0;
if(iScore>0)
{
dPercent = ((double)iScore/iTotalAsked *100);
};
std::cout << " Your Percentage score: " << dPercent << "%" << std::endl;

}else{
std::cout << " i'm sorry you didn't want to ansqwer questions today, goodbye and au revior" << std::endl;

};
return 0;
};
https://blog.thesextree.co.uk/?entry=1854
Return To Journal
No Comments Found


Mobile Site
About Us | FAQ | Terms and Conditions | Privacy | Advertising | Contact Us | Seminars & Publicity
© 2007-2010,2013-20016 The Sex Tree All Rights Reserved