i'm trying to reduce my code by using functions (learning them atm) now ive created my functions but there coming up with errors (not on build process) but on the switch case. when i press a menu option say 1 it says ive not initialized Questions but not sure how?
if i put questions = 0; i get bad input. need advice
please be down to earth as possible as i'm new to this.
Code:
// loop.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
//Functions
int FirstNumber (int number1, int number2); //Random number generator
int Questions1 (void)
{
int Q=0;
printf("How many questions would you like to answer?: ");
scanf("%d", &Q); //Saves User input to Questions Int.
printf("you selected: %d Questions to answer\n\n", Q); //Tells you how many you selected
return Q;
}
int Menu (void)
{
int input=0;
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);
return input;
}
int _tmain(int argc, _TCHAR* argv[])
{
//Variables
int number1;
int number2;
int Questions;
int USER;
int QN;
int CorrectAnswers = 0;
int CPU = 0;
int input = 0;
//Variable End
//Menu System
Menu();
//Menu System Ends//
switch ( input ) {
case 1: // Addition
Questions = Questions1();
//Loop For amount of questions selected
for (QN = 0; QN < Questions; QN++ ) {
number2 = rand(); //Random number generator
number1 = rand(); //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");
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(); //Random number generator
number1 = rand(); //Random Number Generator
//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");
}
break;
default:
printf( "Bad input, Please Select Again" );
break;
}
printf("You have Answered %d out of %d Correct\n",CorrectAnswers, Questions);
return 0;
}