im making a random maths game, which works correctly however ive started to add a menu system which also works fine.
after another programmer looked at my code he suggested having my code in a function. instead of copying my code into each case?
ive read the tutoirail and get the point of functions but im not sure how to excute it for use in my code?
could you please show me an example i could use it with in my code? to reduce the size of the code & make it easier to manage?
Thanks.
Code:
// loop.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
//Variables
int number1;
int number2;
int Questions;
int USER;
int QN;
int CorrectAnswers = 0;
char End;
int CPU = 0;
int input;
//Variable End
//Menu System
printf("===========================================\n");
printf("AppGecko Random Maths V0.30\n");
printf("===========================================\n");
printf("1. Addition\n");
printf("2. Subtraction - Not Complete\n");
printf("3. Division - Not Complete\n");
printf("4. Multiplication - Not Complete\n");
scanf( "%d", &input );
//Menu System Ends//
switch ( input ) {
case 1: // Addition
printf("How many questions would you like to answer?: ");
scanf("%d", &Questions); //Saves User input to Questions Int.
printf("you selected: %d Questions to answer\n\n", Questions); //Tells you how many you selected
//Loop For amount of questions selected
for (QN = 0; QN < Questions; QN++ ) {
number2 = rand() %50; //Random number generator
number1 = rand() %90; //Random Number Generator
CPU = (number1 + number2); //Calculate Answer
//Asks the Question & Gets Answer
printf("what is %d plus %d\n",number1,number2);
printf("Enter Your Answer: ");
scanf_s("%d", &USER);
// Checks if Answer is correct & Responds accordingly
if (USER == CPU) {
printf("\nYou are Right\n\n");
CorrectAnswers = CorrectAnswers +1;
}
else {
printf("\nYou are Wrong\n\n");
}
number2 = 0;
number1 = 0;
CPU = 0;
printf("Next Question\n\n");
}
printf("You have Answered %d out of %d Correct\n",CorrectAnswers, Questions);
printf("Press Any Key to End");
scanf("%d",End);
getchar();
break;
case 2:
printf("How many questions would you like to answer?: ");
scanf("%d", &Questions); //Saves User input to Questions Int.
printf("you selected: %d Questions to answer\n\n", Questions); //Tells you how many you selected
//Loop For amount of questions selected
for (QN = 0; QN < Questions; QN++ ) {
number2 = rand() %50; //Random number generator
number1 = rand() %90; //Random Number Generator
CPU = (number1 - number2); //Calculate Answer
//Asks the Question & Gets Answer
printf("what is %d minus %d\n",number1,number2);
printf("Enter Your Answer: ");
scanf_s("%d", &USER);
// Checks if Answer is correct & Responds accordingly
if (USER == CPU) {
printf("\nYou are Right\n\n");
CorrectAnswers = CorrectAnswers +1;
}
else {
printf("\nYou are Wrong\n\n");
}
number2 = 0;
number1 = 0;
CPU = 0;
printf("Next Question\n\n");
}
printf("You have Answered %d out of %d Correct\n",CorrectAnswers, Questions);
printf("Press Any Key to End");
scanf("%d",End);
getchar();
break;
default:
printf( "Bad input, Please Select Again" );
break;
}
return 0;
}