Jump to content

C++ function question


urlugal
 Share

10 posts in this topic

Recommended Posts

I have been hacking on this for a while now trying to figure it out and I am hitting a wall, maybe someone here can point me in the right direction. I have a switch that allows me to choose 3 different size cookies: small, medium, and large. Each size has a different price, I need to take the size that the user picks and then multiply that by the number of cookies that the user picks and give them the total. I just can't seem to figure out how to get from the size cookie the user picks to multiplying the price for that size and the number of cookies together. The switch only accepts integers and the prices are a float.

 

I have tried switching the value of the variable within the switch but the minute I step outside the switch I have just the integer value not the float value. If I try and call the switch with anything but an integer the compiler pukes. I just finished trying to write separate functions to handle the variable value switch but I cannot seem to get them to hold the value I want outside of the function, I think my functions are correct but I am not sure. The teacher is not the best and we are generally left to our own devices to figure out how to solve the problem. I am sure there is a less complicated way to do this but I can't seem to find it. The only requirement is that is must have switches and a menu listing the cookie sizes and prices, and no if/else statements. Here is what I have so far, I have tried to comment as best I can, thanks for any help. I am just looking for pointers on where to look or advice not for someone to do the work for me.

 

The next thoughts on my mind is that somehow I have declared the variables wrong of I have written the functions wrong for what I am trying to do. Thanks for any help anyone can give. BTW I am using Visual Studio 2005 as my IDE and compiler.

 

Thanks,

Matty

 

#include <iostream>
using namespace std;

void showmenu();
void small(); //function to swap the value of dPrice with dPriceSmall
void medium();  //function to swap the value of dPrice with dPriceMed
void large();  //function to swap the value of dPrice with dPricelarge

int main()
{
int nChoice; //this will be the cone size the person want
int nNumCones = 0;  //this is the number of cones the person wants
double dPrice = 0;  //this is what I want to use within the functions
double dPriceSmall = 1.29;  //the price for a small cone
double dPriceMed = 1.49;  //the price for a medium cone
double dPriceLarge = 1.79;  //the price for a large cone
double dTotal = 0;  //this will be the amount of the final bill

showmenu();

cin >> nChoice;
	switch(nChoice)
	{
		case 1 : small();
			// <old code> cout << "You have chosen a small cone" << endl;
			// <old code> dTotal = dPriceSmall * nChoice;
			break;
		case 2 : medium();
			// <old code> cout << "You have chosen a medium cone" << endl;
			// <old code> nChoice = dPriceMed; 
			break;
		case 3 : large();
			// <old code> cout << "You have chosen a large cone" << endl;
			// <old code> nChoice = dPriceLarge;
			break;
		default : cout << "That is not a choice please chose again"  << endl;
			// <old code> showmenu();
			// <old code> cin >> nChoice;
			exit(0);
	}

cout << "How many cones wuld you like to buy?\n";
cin >> nNumCones; // get the number of cones from the customer

dTotal = nNumCones * dPrice;  //calculate the price of the bill

cout << "Your total bill is $" << dTotal << ".\n" //print the price of the bill
		"Thank You and come back soon.";
return 0;
}


void showmenu()  // this is the menu used to show the sizes and prices
{
cout << "Please enter 1, 2, 3, or 0:\n"
	"1>  Small Cone $1.29\n"
	"2>  Medium Cone $1.49\n"
	"3>  large Cone $1.79\n";
}

void small()  //I am trying to set a variable that I can use for calculations 
{
double dPrice = 0; // setting the value to zero to start with
double dPriceSmall = 1.29;  //the price of a small cone
cout << "You have picked a small cone.\n";
dPrice = dPriceSmall; // trying to make the price of the small cone available for calcultions outside the switch
}

void medium()  //I am trying to set a variable that I can use for calculations   
{
double dPrice = 0; // setting the value to zero to start with
double dPriceMed = 1.49;  //the price of a medium cone
cout << "You have picked a medium cone.\n";
dPrice = dPriceMed; // trying to make the price of the medium cone available for calcultions outside the switch
}

void large()  //I am trying to set a variable that I can use for calculations 
{
double dPrice = 0; // setting the value to zero to start with
double dPriceLarge = 1.59;  //the price of a large cone
cout << "You have picked a large cone.\n";
dPrice = dPriceLarge; // trying to make the price of the large cone available for calcultions outside the switch
}

Link to comment
Share on other sites

I would suggest you think about your algorithm on paper before going onto code in order to get an idea of what it is exactly that you're doing and what you are using each variable for.

 

Now there are several things misplaced in your code that lead to the main problem: not being ablo to calculate dTotal, right?

 

Let's go through the main idea before correcting the code.

1- Get the customer to choose from a set of options.

2- Calculate the total based on the selections.

 

Sounds pretty simple, but as we can see in the above example, there are different approaches that may lead by nature to complicated

logic branching and other common mistakes.

 

Here's basically what you're doing (there seem to be two variations in your //old code):

1- Get the customer to select his options (so all parameters are set at this point).

 

Step 2A:

- Calculate dTotal by multiplying the cost of the selected size with the switch variable.

 

Step 2B:

- Reset nChoice to have the value of the chosen cone size (Normal, Medium, etc).

 

3- Recalculate dTotal by multiplying it with dPrice (which is always 0 in your code).

 

Comments:

The nChoice variable's task is to keep track of a users selection and NOT to store a price value!

(Note: If you multiply a double with an integer and save that value in an integer, then the value will be of type integer. This is what happened when you multiplied the price with nChoice.)

This solution will never work because you are mixing apples with oranges here and later multiplying all of that by dPrice (which is never assigned any other value than 0!).

 

It is clear to see that your intention is to calculate the dTotal value by using the price selection value somehow. You're just not making correct use of the switch statement.

 

SOLUTION:

- What you need to do is to give dPrice a value BASED on the user's cone-size selection in order to calculate the dTotal afterwards.

So in each select case you should calculate dPrice accordingly. For the first case, it would look like this:

case 1 :
 cout << "You have chosen a small cone" << endl;
 dPrice = dPriceSmall;
break;

Which value will be assinged to dPrice is exactly what the select statement is used for.

After that, dPrice will have the value of either dPriceSmall, dPriceMed or dPriceLarge and can be used for further calculations.

 

- Now you can calculate dTotal by multiplying dPrice with nNumCones.

 

Done.

 

There is no need for any extra functions here (remove the function calls from the select) but if you would like to try that alternative you have to let the functions access the outer variables and not re-define them! Ask your teacher to explain what "variable scope" means and how to use it correctly.

Basically, you have to remove the variable definitions inside the functions body, so the functions use the outer variables.

For example this:

void large() 
{
 double dPrice = 0; // setting the value to zero to start with
 double dPriceLarge = 1.59;  //the price of a large cone
 cout << "You have picked a large cone.\n";
 dPrice = dPriceLarge; // trying to make the price of the large cone available for calcultions outside the switch
}

Turns into this:

void large()  
{
 cout << "You have picked a large cone.\n";
 dPrice = dPriceLarge; // trying to make the price of the large cone available for calcultions outside the switch
}

Now you can just call each function from the select statement, like ie:

case 1 :
 cout << "You have chosen a small cone" << endl;
 small():
break;

 

Things to think about:

The switch statement is used to branch into a special section of your code to handle circumstances where a choice is made and thus adding a certain "variation" to your code. But this does not mean that any of the switch components (ie. the selection variable "nChoice") should be used for anything else than what it is intended to. This is what I meant by "don't mix apples with oranges".

 

Don't be afraid to use additional variables if you need to! But don't "borrow" other variables to store values they were not intended to.

 

 

Good luck!

 

- hecker

 

EDIT:

You wrote,

I need to take the size that the user picks and then multiply that by the number of cookies that the user picks and give them the total.

If this were true, then buying ice cream would be something like this:

You go to an ice cream parlor, order two medium cones of chocolate ice cream and the waiter says: "That'll be 2 medium, please."

Not very informative from a payers point of view now, is it?

 

What you want is to figure out is a VALUE which is BASED on the size the user picks first and then multiply that by the number of cones to give them the total.

 

 

EDIT2: HAHAHA, you know what? I just realized that I wrote cones of ice-cream instead of cookies. I blame the reefer.

 

EDIT3: I also noticed that there is a problem with your main code. If the select enters the "default" case (when nChoice is not 1,2 or 3), then your program re-queries the user to input new values but then just exits without re-calculating dPrice. It should actually jump to the part right before the select is handled. A way to do this would be with a do-while statement. You could also make it jump to right before you call the showmenu() function saving yourself the whole "default" part.

Link to comment
Share on other sites

EDIT3: I also noticed that there is a problem with your main code. If the select enters the "default" case (when nChoice is not 1,2 or 3), then your program re-queries the user to input new values but then just exits without re-calculating dPrice. It should actually jump to the part right before the select is handled. A way to do this would be with a do-while statement. You could also make it jump to right before you call the showmenu() function saving yourself the whole "default" part.

 

 

Thanks for the help, funny thing about the ice cream though. The example problem I have been reading and trying to figure out uses ice cream cones.

Link to comment
Share on other sites

I would suggest arrays and maybe classes. It is C++ after all.

make a class for cookies, and array with each size.

I don't see how that can be at all easier. Using classes for a simple selection algorithm is overkill and more so for someone who is just learing the basics.

Link to comment
Share on other sites

I would suggest arrays and maybe classes. It is C++ after all.

make a class for cookies, and array with each size.

 

 

Once I get this version I will try and do it that way as well. I am basically just using the class as a jumping off point and trying to learn as much as I can about c++, my programming skills are not what I want them to be. The more ways I figure out how to solve each problem, the better I will get.

Link to comment
Share on other sites

Thanks for the help, funny thing about the ice cream though. The example problem I have been reading and trying to figure out uses ice cream cones.

:) And more interesting is the question: were they cookies before I replied to your post?

Link to comment
Share on other sites

I don't see how that can be at all easier. Using classes for a simple selection algorithm is overkill and more so for someone who is just learing the basics.

 

Of course I don´t know how far he is in his lessons. But as this is c++, learning about classes is important and part of the basics (this is not c). (And classes overkill???)

Link to comment
Share on other sites

Of course I don´t know how far he is in his lessons. But as this is c++, learning about classes is important and part of the basics (this is not c). (And classes overkill???)
Yes, I consider it overkill because he's still learning about control stuctures and functions. Including classes at this level would just overcomplicate things and distract from the main objective, IMO (unless you're Tony Stark, I guess).
Link to comment
Share on other sites

 Share

×
×
  • Create New...