[basic C++] recursion and memory usage and program design

This is a discussion on [basic C++] recursion and memory usage and program design within the C++ Programming forums, part of the General Programming Boards category; Hello everybody. for practise a little bit. i build a basic vendor machine program (code below). i have to questions: ...

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55

    [basic C++] recursion and memory usage and program design

    Hello everybody.

    for practise a little bit. i build a basic vendor machine program (code below). i have to questions:

    1. Recursion in function chooseproduct and memory usage.
    2. Design

    1.

    As you can see i made a method called "chooseproduct", that allow the user to make a choice of the product it wants to buy. if the choice is a valid product it "returns the product" and then let the user choose another product (like real vendor machines). and there is a exit command and a secret admin option. only when the exit command is entered the program quits.

    I am not sure about the recursion I use. the program works as I expected, but if this program would be used on a real vendingmachine it would have less memory recources. and so, I am afraid that the recursion of the chooseproduct function could cause a "out of memory" exception after long time of use (so it wont be quited and there will be a lot of users). Because, i learned that everytime the function calls itself, i would create a copy of the function in the stack. so after 10 times it would have 10 copies of the function chooseproduct in memory, but the function causes a return statement, and when return i called the function will be "dropped" from the memory stack or.... ?

    So, waht do you guys/girls think about it, is the recursion no problem because it drops itself caused by return of could we expect a memory problem if this program would run on a real vendingmachine?

    2.

    I study the basics of C++ it would be very helpfull if you guys/girls would help me with some tips, suggestion, etc. about the program design, pittfalls etc. etc.

    Thanks in advanced

    Jelte.

    main.cpp
    Code:
    #include "machine.h"
    
    int main()
    {
    	machine objmachine;
    	return 0;
    }
    machine.cpp
    Code:
    #include "machine.h"
    
    machine::machine()
    {
    	menu();
    }
    
    machine::~machine()
    {
    }
    
    void machine::menu() const
    {
    	cout << "producten:" 		<< endl;
    	cout << "1. cola" 			<< endl;
    	cout << "2. sinas" 			<< endl;
    	cout << "3. dubbelfris" 	<< endl;
    	cout << "4. sprite" 		<< endl;
    	cout << "5. bosbubbel"  	<< endl;
    	cout << "99. sluit machine" << endl;
    	chooseproduct();	
    }
    
    int machine::chooseproduct(unsigned int chosenproduct) const
    {
    	int product = 0;
    	if(chosenproduct == 0)
    	{
    		cout << "kies product" << endl;
    		cin >> product;
    		return chooseproduct(product);
    	}
    	else
    	{
    		switch(chosenproduct)
    		{
    			case cola:
    				cout << "uitgave product:\tcola" 	   << endl;
    				return chooseproduct();
    			case sinas:
    				cout << "uitgave product:\tsinas" 	   << endl;
    				return chooseproduct();
    			case dubbelfris:
    				cout << "uitgave product:\tdubbelfris" << endl;
    				return chooseproduct();
    			case sprite:
    				cout << "uitgave product:\tsprite" 	   << endl;
    				return chooseproduct();
    			case bosbubbel:
    				cout << "uitgave product:\tbosbubbel"   << endl;
    				return chooseproduct();
    			case exit:
    				cout << "machine wordt afgesloten!" 	<< endl;
    				break;
    			case beheerder: 
    				cout << "beheer!" 						<< endl;
    				return chooseproduct();  
    			default:
    				cout << "ongeldige keuze, kies opnieuw:" << endl;
    				cin >> product;
    				return chooseproduct(product);
    		}
    		return 0;	
    	}	
    }
    machine.cpp
    Code:
    #include <iostream>
    
    using namespace std;
    
    class machine
    {
    	public:
    		machine();
    		~machine();	
    	private:	
    		enum products {cola = 1, sinas = 2, dubbelfris =3, sprite = 4, bosbubbel = 5 };
    		enum commands {exit = 99, beheerder = 79315};
    		void menu() const;
    		int chooseproduct(unsigned int chosenproduct = 0) const; 
    };
    Last edited by Jelte; 01-12-2011 at 02:35 PM.
    The Programming Dutchman

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    458
    you can use the while or do-while loop inside your chooseproduct() function. in each case of switch, you need "break; "
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Thanks, While loop.. ofcourse! stupid i didnt thought about that one. I updated the chooseproduct function.

    chooseproduct
    Code:
    int machine::chooseproduct() const
    {
    	int choice = 0;
    	while(true)
    	{
    		if(choice == 0)
    		{
    			cout << "kies product" << endl;
    			cin >> choice;
    			continue;
    		}
    		else
    		{
    			switch(choice)
    			{
    				case cola:
    					cout << "uitgave product:\tcola" 	   << endl;
    					choice = 0;
    					break;
    				case sinas:
    					cout << "uitgave product:\tsinas" 	   << endl;
    					choice = 0;
    					break;
    				case dubbelfris:
    					cout << "uitgave product:\tdubbelfris" << endl;
    					choice = 0;
    					break;
    				case sprite:
    					cout << "uitgave product:\tsprite" 	   << endl;
    					choice = 0;
    					break;
    				case bosbubbel:
    					cout << "uitgave product:\tbosbubbel"   << endl;
    					choice = 0;
    					break;
    				case exit:
    					cout << "machine wordt afgesloten!" 	<< endl;
    					return 0;
    				case beheerder: 
    					cout << "beheer!" 						<< endl;
    					choice = 0;
    					break;  
    				default:
    					cout << "ongeldige keuze, kies opnieuw:" << endl;
    					cin >> choice;
    					break;
    			}			
    		}	
    	}
    }
    Suggestions and tips are still welcome

    Greetings

    Jelte.
    The Programming Dutchman

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by nimitzhunter View Post
    you can use the while or do-while loop inside your chooseproduct() function. in each case of switch, you need "break; "
    As he has return there the OP donst need to place in the break. Break is only needed when you have to break the control executing the next case. With the return you just exit the function without executing the following case within the switch.

    EDIT: if you use while or do-while then ignore my above comment.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I doubt if your orginal code would even compiler. You call chooseproduct() with no parameter in. Think you will have to call the function choice rather than choserproduct.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Thanks for your reply!

    Sorry but I think i don't totally understand your last reply, my fault i guess. but if guess you mean that you are not sure if the original code would compile because the function: int chooseproduct(unsigned int chosenproduct = 0) const; doesn't always gets a argument. the answer is, yes it works, because:

    int chooseproduct(unsigned int chosenproduct = 0) const; means that is this is a function that returns a int,and it isn't allowed to change members of the object/class where it is declared in, and takes one usigned int as argument. But unsigned int chosenproduct = 0 is a declaration of a standard parameter value, so if the function is called without a argument (chooseproductchooseproduct() the value of the parameter is 0.

    Correct me if i am wrong

    I used this method because i used recursion in the first place and on this way I was able to handle the product choice completely inside the function (when the program started (no input), when the user chose a product (return product and let the "next" user enter her/his choice (no input), and incorrect choices (no user input)). But the while loop is a more elegant way of doing this i guess.

    about the function name, yes. i think you are right about that because the function not only allow the user to choose the product he/she wants, but also quits the program and "login" into the admin menu (under construction).

    Thanks,

    Greetings,

    Jelte.
    Last edited by Jelte; 01-13-2011 at 12:25 PM.
    The Programming Dutchman

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  2. oooh how much fun recursion is...
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-05-2004, 06:05 PM

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