Thread: Functions broke my app. need advice

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

    Exclamation Functions broke my app. need advice

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

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You forgot to do something with the return value of Menu() - like assign it to 'input'. Otherwise 'input' never changes.

    input = Menu();

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    thank you so much

    i love you right now what a idiot i am

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance and pure virtual functions
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2010, 01:30 PM
  2. Functions calling other functions.
    By kbro3 in forum C++ Programming
    Replies: 2
    Last Post: 12-27-2009, 12:10 AM
  3. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM