Using return value functions and switch statement for c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kobe382
    New Member
    • Apr 2010
    • 5

    Using return value functions and switch statement for c++

    I want to write a program to control the action of a chocolate machine. Displaying a message to ask
    what kind of chocolate is required: Plain (30p), Brazil nut (35p), Luxury (50p). Us
    on the choice. Then money must be requested, in the form: “Please insert 30p “
    depending on the selection. this message must be displayed until the required amount has been
    input. If too much has been inserted, then a message indicating the amount of
    change owing should be displayed. And also display a message indicating ‘Thanks for Buying’. use the
    function and switch command.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Alright, how have you started with this program? Have you broken it down into steps?

    Does the "Please insert 30p" change with the money that the person has inserted? eg if they insert 1p, does it change to "Please insert 29p" ?

    Comment

    • kobe382
      New Member
      • Apr 2010
      • 5

      #3
      This is what i have!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!

      Code:
      #include<iostream>
      #include<cmath>
      
      using namespace std;
      
      int vend_selection(int, int, int);
      
      int main ()
      {
      	char vend1;
      	int price1, price2, price3;
      	cout << "Select Chocolate: \n" << "\nP. Plain (30p)\n" << "B. Brazil nut(35p)\n" << "L. Luxury (50p)\n" << endl;
      	cin >> vend1;
      	switch(vend1)
      	{
      	case 'P':
      		cout << "You have selected Plain chocolate" << endl;
              price1 = 0.30;
      		cout << "The price is: " << price1 << endl;
      		cout << "Please insert £0.30p" << endl;
              break;
      	case 'B':
      		cout << "You have selected Brazil nut chocolate" << endl;
              price2 = 0.35;
              cout << "The price is: " << price2 << endl;
      		cout << "Please insert £0.35p" << endl;
      		break;
      	case 'L':
      		cout << "You have selected Luxury chocolate" << endl;
              price3 = 0.50;
              cout << "The price is: " << price3 << endl;
      		cout << "Please insert £0.50p << endl;
      		break;
      	default:
      		cout << "Please Select again" << endl;
      		break;
      	}
      
      		int vend_selection(price1, price2, price3);
      		return 0;
      }
      THIS IS WHAT I HAVE DONE SO FAR, BUT I AM NOT SURE IF MY APPROACH IS CORRECT.
      Last edited by Banfa; Apr 6 '10, 09:16 PM. Reason: Added [code] ... [/code] tags

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Ahh, this is a classic error. You only want one price variable. Instead of 3 price variables, declare just one.
        int price;
        Then when you set it, set the single variable:
        price1 = 0.30; becomes price = 0.30;
        price2 = 0.35; becomes price = 0.35;
        price3 = 0.50; becomes price = 0.50;

        Then call the vending function with the single price.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          L13 You don`t explain what vendl expects i.e. P or B or L
          There is as yet no facility for the user to pay for the product or the program to verify the correct amount has been paid or for the calculation and return of correct change. Your function has not yet been defined and there is no code to keep the window open. You have made a good start. Try and get it to compile ASAP to find faults and make the necessary improvement.

          Comment

          • kobe382
            New Member
            • Apr 2010
            • 5

            #6
            Still having problems!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!!

            THE REASON I USED CASE P, CASE B AND CASE L FOR THE SWITCH STATEMENT WAS I WANTED THE USER TO INPUT P FOR PLAIN CHOCOLATE, B FOR BRAZIL NUT AND L FOR LUXURY CHOCOLATE. I TRIED USING RETURN VALUE FUNCTIONS FOR INPUTTING MONEY AND GETTING CHANGE BACK BUT THERE WERE ERRORS[ SEE BELOW]:

            Code:
            #include<iostream>
            #include<cmath>
            
            using namespace std;
            
            double vend_selection(double);
            
            int main ()
            {
            	char vend1;
            	double price, Plain, Brazil_nut, Luxury;
            	cout << "Select Chocolate: \n" << "\nP. Plain (30p)\n" << "B. Brazil nut(35p)\n" << "L. Luxury (50p)\n" << endl;
            	cin >> vend1;
            	switch(vend1)
            	{
            	case 'P':
            		cout << "You have selected Plain chocolate" << endl;
                    price=0.30;
            		cout << "The price is: " <<"£"<<price<<"p"<< endl;
            		cout << "Please insert £0.30p" << endl;
            		Plain();
                    break;
            	case 'B':
            		cout << "You have selected Brazil nut chocolate" << endl;
                    price=0.35;
                    cout << "The price is: " <<"£"<<price<<"p"<< endl;
            		cout << "Please insert £0.35p" << endl;
            		Brazil_nut();
            		break;
            	case 'L':
            		cout << "You have selected Luxury chocolate" << endl;
                    price=0.50;
                    cout << "The price is: " <<"£"<<price<<"p"<< endl;
            		cout << "Please insert £0.50p"<< endl;
            		Luxury();
            		break;
            	default:
            		cout << "Please Select again" << endl;
            		break;
            	}
            
            		double vend_selection(double price);
            
            
            		return 0;
            }
            double Plain(double change)
            {
            	double cash,price;
            	cout << "You have inserted "<<"£"<<cash<<"p"<<endl;
            	cash>price;
            	change=cash-price;
            	return change;
            	cout << "Your change is: "<<"£"<<change<<"p"<<endl;
            	cout << "Thank you" << endl;
            }
            double Plain(double change)
            {
            	double price,cash;
            	cout << "You have inserted " <<"£"<<cash<<"p"<<endl;
            	cash<price;
                change=price;
            	return change;
            	cout << "Please Insert 30p" << endl;
            }
            double Brazil_nut(double change)
            {
            	double cash,price;
            	cout << "You have inserted "<<"£"<<cash<<"p"<<endl;
            	cash>price;
            	change=cash-price;
            	return change;
            	cout << "Your change is: "<<"£"<<change<<"p"<<endl;
            	cout << "Thank you" << endl;
            }
            double Brazil_nut(double change)
            {
            	double price,cash;
            	cout << "You have inserted " <<"£"<<cash<<"p"<<endl;
            	cash<price;
                change=price;
            	return change;
            	cout << "Please Insert 35p" << endl;
            }
            double Luxury(double change)
            {
            	double cash,price;
            	cout << "You have inserted "<<"£"<<cash<<"p"<<endl;
            	cash>price;
            	change=cash-price;
            	return change;
            	cout << "Your change is: "<<"£"<<change<<"p"<<endl;
            	cout << "Thank you" << endl;
            }
            double Luxury(double change)
            {
            	double price,cash;
            	cout << "You have inserted " <<"£"<<cash<<"p"<<endl;
            	cash<price;
                change=price;
            	return change;
            	cout << "Please Insert 30p" << endl;
            }
            PS: I WANT THE INTEGER I INPUT AND THE CHANGE I RECEIVE TO BE IN DECIMAL FORM FOR EXAMPLE: £5.89p, should i stick with FLOAT OR DOUBLE FOR MY VARIABLES?
            }

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              It doesn't really matter, as long as you format the output correctly

              You want a single function, not 6 of them.
              Plain(); Brazil_nut(); Luxury(); should all be something like
              getMoney(price) ;
              since you have set the price. Now you want to get that much money from them.

              Also, getMoney should contain the price demands, eg:
              cout << "The price is: " <<"£"<< setiosflags(ios ::fixed) << setprecision(2) << x;
              <<"p"<< endl;
              cout << "Please insert £ << x << "p"<<endl;
              and then demand the money from them. These 2 lines are pretty much the same with all your options!

              Comment

              • kobe382
                New Member
                • Apr 2010
                • 5

                #8
                Sorry another problem!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!

                Code:
                #include<iostream>
                #include<cmath>
                
                using namespace std;
                
                double vend_selection(double);
                
                int main ()
                {
                	char vend1;
                	double price,getMoney;
                	cout << "Select Chocolate: \n" << "\nP. Plain (30p)\n" << "B. Brazil nut(35p)\n" << "L. Luxury (50p)\n" << endl;
                	cin >> vend1;
                	switch(vend1)
                	{
                	case 'P':
                		cout << "You have selected Plain chocolate" << endl;
                        price=0.30;
                		cout << "The price is: " <<"£"<<price<<"p"<< endl;
                		cout << "Please insert £0.30p" << endl;
                        getMoney(price);
                        break;
                	case 'B':
                		cout << "You have selected Brazil nut chocolate" << endl;
                        price=35;
                        cout << "The price is: " <<"£"<<price<<"p"<< endl;
                		cout << "Please insert £0.35p" << endl;
                		getMoney(price);
                		break;
                	case 'L':
                		cout << "You have selected Luxury chocolate" << endl;
                        price=50;
                        cout << "The price is: " <<"£"<<price<<"p"<< endl;
                		cout << "Please insert £0.50p"<< endl;
                		getMoney(price);
                		break;
                	default:
                		cout << "Please Select again" << endl;
                		break;
                	}
                
                		double vend_selection(double price);
                
                
                		return 0;
                }
                (1)Keep getting the error message: term does not evaluate to a function taking 1 arguments, for the program above

                (2) I didn't understand how to incorporate the setprecision(2)
                in to the program.

                (3) Could you please give me a hint on how to use the getmoney variable to get back change, if the money inserted is > (greater than) than the given value for the price and if it is less than< (in this case, should I use another function after return 0; or should all adjustments be made under the switch statement).

                I apologise if my questions are a lot I am new to c++, and I keep asking questions to be sure my approach is right, thanks.
                Last edited by NeoPa; Apr 9 '10, 01:39 PM. Reason: Edited out shout (all upper-case) text

                Comment

                • jkmyoung
                  Recognized Expert Top Contributor
                  • Mar 2006
                  • 2057

                  #9
                  (1) You haven't declared the getMoney function.

                  (2) not entirely sure, but you may have to put the money variable output on a seperate line: http://www.cplusplus.com/reference/i.../setprecision/

                  (3) How do you determine when a person has entered money? Do they just type in 5, 25, 1, 100, etc? Is it a response from another program?

                  Please stop using caps. Case was invented for a reason.

                  Comment

                  • kobe382
                    New Member
                    • Apr 2010
                    • 5

                    #10
                    Example!!!!!!!! !!!!!!!!!!!!!!! !!

                    i don't understand what you mean when you say i haven't declared the getMoney function, could you please give an example to elaborate, i need to actually see what is being done to fully understand.

                    Comment

                    • jkmyoung
                      Recognized Expert Top Contributor
                      • Mar 2006
                      • 2057

                      #11
                      Code:
                      double vend_selection(double); 
                        
                      int main ()
                      In these 2 lines you've declared a function called vend_selection (with code to be determined later), and declared function main, immediately putting the code inside it. After the main function, declare your function.

                      Comment

                      Working...