Recursion Problem I think

This is a discussion on Recursion Problem I think within the C Programming forums, part of the General Programming Boards category; Hello all, I am working on an assignement for school and is just about finished however I have one problem ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    16

    Recursion Problem I think

    Hello all,

    I am working on an assignement for school and is just about finished however I have one problem left to tackle (or only one I have found anyway).

    We have to write a very limited billing program for a cable company that stores data in arrays and we are to use functions for every menu procedure.

    Anyway my problem is my switch statements I think. We are supposed to prevent the user from accidently overwriting an exisiting account. when the user inputs an account number to add (0-9) if it is assigned an account type of anything other than "3" the program assumes it is an exisiting account and asks the user if they wish to overwrite or select anopther account number. Overwriting the file works fine, however if the user selects no I call the function again to restart the procedure of collecting account information, this seems to happen, however once you complete the account information and the account is setup it starts over one more time and you have to do the process again so it lets you exit that portion of the program. I have been looking at the code for sometime and am not sure what I did wrong. Any assistance would be helpful.

    This is the function that is causing me trouble

    Code:
    int addAcct()
    {
    	//declare local variables
    	int i = 0;
    	int acctNUM = 10;
    	int acctTYPE = 2;
    	int acctPREM;
    	char overWrite;
    	int rerun = 0;
    
    	//loops the add acount menu until a valid account number is chosen
    	while (acctNUM > 9){
    		system ("cls");
    		printf("\n\n	Cable Dude's Account Management \n\n");
    		printf("		** Add Account ** \n\n");
    		printf("		Please select an account number 0 - 9 \n");
    		printf("\n	Please make a selection and press <Enter> ");
    		scanf("%d", &acctNUM);
    		
    		//checks to see if the account already exists and give the option to overwrite the data
    		if (cableAcct[acctNUM][0] != 3){
    			printf("\n\n	** Account %d already exists, overwirte? (y/n)  **\n",acctNUM);
    			getchar();
    			overWrite = getchar();
    
    			//checks input to see if the user chose overwrite or not
    			switch (overWrite){
    			 case 'y' :
    				 break;
    			 case 'Y' :
    				 break;
    			 case 'n' :
    				rerun = addAcct();
    				break;
    			 case 'N' :
    				rerun = addAcct();
    				break;
    			}
    		}
    	continue;}
    
    	//loops until a valid account type is chosen
    	while (acctTYPE > 1) {
    		system ("cls");
    		printf("\n\n	Cable Dude's Account Management \n\n");
    		printf("		** Add Account ** \n\n");
    		printf("		Please select an account type \n");
    		printf("		0 - Residential \n");
    		printf("		1 - Commercial \n");
    		printf("\n	Please make a selection and press <Enter> ");
    		scanf("%d", &acctTYPE);
    		continue;}
    
    		//clears the previous screen and displays the premium channel selection
    		system ("cls");
    		printf("\n\n	Cable Dude's Account Management \n\n");
    		printf("		** Add Account ** \n\n");
    		printf("		Please enter the amount of premium channels \n");
    		printf("\n	Please make a selection and press <Enter> ");
    		scanf("%d", &acctPREM);
    
    		//asigns the temporay local variable to the correct array location
    		cableAcct[acctNUM][0] = acctTYPE;
    		cableAcct[acctNUM][1] = acctPREM;
    
    		printf("\n\n	Account successfully added press any key to continue ");
    			getchar();
    			getchar();
    return 0;
    tia

    clearrtc

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if the user selects no I call the function again to restart the procedure
    That's not a good use of recursion. How does calling addAcct again solve a subset of the problem? It solves the same problem, not a smaller problem, which screams iteration. I'd recommend that you try to redesign your algorithm using loops.
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you calling the function inside itself? It should be something like:
    Code:
    do
        display menu
        prompt for choice
        handle choice
    while choice == continue
    That should be your whole function. You don't need to keep calling 'addAcct', because you're not leaving it. You just stay in the function, and loop back to the start of it. No need to keep calling it over again. Just exit (return) from the function when it's time, otherwise stay in it.


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

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    16
    Well I was trying to find a spot to use both a switch statement and recursion as they are required in the assignment. This seemed like the most likely place.

    clearrtc

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have to use recursion? Hm... use it to get "yes" or "no" to continue or not. If they didn't enter a valid command, reprompt for it using recursion. Return the valid command once it's entered. You could even make it more generic, and pass a list of valid commands, and recurse until they've entered one. Return the valid choice, otherwise recurse.

    Then use a switch with your command, to handle the valid command. Just a thought.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion problem
    By trnd in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 03:06 PM
  3. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 04:52 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. recursion problem
    By dustinc in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 04:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21