Thread: Using Functions/Switch Cases? (in my code)

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Question Using Functions/Switch Cases? (in my code)

    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;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    switch( choice )
    {
        case ADD: doadd( ); break;
        case SUBTRACT: dosubtract( ); break;
        ... and so on
    }
    Then make each of those functions.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 04:28 PM
  2. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread