One last question. how do I get the last bit of my code to repeat the application once its finished assuming the user clicks Y?
Code:
//
#include <stdio.h> //commands that essentially pastes previously written code into your program eg printf
#include <stdlib.h> //Defines numeric conversion functions, pseudo-random numbers generation functions and memory allocation
#include <time.h> //Allows the user to use srand and define time and date values
#include <conio.h> //Allows the program to use getch();
#include <ctype.h> //
int main(void) //where the program start the execution
{
int a; //This is where we will temporarily store a value for the first random operand
int b; //This is where we will temporarily store a value for the second random operand
int sum, guess; //An array to store the users inputed answer(guess) and where to compare it to the sum(correct answer)
int numTests; //A counter for 2 parts of the application. One for Question Number and for the loop
int correct; //A counter to add the correct answers
correct=0; //This is so the application knows to start the score at 0 answers correct
float percentage; //Part of the formula to calulate the final percentage of correct answers
srand((unsigned char)time(NULL)); //Seeds the random number generator
for ( numTests = 1 ; numTests < 11 ; numTests++ ) //This is to start the loop and to stop at Q10 by adding one each time the loop has been executed
{
a = rand()%12+1; //Generates a random number between 1-12 for operand 1
b = rand()%12+1; //Generates a random number between 1-12 for operand 2
sum = a * b; //Calculates the sum by multiplying the 2 random numbers above
printf("Q%d What is %d x %d\?\n",numTests,a,b); //Will ask the user the maths question using random numbers
if(scanf("%d", &guess) != 1) //This will ask the computer to see if a number has been been inputed and not a character
printf("Incorrect input.\n"); //Error message if a character has been entered for the maths question
else if(guess == sum) //Compares users guess with the correct sum
{
printf("CORRECT\n"); //Prints CORRECT promt if answer by user is correct
correct++; //Adds 1 to the loop
}
else //If the user has not entered a character or a correct answer then this command is executed
{
printf("INCORRECT\n"); //Incorrect prompt for user
}
}
{
percentage=(float)correct/(numTests-1)*100; //Formula to work out final percentage at end of test
printf("You have answered %f %% of the Questions Correctly.", percentage); //Promt telling user percentage mark
}
{
char ch; //Assigns the Y or N to the computers memory
printf("\n\n\n Another Go? (Y/N)"); //Promt asking user if they want another go
do
{
ch=getch(); //Promts the user to press a character which is not entered on screen
ch=toupper(ch); //Changes an inputted characted into upper case
}
while (ch!='Y' && ch!='N'); //If the user inputs y or n then the compiler will execute the above comand and convert to uppercase
}
}